public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104359] New: GCC Treats bool with value != 1 as falsey when picking branches
@ 2022-02-03 1:59 will at willusher dot io
2022-02-03 2:03 ` [Bug c++/104359] " pinskia at gcc dot gnu.org
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: will at willusher dot io @ 2022-02-03 1:59 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104359
Bug ID: 104359
Summary: GCC Treats bool with value != 1 as falsey when picking
branches
Product: gcc
Version: 11.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: will at willusher dot io
Target Milestone: ---
The following code when compiled with -O0 (or no -O option), will incorrectly
take both if branches:
#include <iostream>
#include <cstring>
int main() {
bool b = false;
std::memset(&b, 255, 1);
if (!b) {
std::cout << "!b = true branch\n";
} else {
std::cout << "!b = false branch\n";
}
if (b) {
std::cout << "b = true branch\n";
} else {
std::cout << "b = false branch\n";
}
return 0;
}
the resulting incorrect output is:
!b = true branch
b = true branch
However, b's "value" is 255, which should evaluate to "true". Recompiling the
program with -O1 or higher results in the correct output:
!b = false branch
b = true branch
Looking at compiler explorer: https://godbolt.org/z/TeMvMEc19 , it looks like
the if (!b) branch is compiled to:
movzx eax, BYTE PTR [rbp-1]
xor eax, 1
test al, al
je .L2
Which will evaluate to true for values != 1
This version of the code: https://godbolt.org/z/rhrP5W36v adds a loop and we
can see in -O1 and higher, the condition looks to just be a comparison vs. 0:
cmp BYTE PTR [rbp+0+rbx], 0
jne .L2
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/104359] GCC Treats bool with value != 1 as falsey when picking branches
2022-02-03 1:59 [Bug c++/104359] New: GCC Treats bool with value != 1 as falsey when picking branches will at willusher dot io
@ 2022-02-03 2:03 ` pinskia at gcc dot gnu.org
2022-02-03 2:47 ` will at willusher dot io
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-03 2:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104359
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Last reconfirmed| |2022-02-03
Status|UNCONFIRMED |WAITING
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is undefined behavior so anything can happen.
Why do you think GCC should act consistent here?
Using -fsanitize=undefined, I get:
/app/example.cpp:9:9: runtime error: load of value 255, which is not a valid
value for type 'bool'
/app/example.cpp:14:5: runtime error: load of value 255, which is not a valid
value for type 'bool'
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/104359] GCC Treats bool with value != 1 as falsey when picking branches
2022-02-03 1:59 [Bug c++/104359] New: GCC Treats bool with value != 1 as falsey when picking branches will at willusher dot io
2022-02-03 2:03 ` [Bug c++/104359] " pinskia at gcc dot gnu.org
@ 2022-02-03 2:47 ` will at willusher dot io
2022-02-03 2:55 ` pinskia at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: will at willusher dot io @ 2022-02-03 2:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104359
--- Comment #2 from Will Usher <will at willusher dot io> ---
Oh gotcha, then this can be closed.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/104359] GCC Treats bool with value != 1 as falsey when picking branches
2022-02-03 1:59 [Bug c++/104359] New: GCC Treats bool with value != 1 as falsey when picking branches will at willusher dot io
2022-02-03 2:03 ` [Bug c++/104359] " pinskia at gcc dot gnu.org
2022-02-03 2:47 ` will at willusher dot io
@ 2022-02-03 2:55 ` pinskia at gcc dot gnu.org
2022-02-03 9:40 ` redi at gcc dot gnu.org
2022-02-03 15:56 ` msebor at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-03 2:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104359
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |INVALID
Status|WAITING |RESOLVED
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Closing as requested.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/104359] GCC Treats bool with value != 1 as falsey when picking branches
2022-02-03 1:59 [Bug c++/104359] New: GCC Treats bool with value != 1 as falsey when picking branches will at willusher dot io
` (2 preceding siblings ...)
2022-02-03 2:55 ` pinskia at gcc dot gnu.org
@ 2022-02-03 9:40 ` redi at gcc dot gnu.org
2022-02-03 15:56 ` msebor at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-03 9:40 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104359
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Will Usher from comment #0)
> However, b's "value" is 255, which should evaluate to "true".
No. The only valid values of bool are true and false. If you set its bits to
some other value representation which does not correspond to either the 'true'
value or the 'false' value, you do not have an object of type bool, you have
garbage.
It's possible that a C++ implementation would use 0x11111111 for one of true or
false, in which case your memcpy would be valid. But that's not the case for
GCC.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/104359] GCC Treats bool with value != 1 as falsey when picking branches
2022-02-03 1:59 [Bug c++/104359] New: GCC Treats bool with value != 1 as falsey when picking branches will at willusher dot io
` (3 preceding siblings ...)
2022-02-03 9:40 ` redi at gcc dot gnu.org
@ 2022-02-03 15:56 ` msebor at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-02-03 15:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104359
Martin Sebor <msebor at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |msebor at gcc dot gnu.org
--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
It is undefined but the issue/question keeps coming up. The store that makes
the subsequent read undefined is clearly visible in the IL at all optimization
levels so it would be quite easy to issue a helpful warning for the code.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-02-03 15:56 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03 1:59 [Bug c++/104359] New: GCC Treats bool with value != 1 as falsey when picking branches will at willusher dot io
2022-02-03 2:03 ` [Bug c++/104359] " pinskia at gcc dot gnu.org
2022-02-03 2:47 ` will at willusher dot io
2022-02-03 2:55 ` pinskia at gcc dot gnu.org
2022-02-03 9:40 ` redi at gcc dot gnu.org
2022-02-03 15:56 ` msebor at gcc dot gnu.org
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).