Skip to content

decode: optionally clear the destination when a merge fails - #833

Open
vanderbr wants to merge 1 commit into
anthropics:mainfrom
reallyme:reallyme/clear-on-merge-error
Open

vanderbr wants to merge 1 commit into
anthropics:mainfrom
reallyme:reallyme/clear-on-merge-error

Conversation

@vanderbr

@vanderbr vanderbr commented Nov 14, 2026 •

Copy link
Copy Markdown

Failed merges can leave old fields and partially decoded values in the destination. Callers that must discard that state currently need to clear it on every error path.

Add an opt-in policy shared by DecodeOptions::merge, DecodeOptions::merge_from_slice, and DynamicMessage::merge_with_options, including errors from size rejection before parsing, recursion, unknown-field and element-memory limits. Typed merges call Message::clear exactly once after an error. Reflective merges remove all fields, registered extensions and unknown fields while retaining the descriptor pool and message type. Default and successful merge behavior is unchanged.

clear_on_merge_error() enables the policy, with_clear_on_merge_error(bool) enables or disables it on an existing or cloned configuration, and clears_on_merge_error() reports its state. The slice wrapper delegates to the buffer wrapper so they share the same failure policy.

Clearing removes logical state; wiping depends on the typed message's own clear implementation. Reflective cleanup does not wipe memory, and backing storage may be retained. Caller input is not wiped and consumed buffer positions are not rewound. Typed decode methods do not apply this policy. Reflective decode_with_options delegates to merge and applies it to its temporary destination on error; it still returns no message and adds no wiping guarantee.

Validation: 4,349 workspace tests, strict all-target/all-feature Clippy, formatting and strict rustdoc pass on the pinned toolchain. Focused Rust 1.75 tests pass using a separate compatible validation lockfile; runtime/reflection host and WASM no_std checks also pass. Regressions cover nested messages, lists/maps, late map normalization, oneofs, registered extensions/groups, MessageSet, limits, cleanup-hook dispatch, clone configuration, fragmented-buffer consumption, empty input and reuse. A separate bounded probe passed 394,758 enabled/disabled comparisons across unrolled, table and reflective decoding, checking errors, successful values and typed buffer cursors.

Release the descriptor changes with a runtime containing the new getter and raise the runtime dependency minimum as part of that coordinated release. Versions, lockfile, MSRV and toolchain preparation remain separate from this feature change.

Related to #822.

@github-actions

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Apply opt-in cleanup consistently to typed and reflective merges, including size and resource-limit failures. Preserve the original error and normal partial-merge behavior when disabled.

Add a reversible builder alongside the existing shortcut and getter. Cover custom cleanup hooks, buffer consumption, nested containers, oneofs, MessageSet and registered extensions, and document logical cleanup without memory-wiping or rewind guarantees.
@vanderbr
vanderbr force-pushed the reallyme/clear-on-merge-error branch from 29641c9 to 4aedbad Compare November 18, 2026 02:46

This branch has not been deployed

No deployments
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