This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug sanitizer/81598] -fsanitize=enum does not detect range violation
- From: "tim.ruehsen at gmx dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 28 Jul 2017 10:40:52 +0000
- Subject: [Bug sanitizer/81598] -fsanitize=enum does not detect range violation
- Auto-submitted: auto-generated
- References: <bug-81598-4@http.gcc.gnu.org/bugzilla/>
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.