lib: buffer - Annotate w_buffer with sized_by(alloc) - #318
LaptopsPlural wants to merge 2 commits into
Conversation
Tie private real_buffer.w_buffer to alloc via sized_by when the compiler supports it, and publish alloc before updating the pointer in buffer_alloc() so the bound stays sound across growth.
| #if defined(__has_attribute) && __has_attribute(sized_by) | ||
| # define ATTR_SIZED_BY(member) __attribute__((sized_by(member))) | ||
| #elif defined(__has_attribute) && __has_attribute(__sized_by__) | ||
| # define ATTR_SIZED_BY(member) __attribute__((__sized_by__(member))) |
There was a problem hiding this comment.
Probably not useful to check for __sized_by__?
There was a problem hiding this comment.
Agreed — dropped the __sized_by__ spelling check. We now only gate on __has_attribute(sized_by) (empty macro otherwise).
|
Should we add this to our malloc() variants? |
|
Thanks for the review. On malloc variants: I think yes as a follow-up, not in this PR. This change is intentionally one concern — private Happy to open a follow-up that annotates the malloc variants once this lands, if that direction still looks useful to you. |
|
Thanks again for the malloc-variants question. I looked at folding that into this tip. The allocator APIs ( Prefer keeping this PR buffer-only ( |
Summary
real_buffer.w_bufferis the private writable allocation whose capacity is tracked byalloc. Annotate that relationship withsized_by(alloc)when the compiler provides the attribute (no-op otherwise), and inbuffer_alloc()assign the newallocbefore publishing the new pointer so the bound remains consistent while the buffer grows.No change to public
buffer_tlayout,writable_size/NUL reservation, or unsafe space helpers.AI-assisted disclosure
This change was prepared with AI assistance. I reviewed the diff for correctness and scope before opening this PR.