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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- `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.
- When opening a submessage failed because the buffer couldn't grow (for example a full `fixed_size_buffer_adaptor`), a partially written field key was left in the parent. The parent is now unchanged in that case.

## [1.8.2] - 2026-06-30

Expand Down
24 changes: 24 additions & 0 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,30 @@ The result is the same as if the lines inside the nested brackets had never
been called. Do not try to call any of the `add_*` functions on the submessage
after a rollback.

When a submessage writer is destructed, it always commits the submessage, even
if the destructor runs because an exception is leaving its scope. If you catch
the exception and keep using the parent writer, it would contain a half-written
submessage. To avoid this, catch the exception and roll back the submessage
while it is still in scope:

```cpp
std::string data;
protozero::pbf_writer pbf_parent{data};

for (const auto& feature : features) {
protozero::pbf_writer pbf_sub{pbf_parent, 1};
try {
encode(pbf_sub, feature); // might throw
} catch (const std::exception&) {
// skip this feature, pbf_parent can still be used
pbf_sub.rollback();
}
}
```

If opening the submessage itself throws (e.g. because a
`fixed_size_buffer_adaptor` is full), the parent writer is left unchanged.

## Writing protobuf-encoded messages using `pbf_builder`

Just like the `pbf_message` template class wraps the `pbf_reader` class, there
Expand Down
31 changes: 20 additions & 11 deletions include/protozero/basic_pbf_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,26 @@ class basic_pbf_writer {
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) {
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 {
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;
try {
if (size == 0) {
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 {
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;
}
} catch (...) {
// Remove the partially written header, so that this writer is
// unchanged.
if (buffer_customization<TBuffer>::size(m_data) > rollback_pos) {
buffer_customization<TBuffer>::resize(m_data, rollback_pos);
}
throw;
}
}

Expand Down
1 change: 1 addition & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(UNIT_TESTS data_view
exceptions
iterators
varint
writer_exceptions
zigzag)

string(REGEX REPLACE "([^;]+)" "test_\\1.cpp" _test_sources "${UNIT_TESTS}")
Expand Down
64 changes: 64 additions & 0 deletions test/unit/test_writer_exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <test.hpp>

#include <protozero/buffer_fixed.hpp>

#include <array>
#include <stdexcept>
#include <string>

namespace {

std::string to_string(const protozero::fixed_size_buffer_adaptor& buffer) {
return std::string{buffer.data(), buffer.size()};
}

using fixed_writer = protozero::basic_pbf_writer<protozero::fixed_size_buffer_adaptor>;

} // anonymous namespace

TEST_CASE("full fixed size buffer while opening submessage leaves parent unchanged") {
std::array<char, 4> data = {{0}};
protozero::fixed_size_buffer_adaptor adaptor{data.data(), data.size()};
fixed_writer pw{adaptor};
pw.add_uint32(1, 17);

// The field key fits, but the space reserved for the length does not.
REQUIRE_THROWS_AS(fixed_writer(pw, 2), std::length_error);
REQUIRE(adaptor.size() == 2);

// The parent can still be used.
pw.add_uint32(3, 18);
REQUIRE(to_string(adaptor) == std::string{"\x08\x11\x18\x12", 4});
}

TEST_CASE("full fixed size buffer while opening submessage with known size leaves parent unchanged") {
std::array<char, 4> data = {{0}};
protozero::fixed_size_buffer_adaptor adaptor{data.data(), data.size()};
fixed_writer pw{adaptor};
pw.add_uint32(1, 17);

// The field key and the first byte of the two-byte length fit, but the
// second byte of the length does not.
REQUIRE_THROWS_AS(fixed_writer(pw, 2, 200), std::length_error);
REQUIRE(adaptor.size() == 2);

pw.add_uint32(3, 18);
REQUIRE(to_string(adaptor) == std::string{"\x08\x11\x18\x12", 4});
}

TEST_CASE("submessage can be rolled back after an exception while writing it") {
std::array<char, 10> data = {{0}};
protozero::fixed_size_buffer_adaptor adaptor{data.data(), data.size()};
fixed_writer pw{adaptor};
pw.add_uint32(1, 17);

{
fixed_writer sub{pw, 2};
REQUIRE_THROWS_AS(sub.add_string(1, "too long for the buffer"), std::length_error);
sub.rollback();
}
REQUIRE(adaptor.size() == 2);

pw.add_uint32(3, 18);
REQUIRE(to_string(adaptor) == std::string{"\x08\x11\x18\x12", 4});
}
Loading