Skip to content

Store enums wider than int at full width instead of truncating#354

Open
gennaroprota wants to merge 1 commit into
developfrom
fix/enum-serialization-preserves-wide-values
Open

Store enums wider than int at full width instead of truncating#354
gennaroprota wants to merge 1 commit into
developfrom
fix/enum-serialization-preserves-wide-values

Conversation

@gennaroprota

Copy link
Copy Markdown
Collaborator

Enumerator serialization cast every value to int on save and back on load. For an enum whose underlying type does not fit in an int, for example a scoped enum on long long with a value above INT_MAX, that cast silently truncated the value, and users could not intervene because the archive routes every enum through this path before any user serialization is consulted.

An enum is now saved through an integer sized to its underlying type: int when the underlying type is no wider than int, otherwise the underlying type itself. The common case (including scoped enums that do not name a type, and narrow underlying types such as char, which stay on the int path to avoid the archives' character handling) is byte for byte identical to before, so existing archives and data are unaffected. Only enums that genuinely need the extra width change on the wire, and those were being corrupted anyway.

The archive library version is bumped to 21.

Fixes issue #353.

@gennaroprota
gennaroprota force-pushed the fix/enum-serialization-preserves-wide-values branch from 68d9b1e to 58e3c6e Compare July 22, 2026 06:46
Enumerator serialization cast every value to `int` on save and back on
load. For an `enum` whose underlying type does not fit in an `int`, for
example a scoped enum on long long with a value above `INT_MAX`, that
cast silently truncated the value, and users could not intervene because
the archive routes every enum through this path before any user
serialization is consulted.

An enum is now saved through an integer sized to its underlying type:
int when the underlying type is no wider than int, otherwise the
underlying type itself. The common case (including scoped enums that do
not name a type, and narrow underlying types such as char, which stay on
the int path to avoid the archives' character handling) is byte for byte
identical to before, so existing archives and data are unaffected. Only
enums that genuinely need the extra width change on the wire, and those
were being corrupted anyway.

The archive library version is bumped to 21.

Fixes #353.
@gennaroprota
gennaroprota force-pushed the fix/enum-serialization-preserves-wide-values branch from 58e3c6e to ac1489c Compare July 23, 2026 06:06
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.

1 participant