From 6cf18388e49eb6fd20d491cc709ef3a5d0d55fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 18 Nov 2026 13:23:59 +0100 Subject: [PATCH] Support rolling back submessages with known size Opening a submessage with a known size (e.g. packed_field_fixed32 with a size argument) stored a sentinel instead of the rollback position, so rollback() failed an assert in debug builds and threw std::length_error in release builds, leaving a partially written field in the buffer. Keep the real rollback position in m_rollback_pos and mark submessages with a known size with a flag bit instead. For these submessages, m_pos now holds the position where the data must end, so closing them can assert that exactly the promised number of bytes was written. The writer doesn't get any bigger. --- CHANGELOG.md | 2 + doc/tutorial.md | 4 +- include/protozero/basic_pbf_writer.hpp | 50 +++++++++++++------ test/t/message/reader_test_cases.cpp | 26 ++++++++++ .../reader_test_cases.cpp | 42 ++++++++++++++++ 5 files changed, 108 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22712042..076e751e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Fix `-Wsign-conversion` warning in `basic_pbf_writer::commit_submessage()`. - `pbf_reader` (and `pbf_message`) now actually provide the documented strong exception guarantee. Previously `next()` on an invalid tag or wire type, `skip()`, `get_view()`, `get_message()`, `get_packed_*()` on a truncated or invalid length-delimited field, and `next(tag)` when skipping over an invalid field would throw after having already advanced the reader. +- Calling `rollback()` on a submessage or packed field that was opened with a known size (for example `packed_field_fixed32 field{pw, 1, 2}`) failed an assert in debug builds and threw `std::length_error` in release builds, leaving a partially written field in the buffer. It now rolls back the field like for any other submessage. +- Committing a submessage or packed field that was opened with a known size now asserts in debug builds that exactly that many bytes were written. Previously writing too few or too many bytes silently produced a broken message. ## [1.8.2] - 2026-06-30 diff --git a/doc/tutorial.md b/doc/tutorial.md index 357472e3..71631ea8 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -525,7 +525,9 @@ protozero::pbf_writer pw{data}; ``` In this case you have to supply exactly as many elements as you promised, -otherwise you will get a broken protobuf message. +otherwise you will get a broken protobuf message. In debug builds an assert +checks this when the field is committed. You can still call `rollback()` on such +a field. This works for `packed_field_fixed32`, `packed_field_sfixed32`, `packed_field_fixed64`, `packed_field_sfixed64`, `packed_field_float`, and diff --git a/include/protozero/basic_pbf_writer.hpp b/include/protozero/basic_pbf_writer.hpp index 836c4bb9..46dac3fd 100644 --- a/include/protozero/basic_pbf_writer.hpp +++ b/include/protozero/basic_pbf_writer.hpp @@ -70,11 +70,16 @@ class basic_pbf_writer { // This is usually 0. If there is an open submessage, this is set in the // parent to the rollback position, ie. the last position before the // submessage was started. This is the position where the header of the - // submessage starts. + // submessage starts. If the size of the submessage was known when it was + // opened, the known_size_flag bit is set in addition. std::size_t m_rollback_pos = 0; // This is usually 0. If there is an open submessage, this is set in the // parent to the position where the data of the submessage is written to. + // If the size of the submessage was known when it was opened, the length + // has already been written and this position isn't needed any more, so + // this is set to the position where the data of the submessage must end + // instead. std::size_t m_pos = 0; void add_varint(uint64_t value) { @@ -165,39 +170,44 @@ class basic_pbf_writer { reserve_bytes = (sizeof(pbf_length_type) * 8 / 7) + 1 }; - // If m_rollpack_pos is set to this special value, it means that when - // the submessage is closed, only m_pos needs to be reset, because the - // length of the submessage has already been written correctly. - enum : std::size_t { - size_is_known = std::numeric_limits::max() - }; + // This bit is set in m_rollback_pos if the size of the open submessage + // was known when it was opened. Buffer positions never get this large. + static constexpr std::size_t known_size_flag = std::size_t(1) << (std::numeric_limits::digits - 1); + + bool submessage_has_known_size() const noexcept { + return (m_rollback_pos & known_size_flag) != 0; + } void open_submessage(pbf_tag_type tag, std::size_t size) { protozero_assert(m_pos == 0); protozero_assert(m_data); + const auto rollback_pos = buffer_customization::size(m_data); + protozero_assert((rollback_pos & known_size_flag) == 0); if (size == 0) { - m_rollback_pos = buffer_customization::size(m_data); add_field(tag, pbf_wire_type::length_delimited); buffer_customization::append_zeros(m_data, std::size_t(reserve_bytes)); + m_rollback_pos = rollback_pos; + m_pos = buffer_customization::size(m_data); } else { - m_rollback_pos = size_is_known; + protozero_assert(size <= std::numeric_limits::max()); add_length_varint(tag, static_cast(size)); reserve(size); + m_rollback_pos = rollback_pos | known_size_flag; + m_pos = buffer_customization::size(m_data) + size; } - m_pos = buffer_customization::size(m_data); } void rollback_submessage() { protozero_assert(m_pos != 0); - protozero_assert(m_rollback_pos != size_is_known); protozero_assert(m_data); - buffer_customization::resize(m_data, m_rollback_pos); + buffer_customization::resize(m_data, m_rollback_pos & ~known_size_flag); + m_rollback_pos = 0; m_pos = 0; } void commit_submessage() { protozero_assert(m_pos != 0); - protozero_assert(m_rollback_pos != size_is_known); + protozero_assert(!submessage_has_known_size()); protozero_assert(m_data); const auto length = pbf_length_type(buffer_customization::size(m_data) - m_pos); @@ -210,7 +220,14 @@ class basic_pbf_writer { void close_submessage() { protozero_assert(m_data); - if (m_pos == 0 || m_rollback_pos == size_is_known) { + if (m_pos == 0) { + return; + } + if (submessage_has_known_size()) { + // The length was already written when the submessage was opened, + // m_pos is the position where the data must end. + protozero_assert(buffer_customization::size(m_data) == m_pos && "number of bytes written to submessage doesn't match the size given when it was opened"); + m_rollback_pos = 0; m_pos = 0; return; } @@ -252,7 +269,10 @@ class basic_pbf_writer { * @param tag Tag (field number) of the field that will be written * @param size Optional size of the submessage in bytes (use 0 for unknown). * Setting this allows some optimizations but is only possible in - * a few very specific cases. + * a few very specific cases. If you set it, you must write + * exactly this many bytes to the submessage before committing + * it, otherwise the resulting message is broken. Debug builds + * check this with an assert. */ basic_pbf_writer(basic_pbf_writer& parent_writer, pbf_tag_type tag, std::size_t size = 0) : m_data{parent_writer.m_data}, diff --git a/test/t/message/reader_test_cases.cpp b/test/t/message/reader_test_cases.cpp index 97426efd..247b7bb1 100644 --- a/test/t/message/reader_test_cases.cpp +++ b/test/t/message/reader_test_cases.cpp @@ -92,6 +92,32 @@ TEST_CASE("write message field with reserved size followed by another field") { REQUIRE(buffer == std::string{"\x0a\x02\x08\x05\x10\x05", 6}); } +TEST_CASE("rollback message field with reserved size") { + std::string buffer; + protozero::pbf_writer pbf_test{buffer}; + pbf_test.add_uint32(2, 5); + + { + protozero::pbf_writer pbf_submessage{pbf_test, 1, 2}; + pbf_submessage.add_uint32(1, 5); + pbf_submessage.rollback(); + } + pbf_test.add_uint32(2, 5); + + REQUIRE(buffer == std::string{"\x10\x05\x10\x05", 4}); +} + +TEST_CASE("commit message field with reserved size but wrong length asserts") { + std::string buffer; + protozero::pbf_writer pbf_test{buffer}; + + protozero::pbf_writer pbf_submessage{pbf_test, 1, 3}; + pbf_submessage.add_uint32(1, 5); + REQUIRE_THROWS_AS(pbf_submessage.commit(), assert_error); + pbf_submessage.rollback(); + REQUIRE(buffer.empty()); +} + TEST_CASE("write message field into non-empty buffer") { std::string buffer{"some data already in here"}; protozero::pbf_writer pbf_test{buffer}; diff --git a/test/t/repeated_packed_fixed32/reader_test_cases.cpp b/test/t/repeated_packed_fixed32/reader_test_cases.cpp index 5927bb93..5f7528da 100644 --- a/test/t/repeated_packed_fixed32/reader_test_cases.cpp +++ b/test/t/repeated_packed_fixed32/reader_test_cases.cpp @@ -65,4 +65,46 @@ TEST_CASE("write repeated packed fixed32 field using packed field with known siz // field 3, length 10, containing the packed field REQUIRE(buffer == std::string{"\x1a\x0a", 2} + packed); } + + SECTION("rollback before adding elements") { + pw.add_uint32(2, 5); + { + protozero::packed_field_fixed32 field{pw, 1, 2}; + field.rollback(); + } + pw.add_uint32(2, 5); + + REQUIRE(buffer == scalar + scalar); + } + + SECTION("rollback after adding elements") { + pw.add_uint32(2, 5); + { + protozero::packed_field_fixed32 field{pw, 1, 2}; + field.add_element(17); + field.add_element(1); + field.rollback(); + } + pw.add_uint32(2, 5); + + REQUIRE(buffer == scalar + scalar); + } + + SECTION("commit() with fewer elements than promised asserts") { + protozero::packed_field_fixed32 field{pw, 1, 2}; + field.add_element(17); + REQUIRE_THROWS_AS(field.commit(), assert_error); + field.rollback(); + REQUIRE(buffer.empty()); + } + + SECTION("commit() with more elements than promised asserts") { + protozero::packed_field_fixed32 field{pw, 1, 2}; + field.add_element(17); + field.add_element(1); + field.add_element(2); + REQUIRE_THROWS_AS(field.commit(), assert_error); + field.rollback(); + REQUIRE(buffer.empty()); + } }