Skip to content

fix: a zero-length output buffer may be handed over as NULL - #195

Merged
Andste82 merged 1 commit into
mainfrom
fix/zero-length-buffer-may-be-null
Oct 12, 2026
Merged

Andste82 merged 1 commit into
mainfrom
fix/zero-length-buffer-may-be-null

Conversation

@Andste82

Copy link
Copy Markdown
Member

Part of the corelib half of sofa-buffers/generator#738 — an empty message is valid and has to work.

The bug

sofab_ostream_init() and sofab_ostream_buffer_set() asserted buffer != NULL unconditionally, three lines above a comment that says the opposite:

assert(buffer != NULL);
/* ... That case has to stay exact: the all-default message is the empty byte
 * string (MESSAGE_SPEC §2), so a zero-length buffer is a legitimate
 * installation ... */

test_zero_room_without_a_sink_is_legal already pins that rule — but it installs a real array at length 0, so the one way a caller actually produces a zero-length buffer was never tested: a (pointer, length) pair whose pointer is null.

That is exactly what std::vector<uint8_t>::data() returns for an empty vector, and the generated C++ one-shot encode of a field-less message installs exactly that. MESSAGE_SPEC §2 makes the all-default message the empty byte string, so a message with no fields has MAX_SIZE == 0:

emptydrv: src/ostream.c:357: sofab_ostream_init: Assertion `buffer != NULL' failed.

corelib-cpp accepts the same installation (its initBuffer only rejects offset > buflen), and the C backend never trips it because a caller's array decays to a non-null pointer even at length 0 — so this is the one C++ profile where a legal schema aborts.

The fix

  • Both install functions now state the precondition the way every other (pointer, length) pair in this API already states it — len == 0 || ptr != NULL. There are five such places today (istream.c:538, ostream.c:441, and the three array writers); these two were the outliers.
  • A null buffer is normalized to a one-byte file-scope sentinel, so no pointer arithmetic in this file is ever performed on a null pointer — undefined before C23, and -fsanitize=undefined reports it. With buflen == 0 the cursor starts at the end of the buffer, so the sentinel can never be read or written and streams may share it.
  • sofab_ostream_init() now delegates the buffer half to sofab_ostream_buffer_set() instead of repeating it: one implementation of the install rules, with the sink in place before the §5.1 minimum is checked against it. That also halves the cost of the new branch.

Measured

arm-none-eabi-gcc -Os -mcpu=cortex-m0 -DNDEBUG, ostream.o, -ffunction-sections -fdata-sections:

.text .bss
before 962 0
after 972 (+10 B) 1

The alternative — relax the assertions and leave NULL + 0 in place — costs nothing but keeps undefined behaviour on paper. The 11 bytes buy a library its users can build under -fsanitize=undefined; say the word if the footprint profile would rather have the bytes.

Tests

test_zero_length_buffer_may_be_null, the NULL twin of the zero-room test: a null install reports 0 bytes used, flushes nothing, answers a write with SOFAB_RET_E_BUFFER_FULL (not a precondition violation) and moves nothing; then the same through sofab_ostream_buffer_set() in both directions — a real buffer replaced by no buffer and back, after which writing works again. Runs under the ctest build, i.e. with assert() live.

Whole C suite green under -fsanitize=address,undefined (302 assertions, 0 failures). End-to-end: the generated C++ encode() of a field-less message returns Error::None with no bytes against this branch, where it aborted against main.

sofab_ostream_init() and sofab_ostream_buffer_set() asserted
`buffer != NULL` unconditionally -- three lines above a comment stating the
opposite: "a zero-length buffer is a legitimate installation". §5.1 agrees ("a
buffer installed without a sink is subject to no minimum"), and
test_zero_room_without_a_sink_is_legal already pins the case -- but it hands
over a real array, so the one way a caller actually PRODUCES a zero-length
buffer went untested: a (pointer, length) pair whose pointer is null.

That is what std::vector<uint8_t>::data() returns for an empty vector, and the
generated C++ one-shot encode of a FIELD-LESS message installs exactly that
(sofa-buffers/generator#738). MESSAGE_SPEC §2 makes the all-default message the
empty byte string, so a message with no fields has MAX_SIZE == 0, and encode()
aborted in this assert instead of returning no bytes.

Both install functions now state the precondition the way every other
(pointer, length) pair in this API states it -- `len == 0 || ptr != NULL` (five
places: istream.c:538, ostream.c:441, and the three array writers) -- and
normalize a null buffer to a one-byte file-scope sentinel, so no pointer
arithmetic here is ever performed on a null pointer (undefined before C23, and
-fsanitize=undefined reports it). With buflen == 0 the cursor starts at the end
of the buffer, so the sentinel can never be read or written and streams may
share it.

sofab_ostream_init() now delegates the buffer half to
sofab_ostream_buffer_set() rather than repeating it: one implementation of the
install rules, and the sink is in place before the §5.1 minimum is checked
against it. That halves the cost of the new branch.

Measured (arm-none-eabi-gcc -Os -mcpu=cortex-m0 -DNDEBUG, ostream.o,
-ffunction-sections -fdata-sections): .text 962 -> 972 (+10 B), .bss 0 -> 1.
The alternative -- relaxing the assertions and leaving NULL + 0 in place --
costs nothing but keeps undefined behaviour on paper; the 11 bytes buy a
library its users can build with -fsanitize=undefined.

Tests: the NULL twin of the zero-room test, in both install functions and in
both directions through buffer_set. Whole C suite green under
-fsanitize=address,undefined.
@Andste82
Andste82 merged commit c7961a7 into main Oct 12, 2026
26 checks passed
@Andste82
Andste82 deleted the fix/zero-length-buffer-may-be-null branch October 12, 2026 17:52
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.

1 participant