Re: [PATCH] scsi: scsi_debug: fix return checks for kcalloc

From: Dan Carpenter
Date: Thu Nov 04 2021 - 06:47:57 EST


This patch isn't right. It has no effect on run time.

On Wed, Nov 03, 2021 at 02:01:42PM -0500, George Kennedy wrote:
> Change return checks from kcalloc() to now check for NULL and
> ZERO_SIZE_PTR using the ZERO_OR_NULL_PTR macro or the following
> crash can occur if ZERO_SIZE_PTR indicator is returned.

ZERO_SIZE_PTR is when you allocate a zero bytes successfully. It's
quite useful because you can do:

p = kmalloc(bytes, GFP_KERNEL);

for (i = 0; i < bytes; i++)
printk("%d\n", p[i]);

The for loop works. Zero bytes now like normal thing instead of needing
to be handled as a special case.

The IS_ERR_OR_NULL() check treats ZERO_SIZE_PTR as a valid pointer.

> diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
> index 40b473e..222e985 100644
> --- a/drivers/scsi/scsi_debug.c
> +++ b/drivers/scsi/scsi_debug.c
> @@ -3909,7 +3909,7 @@ static int resp_comp_write(struct scsi_cmnd *scp,
> return ret;
> dnum = 2 * num;
> arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
> - if (NULL == arr) {
> + if (ZERO_OR_NULL_PTR(arr)) {

kcalloc() only returns NULL on error. This check is Yoda code but
besides the style issue it's fine.

> mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
> INSUFF_RES_ASCQ);
> return check_condition_result;
> @@ -4265,7 +4265,7 @@ static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
> return ret;
>
> arr = kcalloc(lb_size, vnum, GFP_ATOMIC);
> - if (!arr) {

The rest of these are correct. This patch doesn't fix a bug...

regards,
dan carpenter