From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
To: Dmitry Rokosov <ddrokosov@sberdevices.ru>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>,
kernel test robot <lkp@intel.com>, <llvm@lists.linux.dev>,
<kbuild-all@lists.01.org>,
"Linux Kernel Mailing List" <linux-kernel@vger.kernel.org>
Subject: Re: [jic23-iio:testing 124/129] drivers/iio/accel/msa311.c:993:24: warning: format specifies type 'unsigned char' but the argument has type 'unsigned int'
Date: Wed, 31 Aug 2022 09:38:10 +0100 [thread overview]
Message-ID: <20220831093810.00006112@huawei.com> (raw)
In-Reply-To: <20220831002405.m3j5sug2rz7bdz5s@Rockosov-MBP>
On Wed, 31 Aug 2022 03:24:05 +0300
Dmitry Rokosov <ddrokosov@sberdevices.ru> wrote:
> Hello Jonathan and Andy,
>
> Sorry for such a late response, a couple of days ago my daughter was born.
> So I couldn't reach my laptop :)
Congratulations and good luck! :)
>
> Please find my thoughts below.
>
> > > > >> drivers/iio/accel/msa311.c:993:24: warning: format specifies type 'unsigned char' but the argument has type 'unsigned int' [-Wformat]
> > > > "msa311-%hhx", partid);
> > > > ~~~~ ^~~~~~
> > > > %x
> > > > 1 warning generated.
> >
> > > > 992 msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL,
> > > > > 993 "msa311-%hhx", partid);
> >
> > > I'm thinking intent here was to limit range of what was printed. Maybe better to use
> > > local u8 variable or cast?
> > >
> > > I can fix it up if that's fine with you - or even better send me a patch that fixes
> > > it however you prefer!
> >
> > Looking back at what Linus said about those specifiers, I would rather
> > go with simple %x or %02x.
> >
> > P.S. Surprisingly many C developers don't know the difference between
> > %hhx and %02x, which is easy to check by
> >
> > char a = -1;
> > printf("%hhx <==> %02x\n", a, a);
> > a = 217;
> > printf("%hhx <==> %02x\n", a, a);
>
> Thank you for pointing to Linus answer. I have explored it at the link:
>
> https://lore.kernel.org/lkml/CAHk-=wgoxnmsj8GEVFJSvTwdnWm8wVJthefNk2n6+4TC=20e0Q@mail.gmail.com/
>
> Actually, Linus described one exception to this rule, which I have
> in my patch. I have an integer which I want to print as a char.
> I see that Linus mentions it's a bad idea. I agree with that. But
> currently %hhx => %02x replacement breaks the requested behavior, %02x
> will not shrink integer value to char. I want to say, maybe it's better
> just cast the value to u8 type and print as %x. What do you think? I can
> prepare such a patch.
>
> P.S. Andy's example to show the difference between %hhx and %02x makes
> more clear why such a replacement is not acceptable here.
>
> Output:
> ff <==> ffffffff
> d9 <==> ffffffd9
>
In this case the storage is an unsigned int, not an unsigned char.
Hence the value will be small and positive. So I'm fairly sure you
won't hit the above because it's
0x000000ff --> ff
0x000000d9 --> d9
The range is limited to 8 bits because that's all the underlying register
holds.
Jonathan
next prev parent reply other threads:[~2022-08-31 8:38 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-28 22:25 [jic23-iio:testing 124/129] drivers/iio/accel/msa311.c:993:24: warning: format specifies type 'unsigned char' but the argument has type 'unsigned int' kernel test robot
2022-08-30 10:03 ` Jonathan Cameron
2022-08-30 12:45 ` Andy Shevchenko
2022-08-30 12:51 ` Andy Shevchenko
2022-08-31 0:24 ` Dmitry Rokosov
2022-08-31 8:38 ` Jonathan Cameron [this message]
2022-08-31 21:04 ` Dmitry Rokosov
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=20220831093810.00006112@huawei.com \
--to=jonathan.cameron@huawei.com \
--cc=andy.shevchenko@gmail.com \
--cc=ddrokosov@sberdevices.ru \
--cc=kbuild-all@lists.01.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lkp@intel.com \
--cc=llvm@lists.linux.dev \
/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