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

--- Comment #2 from Tim Ruehsen <tim.ruehsen at gmx dot de> ---
(In reply to Jakub Jelinek from comment #1)
> This isn't a load, it is a cast, we sanitize just loads from memory.

Hmmm, seems ok if the compiler doesn't warn.
But the sanitizer IMO should trigger.

What if this cast has been done in a function returning flag_t ?
This could even be buggy code in an external library.
Then the sanitizer should definitely trigger.
And it doesn't with this code:

#include <stdio.h>

typedef enum {
        FLAG1 = (1 << 0),
        FLAG2 = (1 << 1),
} flag_t;

static flag_t setter(int x)
{
        return (flag_t) x;
}

int main(void)
{
        int x = 5;
        flag_t flags = setter(x);

        printf("flags = %X\n", flags);

        return 0;
}

$ g++-7 -O0 -fsanitize=undefined -fsanitize=enum -fno-sanitize-recover
enum_undef.cc
$ ./a.out
flags = 5

Hopefully, -O0 doesn't optimize into a single cast.

Reply via email to