Skip to contentSkip to conversation

stv090x: handle allocation failures

PATCH Superseded Dan Carpenter 2 participants 4 messages Feb 7, 2011 – Feb 27, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 stv090x: handle allocation failures

kmalloc() can fail so check whether state->internal is NULL. append_internal() can return NULL on allocation failures so check that. Also if we hit the error condition later in the function then there is a memory leak and we need to call remove_dev() to fix it.

---
Compile tested only. I'm not very familiar with this code.

drivers/media/dvb/frontends/stv090x.c +9 -1
index d3362d0..85101e8 100644
@@ -4783,7 +4783,13 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 	} else {
 		state->internal = kmalloc(sizeof(struct stv090x_internal),
 					  GFP_KERNEL);
+		if (!state->internal)
+			goto error;
 		temp_int = append_internal(state->internal);
+		if (!temp_int) {
+			kfree(state->internal);
+			goto error;
+		}
 		state->internal->num_used = 1;
 		state->internal->mclk = 0;
 		state->internal->dev_ver = 0;
@@ -4796,7 +4802,7 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 
 		if (stv090x_setup(&state->frontend) < 0) {
 			dprintk(FE_ERROR, 1, "Error setting up device");
-			goto error;
+			goto err_remove;
 		}
 	}
 
@@ -4811,6 +4817,8 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 
 	return &state->frontend;
 
+err_remove:
+	remove_dev(state->internal);
 error:
 	kfree(state);
 	return NULL;
#1 lore raw
Quoted 5 lines · On Monday 07 February 2011 17:56:50 Dan Carpenter wrote:

kmalloc() can fail so check whether state->internal is NULL. append_internal() can return NULL on allocation failures so check that. Also if we hit the error condition later in the function then there is a memory leak and we need to call remove_dev() to fix it.
...

Thanks for the patch. See my comment below.

Quoted 26 lines
drivers/media/dvb/frontends/stv090x.c +7 -1
@@ -4783,7 +4783,13 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 	} else {
 		state->internal = kmalloc(sizeof(struct stv090x_internal),
 					  GFP_KERNEL);
+		if (!state->internal)
+			goto error;
 		temp_int = append_internal(state->internal);
+		if (!temp_int) {
+			kfree(state->internal);
+			goto error;
+		}
 		state->internal->num_used = 1;
 		state->internal->mclk = 0;
 		state->internal->dev_ver = 0;
@@ -4796,7 +4802,7 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 
 		if (stv090x_setup(&state->frontend) < 0) {
 			dprintk(FE_ERROR, 1, "Error setting up device");
-			goto error;
+			goto err_remove;
 		}
 	}
 

drivers/media/dvb/frontends/stv090x.c struct dvb_frontend *stv090x_attach(const struct stv090x_config *config, · line 4821

Quoted 5 lines
drivers/media/dvb/frontends/stv090x.c +2 -0
@@ -4811,6 +4817,8 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 
 	return &state->frontend;
 
+err_remove:
+	remove_dev(state->internal);

AFAICS

        kfree(state->internal);

must be added here, as we allocated state->internal, state and temp_int.

drivers/media/dvb/frontends/stv090x.c struct dvb_frontend *stv090x_attach(const struct stv090x_config *config, · line 4824

Quoted 3 lines
drivers/media/dvb/frontends/stv090x.c +0 -0
 error:
 	kfree(state);
 	return NULL;

CU
Oliver

Signature · 5 lines
----------------------------------------------------------------
VDR Remote Plugin 0.4.0: http://www.escape-edv.de/endriss/vdr/
4 MByte Mod: http://www.escape-edv.de/endriss/dvb-mem-mod/
Full-TS Mod: http://www.escape-edv.de/endriss/dvb-full-ts-mod/
----------------------------------------------------------------
#2 lore raw

PATCH v2 1/1 stv090x: handle allocation failures

kmalloc() can fail so check whether state->internal is NULL. append_internal() can return NULL on allocation failures so check that. Also if we hit the error condition later in the function then there is a memory leak and we need to call remove_dev() to fix it.

Also Oliver Endriss pointed out an additional leak that I missed in the first version of this patch.

---
v2: Fix the leak Oliver noticed.

drivers/media/dvb/frontends/stv090x.c +10 -1
index d3362d0..41d0f0a 100644
@@ -4783,7 +4783,13 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 	} else {
 		state->internal = kmalloc(sizeof(struct stv090x_internal),
 					  GFP_KERNEL);
+		if (!state->internal)
+			goto error;
 		temp_int = append_internal(state->internal);
+		if (!temp_int) {
+			kfree(state->internal);
+			goto error;
+		}
 		state->internal->num_used = 1;
 		state->internal->mclk = 0;
 		state->internal->dev_ver = 0;
@@ -4796,7 +4802,7 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 
 		if (stv090x_setup(&state->frontend) < 0) {
 			dprintk(FE_ERROR, 1, "Error setting up device");
-			goto error;
+			goto err_remove;
 		}
 	}
 
@@ -4811,6 +4817,9 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
 
 	return &state->frontend;
 
+err_remove:
+	remove_dev(state->internal);
+	kfree(state->internal);
 error:
 	kfree(state);
 	return NULL;
Acked-by on v2 1/1
#3 lore raw
Quoted 13 lines · On Tuesday 15 February 2011 11:10:08 Dan Carpenter wrote:

kmalloc() can fail so check whether state->internal is NULL. append_internal() can return NULL on allocation failures so check that. Also if we hit the error condition later in the function then there is a memory leak and we need to call remove_dev() to fix it.

Also Oliver Endriss pointed out an additional leak that I missed in the first version of this patch.

---
v2: Fix the leak Oliver noticed.

drivers/media/dvb/frontends/stv090x.c

Quoted 1 line
drivers/media/dvb/frontends/stv090x.c +0 -0
...

CU
Oliver

Signature · 5 lines
----------------------------------------------------------------
VDR Remote Plugin 0.4.0: http://www.escape-edv.de/endriss/vdr/
4 MByte Mod: http://www.escape-edv.de/endriss/dvb-mem-mod/
Full-TS Mod: http://www.escape-edv.de/endriss/dvb-full-ts-mod/
----------------------------------------------------------------