Skip to content

[Rust] Reset apparent_size in Verifier::reset() - #9209

Open
xhon-pelushi wants to merge 1 commit into
google:masterfrom
xhon-pelushi:fix/9189-verifier-reset
Open

[Rust] Reset apparent_size in Verifier::reset()#9209
xhon-pelushi wants to merge 1 commit into
google:masterfrom
xhon-pelushi:fix/9189-verifier-reset

Conversation

@xhon-pelushi

Copy link
Copy Markdown

Fixes #9189.

Verifier::reset() assigns num_tables twice and never clears apparent_size:

pub fn reset(&mut self) {
    self.depth = 0;
    self.num_tables = 0;
    self.num_tables = 0;      // apparent_size was meant here
}

apparent_size is initialised in new() and incremented in range_in_buffer(), but cleared nowhere. A Verifier reused through reset() therefore keeps accumulating across buffers, and once the running total passes max_apparent_size it rejects every subsequent buffer with ApparentSizeTooLarge, however small or valid that buffer is. In other words reset() leaves the verifier in a state new() can never produce, which is the opposite of what it is for.

Fix

Assign apparent_size in place of the duplicated num_tables line, plus a test that spends most of the budget, resets, and spends it again. The test is inline as #[cfg(test)] mod tests, matching builder.rs.

Against master it fails as:

assertion `left == right` failed: reset() must clear apparent_size, not just depth and num_tables
  left: Err(ApparentSizeTooLarge)
 right: Ok(())

Testing

  • cargo test -p flatbuffers — 7 passed after the change (6 before, plus the new one).
  • tests/rust_usage_test — full suite green: 276 + 21 + 13 and the smaller suites, 0 failures.

One thing to be aware of, unrelated to this change: builder::tests::with_internal_capacity_preallocates_vecs already fails on master with "expected 0 allocations after builder construction, got 2". I confirmed that by stashing this patch and re-running, so please don't attribute it to this PR.

reset() assigns num_tables twice and never clears apparent_size, so a Verifier
reused through reset() keeps the running total from previous buffers. Once
that total passes max_apparent_size, every later buffer is rejected with
ApparentSizeTooLarge no matter how small or how valid it is - reset() puts the
verifier into a state new() never produces.

Assign apparent_size in place of the duplicated num_tables line, and add a
test that spends most of the budget, resets, and spends it again.

Note: builder::tests::with_internal_capacity_preallocates_vecs already fails
on master ("expected 0 allocations after builder construction, got 2"),
independently of this change.
注册 for free to join this conversation on GitHub. Already have an account? 登录 to comment

标签

项目

None yet

Development

Successfully merging this pull request may close these issues.

Rust: Verifier::reset() does not reset apparent_size (duplicated num_tables assignment)

1 participant