public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: George Kennedy <george.kennedy@oracle.com>
Cc: gregkh@linuxfoundation.org, jejb@linux.ibm.com,
	martin.petersen@oracle.com, linux-scsi@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] scsi: scsi_debug: fix return checks for kcalloc
Date: Thu, 4 Nov 2021 13:47:27 +0300	[thread overview]
Message-ID: <20211104104727.GA3164@kadam> (raw)
In-Reply-To: <1635966102-29320-1-git-send-email-george.kennedy@oracle.com>

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


  parent reply	other threads:[~2021-11-04 10:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03 19:01 [PATCH] scsi: scsi_debug: fix return checks for kcalloc George Kennedy
2021-11-04  6:25 ` Greg KH
2021-11-04 15:21   ` George Kennedy
2021-11-04 10:47 ` Dan Carpenter [this message]
2021-11-04 16:04 ` Joe Perches
2021-11-04 17:22   ` George Kennedy
2021-11-04 17:50 ` Douglas Gilbert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211104104727.GA3164@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=george.kennedy@oracle.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jejb@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox