| Issue |
171283
|
| Summary |
[clang] After creating a dynamic array, out-of-bounds access fails to trigger a proper error.
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
pengyuxiao
|
When we create dynamic arrays using the malloc statement in the C language, array accesses may inevitably result in out-of-bounds operations. In such cases, the compiler should generate appropriate warnings or errors to alert developers and prevent more severe consequences. However, the clang compiler currently fails to correctly report this issue.
For example, we edited the following code:
#include <stdio.h>
#include <stdlib.h>
void test_dynamic_oob() {
// Allocate memory for 5 ints (index 0 to 4)
int *arr_dynamic = (int*)malloc(5 * sizeof(int));
if (arr_dynamic == NULL) {
perror("malloc failed");
return;
}
// Out-of-bounds write (index 5)
arr_dynamic[5] = 200;
printf("Dynamic OOB: %d\n", arr_dynamic[5]);
free(arr_dynamic);
}
int main() {
test_dynamic_oob();
return 0;
}
When compiling and running the above code using clang version 14.0.0 with the command `clang -std=c99 -Wall data/seeds/clean/test_bounds.c -o test_clang`, the compiler failed to output any information, not even warnings. In large-scale project development, this compiler error could cause significant damage.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs