https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123569
Bug ID: 123569
Summary: Wrong code when using counted_by attribute
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: b.buschinski at googlemail dot com
Target Milestone: ---
With recent git gcc:
gcc version 16.0.0 20260108 (experimental)
4fbc0bbc03162f3962ea79bac29d36952867c90f (Gentoo 16.0.9999 p, commit
199ec9abeb96b37eb2b3b8974ec4a39b2525e231)
The code:
```
#include <stdio.h>
struct buffer {
#if 1
char * ptr __attribute((counted_by(len)));
#else
char * ptr;
#endif
int len;
};
int ltrim(struct buffer * const buf) {
while (buf->len > 0 && *buf->ptr == ' ') {
buf->len--;
buf->ptr++;
}
return buf->len;
}
int main() {
struct buffer buf = {.ptr = " 123", .len = 4};
int ret = ltrim(&buf);
fprintf(stderr, "ret: %u\n", ret);
return 0;
}
```
With counted_by, it outputs 0, without 3, which is correct.
Compiler Explorer: https://godbolt.org/z/3zWEqoxax