This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/68966] atomic_fetch_* on atomic_bool not diagnosed


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68966

--- Comment #8 from Martin Sebor <msebor at gcc dot gnu.org> ---
Code that directly manipulates the representation of _Bool objects is
potentially buggy.  GCC assumes that the representation is either all bits zero
(for false) or only the least significant bit is set (for true).  Anything else
yields unspecified (and possibly inconsistent) results.  For example, the
output program below shows that the result of the bitwise AND expression with a
bool variable with the wrong (but non-zero) representation yields false when
true should be expected.

$ cat xyz.c && /build/gcc-5.1.0/gcc/xgcc -B /build/gcc-5.1.0/gcc -Wall -xc
xyz.c && ./a.out 
int main ()
{
  _Bool b0 = 1, b1 = 1;

  __sync_add_and_fetch (&b0, 1);
  b1 += 1;

  _Bool c0 = b0 & 1, c1 = b1 & 1;

  __builtin_printf ("%#hhx (%s) & 1 = %#hhx (%s)\n"
                    "%#hhx (%s) & 1 = %#hhx (%s)\n",
                    *(char*)&b0, b0 ? "true" : "false",
                    *(char*)&c0, c0 ? "true" : "false",
                    *(char*)&b1, b1 ? "true" : "false",
                    *(char*)&c1, c1 ? "true" : "false");
}
xyz.c: In function ‘main’:
xyz.c:5:3: warning: value computed is not used [-Wunused-value]
   __sync_add_and_fetch (&b0, 1);
   ^
0x2 (true) & 1 = 0 (false)
0x1 (true) & 1 = 0x1 (true)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]