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

            Bug ID: 103887
           Summary: -fsanitize=shift affects constness of an expression
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m...@mk-sys.cz
  Target Milestone: ---

This simplified testcase

---------------------------------------------------------------------------
#include <stdint.h>

#define ASSERT_ON_COMPILE_SELECTOR_SIZE(e)                                \
        _Static_assert(((__builtin_constant_p(e) && ((e) >> 16) == 0)) || \
                       (sizeof(e) <= 2), "problem")

int f(uint16_t tr)
{
        ASSERT_ON_COMPILE_SELECTOR_SIZE(tr);
        return 0;
}
---------------------------------------------------------------------------

builds cleanly with "-O2" but fails with "-O2 -fsanitize=shift":

---------------------------------------------------------------------------
mike@lion:/tmp/gcc> gcc-11 -Wall -Wextra -O2 -c foo.c
mike@lion:/tmp/gcc> gcc-11 -Wall -Wextra -O2 -fsanitize=shift -c foo.c
foo.c: In function ‘f’:
foo.c:4:72: error: expression in static assertion is not constant
    4 |         _Static_assert(((__builtin_constant_p(e) && ((e) >> 16) == 0))
|| \
      |                       
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
    5 |                        (sizeof(e) <= 2), "problem")
      |                        ~~~~~~~~~~~~~~~~                                 
foo.c:9:9: note: in expansion of macro ‘ASSERT_ON_COMPILE_SELECTOR_SIZE’
    9 |         ASSERT_ON_COMPILE_SELECTOR_SIZE(tr);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---------------------------------------------------------------------------

I was able to reproduce the same with various gcc version from gcc7 to gcc12.

Reply via email to