llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[BoundsSafety] passing negative value as count to function returning `counted_by` does not error

Open hnrklssn opened this issue 4 months ago • 1 comments

Setting the length of a count to a known negative value is an error, but the error doesn't currently cover the case when the pointer is the return value of a function. Example (https://godbolt.org/z/15sfWEdhY):

int * __counted_by(len) foo(int len);
void bar(int * __counted_by(len) p, int len);

void baz(int * __counted_by(*len) *p, int *len) {
    *p = (void*)0; *len = -1;                              // error
}

void test() {
    int *p1 = foo(-1);                                     // no error!
    bar(p1, -1);                                           // error
    int len = -1; int * __counted_by(len) p2 = (void*)0;   // error
}

Related: rdar://80808704

hnrklssn avatar Jul 29 '26 03:07 hnrklssn

Interested in this issue!

pandaaured avatar Aug 02 '26 02:08 pandaaured