-fcatch- undefined -behavior only catches access to a local array that contains more than one element above the size

I have a question regarding the catch-undefined -behavior flag in clang. I tried this in a large project written in C, where at some point the integer value (i) provided by the user comes in. Then I added the following code:

int arr[3] arr[i] = 1234; 

But when I run the code using gdb, it stops only when the variable I have is 4 or more. Therefore, when I pass the value 3 to i, it still accesses the array outside of it, not stopping.

Is this a known limitation of -fcatch- undefined -behavior? Or does it only check if access is outside the stack frame, and not outside the local arrays?

Regards Christian

PS: I use clang + llvm 3.0 as a compiler / linker. The target is x86. The program runs inside the xubuntu 12.04 virtual machine in a Windows XP window.

+4
source share
1 answer

Appendix J of the ISO C standard contains the following undefined behavior relevant to your question:

  • Adding or subtracting a pointer to an array object or an integer type produces a result that does not indicate or extend beyond the limits of the same array object (6.5.6).
  • Adding or subtracting a pointer to an array object or an integer type gives a result that points directly to the array object and is used as the operand of the unary * operator, which is evaluated (6.5.6).

According to your post, Clang -fcatch-undefined-behavior seems to catch only the first of these two.

+1
source

Source: https://habr.com/ru/post/1442467/


All Articles