Repository navigation
fix: a zero-length output buffer may be handed over as NULL - #195
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the corelib half of sofa-buffers/generator#738 — an empty message is valid and has to work.
The bug
sofab_ostream_init()andsofab_ostream_buffer_set()assertedbuffer != NULLunconditionally, three lines above a comment that says the opposite:test_zero_room_without_a_sink_is_legalalready 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 hasMAX_SIZE == 0:corelib-cppaccepts the same installation (itsinitBufferonly rejectsoffset > 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
(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.-fsanitize=undefinedreports it. Withbuflen == 0the 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 tosofab_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:The alternative — relax the assertions and leave
NULL + 0in 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 withSOFAB_RET_E_BUFFER_FULL(not a precondition violation) and moves nothing; then the same throughsofab_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. withassert()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 returnsError::Nonewith no bytes against this branch, where it aborted againstmain.