Message ID | 20220316213055.2351342-1-morbo@google.com |
---|---|
State | Superseded |
Headers | show |
Series | gpiolib: acpi: use correct format characters | expand |
On Thu, Mar 17, 2022 at 11:06:51AM +0200, Andy Shevchenko wrote: > On Wed, Mar 16, 2022 at 02:30:55PM -0700, Bill Wendling wrote: > > When compiling with -Wformat, clang emits the following warning: > > > > drivers/gpio/gpiolib-acpi.c:393:4: warning: format specifies type > > 'unsigned char' but the argument has type 'int' [-Wformat] > > pin); > > ^~~ > > > > The types of these arguments are unconditionally defined, so this patch > > updates the format character to the correct ones for ints and unsigned > > ints. > > hhX specifier refers to unsigned char. It's a bug in the compiler. > > NAK. Oh, I read this wrong, sorry. The pin has been checked to fit in one byte, but its type is bigger indeed. I will apply your patch right away and send as a fix after rc1.
On Fri, Mar 18, 2022 at 11:01 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > On Fri, Mar 18, 2022 at 7:04 AM Andy Shevchenko > <andriy.shevchenko@linux.intel.com> wrote: > > > > On Thu, Mar 17, 2022 at 11:11:21AM -0700, Nick Desaulniers wrote: > > > Our goal is to enable -Wformat for CC=clang. Please see also: > > > commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of > > > unnecessary %h[xudi] and %hh[xudi]") > > > > Not that I agree on that commit for %h[h]x > > > > signed char ch = -1; > > printf("%x\n", ch); > > printf("%hhx\n", ch); > > Will print: > ffffffff > ff > I noticed this. My first thought was to do something akin to: printf("%x\n", (u8)ch); but went the route of removing the "h" qualifiers to be more in line with previous fixes. I will be happy to change this patch if that's what you would prefer. -bw
On Fri, Mar 18, 2022 at 11:25 AM Bill Wendling <morbo@google.com> wrote: > > On Fri, Mar 18, 2022 at 11:01 AM Nick Desaulniers > <ndesaulniers@google.com> wrote: > > > > On Fri, Mar 18, 2022 at 7:04 AM Andy Shevchenko > > <andriy.shevchenko@linux.intel.com> wrote: > > > > > > On Thu, Mar 17, 2022 at 11:11:21AM -0700, Nick Desaulniers wrote: > > > > Our goal is to enable -Wformat for CC=clang. Please see also: > > > > commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of > > > > unnecessary %h[xudi] and %hh[xudi]") > > > > > > Not that I agree on that commit for %h[h]x > > > > > > signed char ch = -1; > > > printf("%x\n", ch); > > > printf("%hhx\n", ch); > > > > Will print: > > ffffffff > > ff > > > I noticed this. My first thought was to do something akin to: > > printf("%x\n", (u8)ch); > > but went the route of removing the "h" qualifiers to be more in line > with previous fixes. I will be happy to change this patch if that's > what you would prefer. Should we add a note diagnostic to clang suggesting the explicit cast as one method of silencing the warning?
On Fri, Mar 18, 2022 at 11:01 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > Maybe we should reconsider our recommendations for signed types? For 'hhx' is probably does make sense in some cases. That said, for kernel work, if you work on byte values, I would seriously suggest not using 'char' at all, which has badly defined sign. And 'signed char' makes no sense either. So while 'hhx' makes sense in the general case, for kernel work I'd much rather see "don't use stupid types". So why not just use 'unsigned char' (or 'u8' if you think typing is boring). Linus
On Fri, Mar 18, 2022 at 11:29 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > On Fri, Mar 18, 2022 at 11:25 AM Bill Wendling <morbo@google.com> wrote: > > > > On Fri, Mar 18, 2022 at 11:01 AM Nick Desaulniers > > <ndesaulniers@google.com> wrote: > > > > > > On Fri, Mar 18, 2022 at 7:04 AM Andy Shevchenko > > > <andriy.shevchenko@linux.intel.com> wrote: > > > > > > > > On Thu, Mar 17, 2022 at 11:11:21AM -0700, Nick Desaulniers wrote: > > > > > Our goal is to enable -Wformat for CC=clang. Please see also: > > > > > commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of > > > > > unnecessary %h[xudi] and %hh[xudi]") > > > > > > > > Not that I agree on that commit for %h[h]x > > > > > > > > signed char ch = -1; > > > > printf("%x\n", ch); > > > > printf("%hhx\n", ch); > > > > > > Will print: > > > ffffffff > > > ff > > > > > I noticed this. My first thought was to do something akin to: > > > > printf("%x\n", (u8)ch); > > > > but went the route of removing the "h" qualifiers to be more in line > > with previous fixes. I will be happy to change this patch if that's > > what you would prefer. > > Should we add a note diagnostic to clang suggesting the explicit cast > as one method of silencing the warning? I don't think we should offer multiple suggestions in the notes. It could become confusing and make the diagnostic messages much bigger. That doesn't mean we couldn't change the suggestion. :-) -bw
On Fri, Mar 18, 2022 at 11:29 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > Should we add a note diagnostic to clang suggesting the explicit cast > as one method of silencing the warning? On the compiler side, I would love to see warnings about the ambiguity of the sign of 'char' in the general case. That said, I tried to add that to 'sparse' long long ago, and couldn't make it work sanely. All the approaches I tried all get _way_ too many false positives. I tried to come up with some way of figuring out "this code acts differently depending on whether 'char' is signed or not" and warning about it, and never could. And I suspect the same is true even for the much moire limited case of only format warnings. Because it's a *bad* idea to use '%d' (or almost any other format specifier) together with a 'char' argument, but only if you don't know the range of the char argument. But the other side of the argument is that quite often, people *do* know the range of the 'char' argument. If your 'char' type thing comes from some array or string that you control, and you used 'char' simply because you know you have small values (typical example: use it for an array of booleans etc), then it would be very annoying if the compiler warned you about using '%d'. There is no reason to use '%hhd' when you know your data range is [0,1]. So honestly, I don't think you can come up with a sane warning that doesn't cause *way* too many false positives and just annoys people. I'd love to be proven wrong. In fact, I'd _really_ love to be proven wrong for that generic case. The "sometimes 'char' is signed, sometimes it is unsigned, and it depends on the architecture and the compiler flags" can be a real problem. Linus
On Fri, Mar 18, 2022 at 11:41 AM Linus Torvalds <torvalds@linux-foundation.org> wrote: > > On Fri, Mar 18, 2022 at 11:29 AM Nick Desaulniers > <ndesaulniers@google.com> wrote: > > > > Should we add a note diagnostic to clang suggesting the explicit cast > > as one method of silencing the warning? > > On the compiler side, I would love to see warnings about the ambiguity > of the sign of 'char' in the general case. > > That said, I tried to add that to 'sparse' long long ago, and couldn't > make it work sanely. All the approaches I tried all get _way_ too many > false positives. > > I tried to come up with some way of figuring out "this code acts > differently depending on whether 'char' is signed or not" and warning > about it, and never could. > > And I suspect the same is true even for the much moire limited case of > only format warnings. > > Because it's a *bad* idea to use '%d' (or almost any other format > specifier) together with a 'char' argument, but only if you don't know > the range of the char argument. > > But the other side of the argument is that quite often, people *do* > know the range of the 'char' argument. If your 'char' type thing comes > from some array or string that you control, and you used 'char' simply > because you know you have small values (typical example: use it for an > array of booleans etc), then it would be very annoying if the compiler > warned you about using '%d'. > > There is no reason to use '%hhd' when you know your data range is [0,1]. > > So honestly, I don't think you can come up with a sane warning that > doesn't cause *way* too many false positives and just annoys people. > > I'd love to be proven wrong. In fact, I'd _really_ love to be proven > wrong for that generic case. The "sometimes 'char' is signed, > sometimes it is unsigned, and it depends on the architecture and the > compiler flags" can be a real problem. > My first thought is that this might be better suited for a static analyzer, like clang-tidy, that can do deeper analysis on code. It might still be difficult to weed out all of the false positives, but could be useful for specific offenders. -bw
On Sat, Mar 19, 2022 at 3:54 PM Linus Torvalds <torvalds@linux-foundation.org> wrote: > > So warning that '%hhX' is paired with an 'int' is all just completely > mindless and wrong. Sadly, I can see a different bogus warning reason why people would want to use '%02hhX'. Again, the *sane* thing from a human perspective is to use '%02X. But if the compiler doesn't do any range analysis at all, it could decide that "Oh, that print format could need up to 8 bytes of space in the result". Using '%02hhX' would cut that down to two. And since we use char ev_name[5]; and currently use "_%c%02hhX" as the format string, even a compiler that doesn't notice that "pin <= 255" test that guards this all will go "ok, that's at most 4 bytes and the final NUL termination, so it's fine". While a compiler - like gcc - that only sees that the original source of the 'pin' value is a 'unsigned short' array, and then doesn't take the "pin <= 255" into account, will warn like this: drivers/gpio/gpiolib-acpi.c: In function 'acpi_gpiochip_request_interrupt': drivers/gpio/gpiolib-acpi.c:206:24: warning: '%02X' directive writing between 2 and 4 bytes into a region of size 3 [-Wformat-overflow=] sprintf(ev_name, "_%c%02X", ^~~~ drivers/gpio/gpiolib-acpi.c:206:20: note: directive argument in the range [0, 65535] because gcc isn't being very good at that argument range analysis either. In other words, the original use of 'hhx' was bogus to begin with, and due to *another* compiler warning being bad, and we had that bad code being written back in 2016 to work around _that_ compiler warning (commit e40a3ae1f794: "gpio: acpi: work around false-positive -Wstring-overflow warning"). Sadly, two different bad compiler warnings together does not make for one good one. It just makes for even more pain. End result: I think the simplest and cleanest option is simply this: --- a/drivers/gpio/gpiolib-acpi.c +++ b/drivers/gpio/gpiolib-acpi.c @@ -387,8 +387,8 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares, pin = agpio->pin_table[0]; if (pin <= 255) { - char ev_name[5]; - sprintf(ev_name, "_%c%02hhX", + char ev_name[8]; + sprintf(ev_name, "_%c%02X", agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', pin); if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) which undoes that '%hhX' change for gcc, and replaces it with just using a slightly bigger stack allocation. It's not like a 5-byte allocation is in any way likely to have saved any actual stack, since all the other variables in that function are 'int' or bigger. False-positive compiler warnings really do make people write worse code, and that's a problem. But on a scale of bad code, I feel that extending the buffer trivially is better than adding a pointless cast that literally makes no sense. At least in this case the end result isn't unreadable or buggy. We've had several cases of bad compiler warnings that caused changes that were actually horrendously wrong. Linus
On Sun, Mar 20, 2022 at 1:06 PM Linus Torvalds <torvalds@linux-foundation.org> wrote: > On Sat, Mar 19, 2022 at 3:54 PM Linus Torvalds > <torvalds@linux-foundation.org> wrote: ... > End result: I think the simplest and cleanest option is simply this: Would you sign off on this? I will then replace the original patch with your version. And for the record I have a follow up patch to clearly show that pin is always unsigned, induced by this discussion.
On Mon, Mar 21, 2022 at 3:09 AM Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > > Would you sign off on this? I will then replace the original patch > with your version. Sure. Note that I (intentionally) do bogus indentation of my inline patches, because that thing wasn't actually _tested_. It's obvious enough and should fix the issue, but I just wanted to point that out. With that said, it's _so_ obviously correct (famous last words) that I'll happily add my sign-off to it: Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> and I suspect the explanations in the email could probably be used as much of a commit message. > And for the record I have a follow up patch to clearly show that pin > is always unsigned, induced by this discussion. Yeah, that sounds like a good idea - for a compiler it was obvious due to the load from a 'u16' array, but a human would actually have to go and check what that pin_table[] array was to see that "yup, that can't be negative in 'int'". Linus
diff --git a/drivers/gpio/gpiolib-acpi.c b/drivers/gpio/gpiolib-acpi.c index a5495ad31c9c..be6fb2ad2c4a 100644 --- a/drivers/gpio/gpiolib-acpi.c +++ b/drivers/gpio/gpiolib-acpi.c @@ -388,7 +388,7 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares, if (pin <= 255) { char ev_name[5]; - sprintf(ev_name, "_%c%02hhX", + sprintf(ev_name, "_%c%02X", agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', pin); if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
When compiling with -Wformat, clang emits the following warning: drivers/gpio/gpiolib-acpi.c:393:4: warning: format specifies type 'unsigned char' but the argument has type 'int' [-Wformat] pin); ^~~ The types of these arguments are unconditionally defined, so this patch updates the format character to the correct ones for ints and unsigned ints. Link: ClangBuiltLinux/linux#378 Signed-off-by: Bill Wendling <morbo@google.com> --- drivers/gpio/gpiolib-acpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)