* [RFC] Warnings for cases where int promotion is unexpected and may cause bugs
@ 2022-06-17 1:11 Aniruddh Agarwal
2022-06-17 1:14 ` Aniruddh Agarwal
0 siblings, 1 reply; 2+ messages in thread
From: Aniruddh Agarwal @ 2022-06-17 1:11 UTC (permalink / raw)
To: gcc
Hello,
A colleague patched a prod-critical bug today caused by an overlooked implicit int promotion when adding uint8_t's. g++ (v12.1) doesn't report any warnings for it with all combinations of warnings flags that I've tried, so I thought I'd ask if:
- there *is* already some combination of warning flags that *would* report a warning for this code
- if not, then if there's any interest in work (which of course I'd be happy to contribute to) on detecting and flagging this sort of problem.
A (much simplified) example which illustrates the bug:
#+BEGIN_SRC cpp
#include <cstdint>
using std::uint8_t;
bool foo(uint8_t a, uint8_t b, uint8_t c) {
return (a + b) == c;
}
#+END_SRC
Here's the problem: the expectation here is that "a + b" will have type uint8_t. So, for example it expects "foo(200, 200, 144)" to return "true".
In reality, "a + b" implicitly promotes to an "int" and so we end up comparing 400 and 144, which returns false.
(Side note, not immediately relevant: I'm not sure if this ends up being equivalent to calling something like a "bool operator==(int, uint8_t)" or if the RHS is also implicitly promoted to an int before the comparison. This is irrelevant for the immediate example because the end result is the same in either case, but I would appreciate it if someone can shed light on what the standard has to say on this for future reference.)
A correct implementation of the expected behavior is instead therefore:
#+BEGIN_SRC cpp
#include <cstdint>
using std::uint8_t;
bool foo(uint8_t a, uint8_t b, uint8_t c) {
return static_cast<uint8_t>(a + b) == c;
}
#+END_SRC
Does anyone else find this very surprising, and as I asked above, does it seem worthwhile to try to flag code like in the first snippet? I don't know what gcc's general policy on trying to warn about code like this is. The new theoretical warning would be in the spirit of -Wconversion.
-Ani
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [RFC] Warnings for cases where int promotion is unexpected and may cause bugs
2022-06-17 1:11 [RFC] Warnings for cases where int promotion is unexpected and may cause bugs Aniruddh Agarwal
@ 2022-06-17 1:14 ` Aniruddh Agarwal
0 siblings, 0 replies; 2+ messages in thread
From: Aniruddh Agarwal @ 2022-06-17 1:14 UTC (permalink / raw)
To: gcc
Apologies, I didn't realize that my mail client doesn't
auto-wrap. Please find a wrapped copy of my original message below my
signature.
-Ani
On Thu, Jun 16, 2022, at 9:11 PM, Aniruddh Agarwal wrote:
> Hello,
>
> A colleague patched a prod-critical bug today caused by an overlooked
> implicit int promotion when adding uint8_t's. g++ (v12.1) doesn't
> report any warnings for it with all combinations of warnings flags that
> I've tried, so I thought I'd ask if:
>
> - there *is* already some combination of warning flags that *would*
> report a warning for this code
>
> - if not, then if there's any interest in work (which of course I'd be
> happy to contribute to) on detecting and flagging this sort of problem.
>
> A (much simplified) example which illustrates the bug:
> #+BEGIN_SRC cpp
> #include <cstdint>
>
> using std::uint8_t;
>
> bool foo(uint8_t a, uint8_t b, uint8_t c) {
> return (a + b) == c;
> }
> #+END_SRC
>
> Here's the problem: the expectation here is that "a + b" will have type
> uint8_t. So, for example it expects "foo(200, 200, 144)" to return
> "true".
>
> In reality, "a + b" implicitly promotes to an "int" and so we end up
> comparing 400 and 144, which returns false.
>
> (Side note, not immediately relevant: I'm not sure if this ends up
> being equivalent to calling something like a "bool operator==(int,
> uint8_t)" or if the RHS is also implicitly promoted to an int before
> the comparison. This is irrelevant for the immediate example because
> the end result is the same in either case, but I would appreciate it if
> someone can shed light on what the standard has to say on this for
> future reference.)
>
> A correct implementation of the expected behavior is instead therefore:
> #+BEGIN_SRC cpp
> #include <cstdint>
>
> using std::uint8_t;
>
> bool foo(uint8_t a, uint8_t b, uint8_t c) {
> return static_cast<uint8_t>(a + b) == c;
> }
> #+END_SRC
>
> Does anyone else find this very surprising, and as I asked above, does
> it seem worthwhile to try to flag code like in the first snippet? I
> don't know what gcc's general policy on trying to warn about code like
> this is. The new theoretical warning would be in the spirit of
> -Wconversion.
>
> -Ani
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-06-17 1:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-17 1:11 [RFC] Warnings for cases where int promotion is unexpected and may cause bugs Aniruddh Agarwal
2022-06-17 1:14 ` Aniruddh Agarwal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).