minor patch for slhc.c/ppp_deflate.c

Bill Hawes (whawes@star.net)
Mon, 26 Jan 1998 10:55:19 -0500


This is a multi-part message in MIME format.
--------------1E1E034C928208430D7FAF5B
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

The attached patch moves the MOD_INC_USE_COUNT macros in drivers/net/slhc.c and
ppp_deflate.c to ensure that the modules can't be unloaded if a call to kmalloc
blocks. The counts are now incremented at entry, and the failure exits go
through a common point to restore the count.

The patch is untested except for compilation, but should work with no change of
behavior.

Regards,
Bill
--------------1E1E034C928208430D7FAF5B
Content-Type: text/plain; charset=us-ascii; name="slhc_81-patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="slhc_81-patch"

--- linux-2.1.81/drivers/net/slhc.c.old Sun Nov 30 11:33:36 1997
+++ linux-2.1.81/drivers/net/slhc.c Sun Jan 25 11:50:09 1998
@@ -99,21 +99,18 @@
register struct cstate *ts;
struct slcompress *comp;

+ MOD_INC_USE_COUNT;
comp = (struct slcompress *)kmalloc(sizeof(struct slcompress),
GFP_KERNEL);
if (! comp)
- return NULL;
-
+ goto out_fail;
memset(comp, 0, sizeof(struct slcompress));

if ( rslots > 0 && rslots < 256 ) {
size_t rsize = rslots * sizeof(struct cstate);
comp->rstate = (struct cstate *) kmalloc(rsize, GFP_KERNEL);
if (! comp->rstate)
- {
- kfree((unsigned char *)comp);
- return NULL;
- }
+ goto out_free;
memset(comp->rstate, 0, rsize);
comp->rslot_limit = rslots - 1;
}
@@ -122,11 +119,7 @@
size_t tsize = tslots * sizeof(struct cstate);
comp->tstate = (struct cstate *) kmalloc(tsize, GFP_KERNEL);
if (! comp->tstate)
- {
- kfree((unsigned char *)comp->rstate);
- kfree((unsigned char *)comp);
- return NULL;
- }
+ goto out_free2;
memset(comp->tstate, 0, tsize);
comp->tslot_limit = tslots - 1;
}
@@ -151,8 +144,15 @@
ts[0].next = &(ts[comp->tslot_limit]);
ts[0].cs_this = 0;
}
- MOD_INC_USE_COUNT;
return comp;
+
+out_free2:
+ kfree((unsigned char *)comp->rstate);
+out_free:
+ kfree((unsigned char *)comp);
+out_fail:
+ MOD_DEC_USE_COUNT;
+ return NULL;
}


@@ -163,14 +163,14 @@
if ( comp == NULLSLCOMPR )
return;

- if ( comp->rstate != NULLSLSTATE )
- kfree( comp->rstate );
-
if ( comp->tstate != NULLSLSTATE )
kfree( comp->tstate );

- MOD_DEC_USE_COUNT;
+ if ( comp->rstate != NULLSLSTATE )
+ kfree( comp->rstate );
+
kfree( comp );
+ MOD_DEC_USE_COUNT;
}


--- linux-2.1.81/drivers/net/ppp_deflate.c.old Tue Dec 30 16:08:58 1997
+++ linux-2.1.81/drivers/net/ppp_deflate.c Mon Jan 26 11:26:21 1998
@@ -188,20 +188,20 @@
struct ppp_deflate_state *state;
int w_size;

+ MOD_INC_USE_COUNT;
if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
|| options[1] != CILEN_DEFLATE
|| DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
|| options[3] != DEFLATE_CHK_SEQUENCE)
- return NULL;
+ goto out_fail;
w_size = DEFLATE_SIZE(options[2]);
if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
- return NULL;
+ goto out_fail;

state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
if (state == NULL)
- return NULL;
+ goto out_fail;

- MOD_INC_USE_COUNT;
memset (state, 0, sizeof (struct ppp_deflate_state));
state->strm.next_in = NULL;
state->strm.zalloc = zalloc_init;
@@ -210,13 +210,16 @@

if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
- != Z_OK) {
- z_comp_free(state);
- return NULL;
- }
-
+ != Z_OK)
+ goto out_free;
state->strm.zalloc = zalloc;
return (void *) state;
+
+out_free:
+ z_comp_free(state);
+out_fail:
+ MOD_DEC_USE_COUNT;
+ return NULL;
}

static int
@@ -369,33 +372,35 @@
struct ppp_deflate_state *state;
int w_size;

+ MOD_INC_USE_COUNT;
if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
|| options[1] != CILEN_DEFLATE
|| DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
|| options[3] != DEFLATE_CHK_SEQUENCE)
- return NULL;
+ goto out_fail;
w_size = DEFLATE_SIZE(options[2]);
if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
- return NULL;
+ goto out_fail;

state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
if (state == NULL)
- return NULL;
-
- MOD_INC_USE_COUNT;
+ goto out_fail;
memset (state, 0, sizeof (struct ppp_deflate_state));
state->w_size = w_size;
state->strm.next_out = NULL;
state->strm.zalloc = zalloc_init;
state->strm.zfree = zfree;

- if (inflateInit2(&state->strm, -w_size) != Z_OK) {
- z_decomp_free(state);
- return NULL;
- }
-
+ if (inflateInit2(&state->strm, -w_size) != Z_OK)
+ goto out_free;
state->strm.zalloc = zalloc;
return (void *) state;
+
+out_free:
+ z_decomp_free(state);
+out_fail:
+ MOD_DEC_USE_COUNT;
+ return NULL;
}

static int

--------------1E1E034C928208430D7FAF5B--