Checks not generated for vla-bound when the size is unsigned
| Bugzilla Link | 50127 |
| Version | unspecified |
| OS | Linux |
| Attachments | Source file with VLAs being instantiated via signed and unsigned types |
Extended Description
The vla-bound checker for UBSan does not generate the necessary checks when the size of the VLA is an unsigned type. Given the following code (vla-bound.c, also attached):
#include <stdlib.h>
long int s = 0; int main(void) { int w[(size_t)s]; int x[(ssize_t)s]; int y[(unsigned)s]; int z[(int)s]; return 0; }
Inspecting the IR generated from compiling with clang -S -emit-llvm -O0 -fsanitize=vla-bound vla-bound.c we see that there's only two checks generated for the VLA bounds checking:
$ clang -S -emit-llvm -O0 -fsanitize=vla-bound vla-bound.c $ grep -P "^handler.vla_bound_not_positive" vla-bound.ll handler.vla_bound_not_positive: ; preds = %entry handler.vla_bound_not_positive4: ; preds = %cont
Further comparing this behaviour between clang and gcc, we see that gcc performs the checking on the VLA size for both signed and unsigned variable types:
clang:
$ clang -O0 -fsanitize=vla-bound vla-bound.c $ ./a.out vla-bound.c:6:9: runtime error: variable length array bound evaluates to non-positive value 0 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior vla-bound.c:6:9 in vla-bound.c:8:9: runtime error: variable length array bound evaluates to non-positive value 0 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior vla-bound.c:8:9 in
gcc:
$ gcc -O0 -fsanitize=vla-bound vla-bound.c $ ./a.out vla-bound.c:5:7: runtime error: variable length array bound evaluates to non-positive value 0 vla-bound.c:6:7: runtime error: variable length array bound evaluates to non-positive value 0 vla-bound.c:7:7: runtime error: variable length array bound evaluates to non-positive value 0 vla-bound.c:8:7: runtime error: variable length array bound evaluates to non-positive value 0
We would expect that the behaviour of both clang and gcc would agree when it comes to checking the VLA bounds. Reading through the C specification does not indicate any limitation on whether or not a VLA's size needs to be in the form of a signed or unsigned type so there seems no reason why this check shouldn't be performed.
Resolved in b2715660ed0f821619f51158fb92cd8bddd105d8