Skip to content

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

@pengyuxiao

Description

@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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzercompiler-rt:asanAddress sanitizerquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions