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 sanitizer/81598] -fsanitize=enum does not detect range violation


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

--- Comment #8 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #7)
> As for the switch, I think there shouldn't be any runtime error, there is no
> UB.
> You can have default: label even if you list all the possible in-range
> cases, you can have enum values in between min/max that aren't in the
> enumeration, and you can have case labels outside of the enum range too,
> that is something for a warning, but not a runtime error.

I've just noticed that in this example:

cat enum2.c
int main(int argc, char **argv)
{
  char c = (char)argc;

  bool x = argc;

  switch (c)
  {
  case -128 ... 127:
    break;
  default:
    return 2;
  }

  switch (x)
  {
    case true:
      return 2;
    case false:
      return 3;
    default:
      return 4;
  }
}

We are able to simplify the second switch in vrp:

[...]
Folding statement: switch (x_4) <default: <L5> [33.33%], case 0: <L4> [33.33%],
case 1: <L8> [33.33%]>
removing unreachable case label
[...]

But we can't do it for the first switch. That's probably missing optimization.
I'm interested in the case where we've got call enum values covered and default
is provided: would it be valid to
simply remove the block (via __builtin_unreachable or similarly)?

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