public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "Luis R. Rodriguez" <mcgrof@kernel.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH 2/2] test_firmware: silence underflow warning in test_dev_config_update_u8()
Date: Thu, 21 Feb 2019 22:18:56 +0300	[thread overview]
Message-ID: <20190221191855.GB1737@kadam> (raw)
In-Reply-To: <20190221105458.409d1968961962079c54b815@linux-foundation.org>

On Thu, Feb 21, 2019 at 10:54:58AM -0800, Andrew Morton wrote:
> On Thu, 21 Feb 2019 21:38:26 +0300 Dan Carpenter <dan.carpenter@oracle.com> wrote:
> 
> > We put an upper bound on "new" but we don't check for negatives.
> 
> U8_MAX has unsigned type, so `if (new > U8_MAX)' does check for negative.
> 

No, doesn't work in this case.

#define U8_MAX          ((u8)~0U)

It would need to unsigned long for the type promotion to prevent
negatives, but it starts as unsigned int, then unsigned char, which is
type promoted to int.

It would be more clear to just write it as:

#define U8_MAX 0xff

> > In
> > this case the underflow doesn't matter very much, but we may as well
> > make the static checker happy.
> > 
> > ...
> >
> > --- a/lib/test_firmware.c
> > +++ b/lib/test_firmware.c
> > @@ -326,15 +326,12 @@ static ssize_t test_dev_config_show_int(char *buf, int cfg)
> >  static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
> >  {
> >  	int ret;
> > -	long new;
> > +	u8 new;
> >  
> > -	ret = kstrtol(buf, 10, &new);
> > +	ret = kstrtou8(buf, 10, &new);
> >  	if (ret)
> >  		return ret;
> >  
> > -	if (new > U8_MAX)
> > -		return -EINVAL;
> > -
> >  	mutex_lock(&test_fw_mutex);
> >  	*(u8 *)cfg = new;
> >  	mutex_unlock(&test_fw_mutex);
> 
> if *buf=="257",
> 
> previous behavior: -EINVAL
> new behavior: *cfg = 1
> 
> yes?

No.  The kstrtou8() check the limit so it will return -ERANGE.  I should
have probably spelled that out better in the commit message.  I'll
resend tomorrow.

regard,
dan carpenter


  reply	other threads:[~2019-02-21 19:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-21 18:37 [PATCH 1/2] test_firmware: remove some dead code Dan Carpenter
2019-02-21 18:38 ` [PATCH 2/2] test_firmware: silence underflow warning in test_dev_config_update_u8() Dan Carpenter
2019-02-21 18:54   ` Andrew Morton
2019-02-21 19:18     ` Dan Carpenter [this message]
2019-02-21 19:54       ` Andrew Morton
2019-02-22  7:29         ` Dan Carpenter

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=20190221191855.GB1737@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=rdunlap@infradead.org \
    /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