Skip to content

-Warray-bounds misses unsafe pointer arithmetic #136379

@shuffle2

Description

@shuffle2

I would expect the following to issue a warning:

void g(uint64_t a, uint64_t b) {
    printf("%lx %lx\n", a, b);
}

int main(int argc, char **argv) {
    uint8_t a;
    
    // one-past the end is valid (as long as not deref'd)
    g((uint64_t)&a, (uint64_t)(&a + 1));
    // >1 past end is invalid
    // XXX clang has -Warray-bounds, but it does not warn on the below.
    // clang's -Wunsafe-buffer-usage *does* warn on it, though.
    // -Wunsafe-buffer-usage doesn't seem usable in real world tho for C code. (lots of false positives).
    g((uint64_t)&a, (uint64_t)(&a + 2));
    
    return 0;
}

gcc detects this as I'd expect, clang does not: https://godbolt.org/z/WEYTzMGGb

It's unclear to me if -Wunsafe-buffer-usage is the expected solution here - this flag seems unhelpful for plain C code. https://clang.llvm.org/docs/SafeBuffers.html makes it sound like the flag is mainly for use in C++ code, to detect locations that should be converted to c++-specific code patterns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-negativeWarning doesn't fire when it should

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions