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

            Bug ID: 119245
           Summary: Relational operator returns integers other than 0 or 1
                    for _Bool operands
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nandakumar at nandakumar dot co.in
  Target Milestone: ---

Created attachment 60721
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60721&action=edit
Minimal test program

The C standard states that relational operators should return 1 or 0. However,
GCC produces code that returns other values under specific conditions.

The attached minimal test program has a _Bool variable that is set to 1. The
variable is immediately set to 13 with the help of casting and pointer (to
avoid the compiler truncating 13 to 1). The next statement compares the same
variable with 1 using `==`, which returns 13 instead of 0 or 1.

This is probably caused by GCC replacing `1 == x` with `x`, which is valid if
`x` can only be 0 or 1. But _Bool at least requires one byte, and setting it to
non-one values (usually accidentally or as part of optimizations) to indicate
truth seems, well, at least not explicitly forbidden. If that's the case, this
is a problematic optimization.

The issue seems to originate at some earlier point, if I'm interpreting
test.c.005t.gimple (generated using -fdump-tree-all) correctly.

I originally faced this issue with gcc (Debian 10.2.1-6) 10.2.1 20210110, but
I've confirmed that the generated code is the same with gcc 14.2 using
https://godbolt.org/.

Yes, I've tried `-Wall -Wextra -fno-strict-aliasing -fwrapv`.

Debian clang version 11.0.1-2 (x86_64-pc-linux-gnu) produces `1`, the expected
output.

Reply via email to