Re: [PATCH 3/3] drivers/scsi: Add kmalloc NULL tests

From: James Bottomley
Date: Thu Aug 06 2009 - 17:20:22 EST


On Thu, 2009-08-06 at 22:06 +0200, Julia Lawall wrote:
> From: Julia Lawall <julia@xxxxxxx>
>
> Check that the result of kmalloc is not NULL before passing it to other
> functions.
>
> The semantic match that finds this problem is as follows:
> (http://coccinelle.lip6.fr/)
>
> // <smpl>
> @@
> expression *x;
> identifier f;
> constant char *C;
> @@
>
> x = \(kmalloc\|kcalloc\|kzalloc\)(...);
> ... when != x == NULL
> when != x != NULL
> when != (x || ...)
> (
> kfree(x)
> |
> f(...,C,...,x,...)
> |
> *f(...,x,...)
> |
> *x->f
> )
> // </smpl>
>
> Signed-off-by: Julia Lawall <julia@xxxxxxx>
>
> ---
> drivers/scsi/sr.c | 2 ++
> 1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
> index cce0fe4..3fbf42e 100644
> --- a/drivers/scsi/sr.c
> +++ b/drivers/scsi/sr.c
> @@ -211,6 +211,8 @@ static int sr_media_change(struct cdrom_device_info *cdi, int slot)
> }
>
> sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
> + if (!sshdr)
> + return -ENOMEM;

this isn't necessary; sr_test_unit_ready is designed to take a NULL
pointer for sshdr ... it's just slightly less useful when this happens,
but it still returns some status.

James


> retval = sr_test_unit_ready(cd->device, sshdr);
> if (retval || (scsi_sense_valid(sshdr) &&
> /* 0x3a is medium not present */
> --
> To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/