Skip to content

Zero padding in padded_string_builder::convert() and print padded_string by size - #3156

Merged
lemire merged 1 commit into
masterfrom
padded-string-builder-convert-padding
Dec 1, 2026
Merged

lemire merged 1 commit into
masterfrom
padded-string-builder-convert-padding

Conversation

@lemire

@lemire lemire commented Nov 30, 2026

Copy link
Copy Markdown
Member

Reported by @perfloop.

padded_string_builder allocates its buffer with allocate_padded_buffer(capacity), which zeroes only the bytes after capacity. The bytes between size and capacity are left uninitialized, and convert() handed them over as the padding of the resulting padded_string. As a result:

  • the padding after size() could hold stale heap bytes, contrary to the padded_string guarantee, and parsing could read uninitialized memory under MSan or valgrind;
  • operator<< printed s.data() as a C string, so it wrote those stale bytes after the content.

convert() now zeroes the SIMDJSON_PADDING bytes after size. They are always inside the allocation, which holds capacity + SIMDJSON_PADDING bytes.

operator<< now writes size() bytes. This also fixes a crash when printing a default-constructed or empty padded_string (null data()), and it no longer stops at an embedded NUL.

The new test replaces the array forms of operator new so that fresh memory holds 0xAA, which makes it fail before the fix regardless of the allocator.

…ing by size

convert() handed over the uninitialized bytes between size and capacity
as the padding of the resulting padded_string, and operator<< printed
data() as a C string, so it could write those stale bytes. It also
crashed on a null data() and stopped at an embedded NUL.

convert() now zeroes the SIMDJSON_PADDING bytes after size, and
operator<< writes size() bytes.

Reported by @perfloop.
@lemire
lemire merged commit 7e62744 into master Dec 1, 2026
110 of 117 checks passed
@lemire
lemire deleted the padded-string-builder-convert-padding branch December 1, 2026 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant