Hi Qing and Jakub,
I am bit confused about the restrictions we have about
counted_by on pointers according to the documentation, i.e.
struct buffer {
char * ptr __attribute__((counted_by(len)));
int len;
};
https://godbolt.org/z/Y44Mfv5bo
The documentation says:
---------
In addition to the above requirements, there is one more
requirement between this pair if and only if p->array is an
array that is pointed by the pointer field:
p->array and p->count can only be changed by changing the
whole structure at the same time.
---------
Why is this the case?
------------------------
One important feature of the attribute is that a reference
to the flexible array member field uses the latest value
assigned to the field that represents the number of the
elements before that reference.
Note, however, the above feature is not valid for the pointer field.
-------------------------
Here, why is this not the case for the pointer?
For example, for
struct buffer {
char * ptr __attribute((counted_by(len)));
int len;
};
int main() {
struct buffer buf = {.ptr = " 123", .len = 4};
buf.ptr[4] = 1;
return 0;
}
we create the following gimple:
buf = {};
buf.ptr = " 123";
buf.len = 4;
_2 = buf.ptr;
_1 = .ACCESS_WITH_SIZE (_2, &buf.len, 0B, 1);
_3 = MEM <int> [(void *)&buf + 8B];
_4 = MAX_EXPR <_3, 0>;
_5 = (sizetype) _4;
.UBSAN_BOUNDS (0B, 4, _5);
_6 = _1 + 4;
*_6 = 1;
Martin