Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 35 additions & 15 deletions include/protozero/basic_pbf_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<std::size_t>::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<std::size_t>::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<TBuffer>::size(m_data);
protozero_assert((rollback_pos & known_size_flag) == 0);
if (size == 0) {
m_rollback_pos = buffer_customization<TBuffer>::size(m_data);
add_field(tag, pbf_wire_type::length_delimited);
buffer_customization<TBuffer>::append_zeros(m_data, std::size_t(reserve_bytes));
m_rollback_pos = rollback_pos;
m_pos = buffer_customization<TBuffer>::size(m_data);
} else {
m_rollback_pos = size_is_known;
protozero_assert(size <= std::numeric_limits<pbf_length_type>::max());
add_length_varint(tag, static_cast<pbf_length_type>(size));
reserve(size);
m_rollback_pos = rollback_pos | known_size_flag;
m_pos = buffer_customization<TBuffer>::size(m_data) + size;
}
m_pos = buffer_customization<TBuffer>::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<TBuffer>::resize(m_data, m_rollback_pos);
buffer_customization<TBuffer>::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<TBuffer>::size(m_data) - m_pos);

Expand All @@ -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<TBuffer>::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;
}
Expand Down Expand Up @@ -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},
Expand Down
26 changes: 26 additions & 0 deletions test/t/message/reader_test_cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
42 changes: 42 additions & 0 deletions test/t/repeated_packed_fixed32/reader_test_cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading