From 54e2538d9a8c8f1659826e1c88903a0f4ebe7851 Mon Sep 17 00:00:00 2001 From: Andreas Steinbart Date: Mon, 12 Oct 2026 16:45:27 +0000 Subject: [PATCH] fix: a zero-length output buffer may be handed over as NULL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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::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. --- src/ostream.c | 52 ++++++++++++++++++++++++++++++------------- test/c/test_ostream.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 16 deletions(-) diff --git a/src/ostream.c b/src/ostream.c index aaa9fd9..a77a0ef 100644 --- a/src/ostream.c +++ b/src/ostream.c @@ -349,25 +349,29 @@ static sofab_ret_t _write_fixlen_reverse (sofab_ostream_t *ctx, const uint8_t *d // +/* Stand-in for a zero-length buffer handed over as NULL. + * + * A (pointer, length) pair of (NULL, 0) is what std::vector::data() + * yields for an empty vector, and the generated C++ one-shot encode of a + * FIELD-LESS message installs exactly that (generator#738): MESSAGE_SPEC §2 + * makes the all-default message the empty byte string, so a zero-length buffer + * is the right size for it, and §5.1 already admits one ("a buffer installed + * without a sink is subject to no minimum"). The two install functions below + * therefore accept a null pointer at length 0 -- the same precondition every + * other (pointer, length) pair in this API states -- and point the cursor here + * rather than at NULL, 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 this + * byte can never be read or written and every stream may share it. + */ +static uint8_t _empty_buffer; + extern void sofab_ostream_init ( sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset, sofab_ostream_flush_cb_t flush, void *usrptr) { assert(ctx != NULL); - assert(buffer != NULL); - /* CORELIB_PLAN §5.1 binds SOFAB_MIN_OUTPUT_BUFFER to a buffer installed - * *with* a sink, "and on no other" (§13). A buffer without one can be - * arbitrarily small -- no flush can occur, so nothing can be split, and it - * either holds the message or reports SOFAB_RET_E_BUFFER_FULL. 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 - * and offset == buflen is a legitimate (immediately full) cursor. */ - assert(offset <= buflen); - assert(flush == NULL || buflen - offset >= SOFAB_MIN_OUTPUT_BUFFER); - ctx->buffer = buffer; - ctx->offset = buffer + offset; - ctx->bufend = buffer + buflen; ctx->flush = flush; ctx->usrptr = usrptr; #if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) && !defined(SOFAB_DISABLE_LAZY_SEQ_SUPPORT) @@ -375,6 +379,12 @@ extern void sofab_ostream_init ( * themselves stay untouched -- npending bounds every read of them. */ ctx->npending = 0; #endif /* SEQUENCE && LAZY_SEQ */ + + /* The buffer half IS the mid-stream installation, preconditions included: + * the sink is already in place above, so the §5.1 minimum is checked + * against the one this stream will actually drain through. One + * implementation of the install rules, not two. */ + sofab_ostream_buffer_set(ctx, buffer, buflen, offset); } extern size_t sofab_ostream_flush (sofab_ostream_t *ctx) @@ -403,12 +413,22 @@ extern void sofab_ostream_buffer_set ( sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset) { assert(ctx != NULL); - assert(buffer != NULL); - /* Same split as in sofab_ostream_init(): the minimum binds this - * installation only if the stream drains through a sink (§5.1, §13). */ + assert(buflen == 0 || buffer != NULL); /* see _empty_buffer above */ + /* CORELIB_PLAN §5.1 binds SOFAB_MIN_OUTPUT_BUFFER to a buffer installed + * *with* a sink, "and on no other" (§13). A buffer without one can be + * arbitrarily small -- no flush can occur, so nothing can be split, and it + * either holds the message or reports SOFAB_RET_E_BUFFER_FULL. 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 + * and offset == buflen is a legitimate (immediately full) cursor. */ assert(offset <= buflen); assert(ctx->flush == NULL || buflen - offset >= SOFAB_MIN_OUTPUT_BUFFER); + if (buffer == NULL) + { + buffer = &_empty_buffer; + } + ctx->buffer = buffer; ctx->offset = buffer + offset; ctx->bufend = buffer + buflen; diff --git a/test/c/test_ostream.c b/test/c/test_ostream.c index a436127..7fac899 100644 --- a/test/c/test_ostream.c +++ b/test/c/test_ostream.c @@ -2038,6 +2038,51 @@ static void test_zero_room_without_a_sink_is_legal (void) TEST_ASSERT_EQUAL_size_t_MESSAGE(sizeof(buf), sofab_ostream_bytes_used(&ctx), "the cursor stays where it was installed"); } + +/* + * The NULL twin of the test above, and the one case the preconditions still + * forbade (generator#738). A zero-length buffer is a legitimate installation, + * and the way a caller PRODUCES one is a (pointer, length) pair whose pointer + * is null: std::vector::data() of an empty vector is NULL, and the + * generated C++ one-shot encode of a field-less message installs exactly that. + * Every other (pointer, length) pair in this API states its precondition as + * "len == 0 || ptr != NULL"; these two install functions now do too. + * + * Runs under the ctest build, i.e. with assert() live: it is the debug + * preconditions that have to admit the case. + */ +static void test_zero_length_buffer_may_be_null (void) +{ + sofab_ostream_t ctx; + uint8_t buf[2]; + + sofab_ostream_init(&ctx, NULL, 0, 0, NULL, NULL); + TEST_ASSERT_EQUAL_size_t_MESSAGE(0, sofab_ostream_bytes_used(&ctx), + "the empty message occupies no bytes"); + TEST_ASSERT_EQUAL_size_t_MESSAGE(0, sofab_ostream_flush(&ctx), + "nothing to flush"); + TEST_ASSERT_EQUAL_MESSAGE(SOFAB_RET_E_BUFFER_FULL, + sofab_ostream_write_unsigned(&ctx, 1, 42), + "a write into no buffer is buffer-full, not a precondition violation"); + TEST_ASSERT_EQUAL_size_t_MESSAGE(0, sofab_ostream_bytes_used(&ctx), + "the refused write moved nothing"); + + /* The same through a mid-stream buffer-set, in both directions: a real + * buffer may be replaced by no buffer and the other way round. */ + sofab_ostream_init(&ctx, buf, sizeof(buf), 0, NULL, NULL); + sofab_ostream_buffer_set(&ctx, NULL, 0, 0); + TEST_ASSERT_EQUAL_size_t_MESSAGE(0, sofab_ostream_bytes_used(&ctx), + "installing no buffer resets the cursor to it"); + TEST_ASSERT_EQUAL_MESSAGE(SOFAB_RET_E_BUFFER_FULL, + sofab_ostream_write_unsigned(&ctx, 1, 42), + "still buffer-full after the set"); + sofab_ostream_buffer_set(&ctx, buf, sizeof(buf), 0); + TEST_ASSERT_EQUAL_MESSAGE(SOFAB_RET_OK, + sofab_ostream_write_unsigned(&ctx, 1, 42), + "a real buffer installed after it writes again"); + TEST_ASSERT_EQUAL_size_t_MESSAGE(2, sofab_ostream_bytes_used(&ctx), + "id byte + value byte"); +} #endif /* !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) */ int test_ostream_main (void) @@ -2054,6 +2099,7 @@ int test_ostream_main (void) RUN_TEST(test_min_output_buffer_matches_one_shot); RUN_TEST(test_no_minimum_without_a_sink); RUN_TEST(test_zero_room_without_a_sink_is_legal); + RUN_TEST(test_zero_length_buffer_may_be_null); #endif RUN_TEST(test_buffer_overflow_by_id_via_unsigned);