public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
@ 2015-09-02 7:53 ` xuejuncao at gmail dot com
2015-09-02 7:58 ` pinskia at gcc dot gnu.org
` (9 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: xuejuncao at gmail dot com @ 2015-09-02 7:53 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
xuejuncao <xuejuncao at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|?: expresion returns |?: expression returns
|unexpected value when |unexpected value when
|condition is a bool |condition is a bool
|variable and it's value |variable which memory is
|above 1 |not true/false
--- Comment #1 from xuejuncao <xuejuncao at gmail dot com> ---
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main()
{
union u {
bool b;
char c;
} u;
memset((void *)&u, -1, sizeof(u));
bool b = (u.b ? 1 : 0);
int i = (u.b ? 1 : 0);
int j = (u.b ? 1 : 2);
printf("b = %d, i = %d, j = %d\n", b, i, j);
return 0;
}
without optimisation, the output is:
b = 1, i = 255, j = 1
with -O2, is:
b = 255, i = 255, j = 1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
2015-09-02 7:53 ` [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false xuejuncao at gmail dot com
@ 2015-09-02 7:58 ` pinskia at gcc dot gnu.org
2015-09-02 8:11 ` xuejuncao at gmail dot com
` (8 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2015-09-02 7:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Bool can only be 0 or 1, any other value causes undefined behavior.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
2015-09-02 7:53 ` [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false xuejuncao at gmail dot com
2015-09-02 7:58 ` pinskia at gcc dot gnu.org
@ 2015-09-02 8:11 ` xuejuncao at gmail dot com
2015-09-02 8:20 ` xuejuncao at gmail dot com
` (7 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: xuejuncao at gmail dot com @ 2015-09-02 8:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #3 from xuejuncao <xuejuncao at gmail dot com> ---
(In reply to Andrew Pinski from comment #2)
> Bool can only be 0 or 1, any other value causes undefined behavior.
but it seems like a trap, the below code will not work fine, and will make
program abort.
(expect to write "1/0" that only takes 1 byte, but write 2 bytes when value is
negative)
https://github.com/google/protobuf/blob/master/src/google/protobuf/wire_format_lite_inl.h
inline void WireFormatLite::WriteBoolNoTag(bool value,
io::CodedOutputStream* output) {
output->WriteVarint32(value ? 1 : 0);
}
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (2 preceding siblings ...)
2015-09-02 8:11 ` xuejuncao at gmail dot com
@ 2015-09-02 8:20 ` xuejuncao at gmail dot com
2015-09-02 8:21 ` pinskia at gcc dot gnu.org
` (6 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: xuejuncao at gmail dot com @ 2015-09-02 8:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #4 from xuejuncao <xuejuncao at gmail dot com> ---
btw, the same code works fine on Mac:
$gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (3 preceding siblings ...)
2015-09-02 8:20 ` xuejuncao at gmail dot com
@ 2015-09-02 8:21 ` pinskia at gcc dot gnu.org
2015-09-02 8:22 ` pinskia at gcc dot gnu.org
` (5 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2015-09-02 8:21 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Try -fsantitized=undefined to catch this error.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (4 preceding siblings ...)
2015-09-02 8:21 ` pinskia at gcc dot gnu.org
@ 2015-09-02 8:22 ` pinskia at gcc dot gnu.org
2015-09-02 8:33 ` pinskia at gcc dot gnu.org
` (4 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2015-09-02 8:22 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to xuejuncao from comment #4)
> btw, the same code works fine on Mac:
>
> $gcc --version
> Configured with: --prefix=/Library/Developer/CommandLineTools/usr
> --with-gxx-include-dir=/usr/include/c++/4.2.1
> Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
> Target: x86_64-apple-darwin14.5.0
> Thread model: posix
So this is undefined behavior.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (5 preceding siblings ...)
2015-09-02 8:22 ` pinskia at gcc dot gnu.org
@ 2015-09-02 8:33 ` pinskia at gcc dot gnu.org
2015-09-02 9:17 ` trippels at gcc dot gnu.org
` (3 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2015-09-02 8:33 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I should say the value that is stored in the bool variable can only be 0 or 1;
anything else is undefined bahavior.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (6 preceding siblings ...)
2015-09-02 8:33 ` pinskia at gcc dot gnu.org
@ 2015-09-02 9:17 ` trippels at gcc dot gnu.org
2015-09-02 10:15 ` xuejuncao at gmail dot com
` (2 subsequent siblings)
10 siblings, 0 replies; 11+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-09-02 9:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
Markus Trippelsdorf <trippels at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |trippels at gcc dot gnu.org
--- Comment #9 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
(In reply to xuejuncao from comment #8)
> thanks, i get the below result with -fsanitize=undefined
>
> boolmagic.c:12:16: runtime error: load of value 255, which is not a valid
> value for type '_Bool'
> boolmagic.c:13:15: runtime error: load of value 255, which is not a valid
> value for type '_Bool'
> boolmagic.c:14:15: runtime error: load of value 255, which is not a valid
> value for type '_Bool'
> b = 1, i = 1, j = 1
>
> but, can i say the "-fsanitize=undefined" will _corrects_ the undefined
> behaviour?
>
> or replace the code by if/else, which works fine (lucky?)
>
> int i; // = (u.b ? 1 : 0);
> if (u.b) i = 1; else i = 0;
No, you're still invoking undefined behavior. -fsanitize=undefined will not
fix this at all; it just points out the issues.
You need to get rid of the underlying problem, but this is not the place
to discuss this further.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (7 preceding siblings ...)
2015-09-02 9:17 ` trippels at gcc dot gnu.org
@ 2015-09-02 10:15 ` xuejuncao at gmail dot com
2015-09-02 10:20 ` rguenth at gcc dot gnu.org
2015-09-02 10:59 ` xuejuncao at gmail dot com
10 siblings, 0 replies; 11+ messages in thread
From: xuejuncao at gmail dot com @ 2015-09-02 10:15 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #10 from xuejuncao <xuejuncao at gmail dot com> ---
when bool value read from unpacked stream or file, wen can not ensure it's 1 or
0;
so maybe the best solution is compile with "-D_Bool=char" for now :-/
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (8 preceding siblings ...)
2015-09-02 10:15 ` xuejuncao at gmail dot com
@ 2015-09-02 10:20 ` rguenth at gcc dot gnu.org
2015-09-02 10:59 ` xuejuncao at gmail dot com
10 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-09-02 10:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #11 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to xuejuncao from comment #10)
> when bool value read from unpacked stream or file, wen can not ensure it's 1
> or 0;
> so maybe the best solution is compile with "-D_Bool=char" for now :-/
You can always do the read with 'char' and then convert to _Bool.
You can also "fix" your testcase (I think, didn't try) by using
union u {
bool b : 1;
char c;
} u;
(in case unions allow bitfield members)
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
` (9 preceding siblings ...)
2015-09-02 10:20 ` rguenth at gcc dot gnu.org
@ 2015-09-02 10:59 ` xuejuncao at gmail dot com
10 siblings, 0 replies; 11+ messages in thread
From: xuejuncao at gmail dot com @ 2015-09-02 10:59 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433
--- Comment #12 from xuejuncao <xuejuncao at gmail dot com> ---
(In reply to Richard Biener from comment #11)
> (In reply to xuejuncao from comment #10)
> > when bool value read from unpacked stream or file, wen can not ensure it's 1
> > or 0;
> > so maybe the best solution is compile with "-D_Bool=char" for now :-/
>
> You can always do the read with 'char' and then convert to _Bool.
>
> You can also "fix" your testcase (I think, didn't try) by using
>
> union u {
> bool b : 1;
> char c;
> } u;
>
> (in case unions allow bitfield members)
yes, it works, but likes a trick:-)
bit operation to bool is also undefined
int i = (u.b & 1 ? 1 : 0); // works without -O2, useless with -O2
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2015-09-02 10:59 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <bug-67433-4@http.gcc.gnu.org/bugzilla/>
2015-09-02 7:53 ` [Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false xuejuncao at gmail dot com
2015-09-02 7:58 ` pinskia at gcc dot gnu.org
2015-09-02 8:11 ` xuejuncao at gmail dot com
2015-09-02 8:20 ` xuejuncao at gmail dot com
2015-09-02 8:21 ` pinskia at gcc dot gnu.org
2015-09-02 8:22 ` pinskia at gcc dot gnu.org
2015-09-02 8:33 ` pinskia at gcc dot gnu.org
2015-09-02 9:17 ` trippels at gcc dot gnu.org
2015-09-02 10:15 ` xuejuncao at gmail dot com
2015-09-02 10:20 ` rguenth at gcc dot gnu.org
2015-09-02 10:59 ` xuejuncao at gmail dot com
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).