Skip to content

Do not zero-fill the buffer of a protocol on construction - #533

Open
philipcraig wants to merge 1 commit into
mainfrom
default-initialise-small-buffer
Open

philipcraig wants to merge 1 commit into
mainfrom
default-initialise-small-buffer

Conversation

@philipcraig

@philipcraig philipcraig commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #534.

construct_in_buffer started the lifetime of the buffer with std::construct_at, which value-initializes the array, so every construction, copy and move of a buffered object zero-filled the buffer first. It now default-initializes the array.

Constructing a protocol from a type that holds one double, GCC trunk, -O2:

; before
pxor    xmm1, xmm1
mov     QWORD PTR [rdi+24], 0
mov     rax, rdi
movups  XMMWORD PTR [rdi+8], xmm1
mov     QWORD PTR [rdi], OFFSET owning_vtable_for<Shape, Circle>
movsd   QWORD PTR [rdi+8], xmm0
ret

; after
mov     QWORD PTR [rdi], OFFSET owning_vtable_for<Shape, Circle>
mov     rax, rdi
movsd   QWORD PTR [rdi+8], xmm0
ret

An empty stored type then leaves the storage unwritten, and GCC fails an optimized build with -Werror=maybe-uninitialized when a const member is called on the protocol: the vtable entry is passed a pointer to const to storage that nothing has written. construct_in_buffer writes the one byte an empty object occupies.

A stored type whose constructor initializes nothing now gets that warning, as it does when it is a local variable. The zero-fill hid it.

construct_in_buffer started the lifetime of the buffer with
std::construct_at, which value-initializes the array. Default-initialize
it instead.

An empty stored type then leaves the storage unwritten, and GCC warns
with -Wmaybe-uninitialized when a const member is called. Write the one
byte an empty object occupies.
@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.72%. Comparing base (675a680) to head (8f5bf73).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #533   +/-   ##
=======================================
  Coverage   95.72%   95.72%           
=======================================
  Files          11       11           
  Lines         935      936    +1     
  Branches       31       31           
=======================================
+ Hits          895      896    +1     
  Misses         28       28           
  Partials       12       12           
Flag Coverage Δ
consteval 89.88% <ø> (ø)
runtime 98.05% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Constructing an object in the buffer of a protocol zero-fills the buffer

1 participant