llvm / llvm/llvm-project

[clang] After creating a dynamic array, out-of-bounds access fails to trigger a proper error.

Open
#171,283 3 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics compiler-rt:asan question
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

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:
```c
#include
#include

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.

Contributor guide

Open the contributing guide

Research direction

Reproduce the report with data/seeds/clean/test_bounds.c using the stated clang -std=c99 -Wall command, and confirm the current diagnostic behavior for arr_dynamic[5]. Investigate the relevant Clang diagnostic entry points, then define completion as producing an appropriate warning or error for this dynamic-array out-of-bounds access.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.