Skip to content

Fix closing submessages with known size - #166

Merged
joto merged 1 commit into
masterfrom
kk/fix-packed-known-size
Nov 2, 2026
Merged

joto merged 1 commit into
masterfrom
kk/fix-packed-known-size

Conversation

@kkaefer

@kkaefer kkaefer commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

When a submessage was opened with a known size (e.g. packed_field_fixed32 field{pw, 1, 2}, as in the tutorial), closing it never reset the parent's m_pos. Any later write to the parent then hit the "existing submessage" assert in debug builds.

Closing now resets the parent and asserts that exactly size bytes were written. Tests cover the correct, too-few and too-many element cases, plus a plain known-size submessage.

@kkaefer
kkaefer requested a review from a team as a code owner October 14, 2026 12:53
@kkaefer
kkaefer requested a review from joto October 14, 2026 12:53
Comment thread include/protozero/basic_pbf_writer.hpp Resolved

@joto joto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code was already quite complicated with the special case of the m_rollback_pos = size_is_known, now the different meanings of where m_pos is also dependent on whether this is a fixed or variable sized submessage.

The clean solution would probably be to have a std::variant keeping those two cases apart (plus the case where we are not in a submessage at all), but that would mean moving to C+17.

Comment thread include/protozero/basic_pbf_writer.hpp Resolved
close_submessage() returned early for submessages opened with a known
size without resetting m_pos in the parent, so any further write to the
parent hit the "existing submessage" assert. This affected e.g.
packed_field_fixed32 with a size argument, as shown in the tutorial.

Reset m_pos when closing such a submessage.
@kkaefer
kkaefer force-pushed the kk/fix-packed-known-size branch from 1bced71 to a15ab8f Compare November 2, 2026 14:42
@kkaefer

kkaefer commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

Fair; I simplified this so that we are now just simply resetting m_pos to zero when a submessage with known size is closed. This means we no longer check that exactly size bytes were written, but the code on the master branch didn’t check that either. In any case, the main fix is still present.

@kkaefer
kkaefer requested a review from joto November 2, 2026 14:45
@kkaefer

kkaefer commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

I also decided against some kind of std::variant-like version for this fix because it would increase the memory size.

@joto
joto merged commit 9ecfce4 into master Nov 2, 2026
72 checks passed
@kkaefer
kkaefer deleted the kk/fix-packed-known-size branch November 3, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants