Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions src/ostream.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,32 +349,42 @@ 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<uint8_t>::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)
/* No sequence is open yet, so nothing is held back. The pending[] slots
* 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)
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions test/c/test_ostream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>::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)
Expand All @@ -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);
Expand Down
Loading