fix(common): return error instead of panicking on truncated record bytes#362
fix(common): return error instead of panicking on truncated record bytes#362
Conversation
Replace `get_uint` with `try_get_uint` when parsing metered size in `TryFrom<Bytes> for Metered<Record>`, consistent with the rest of the deserialization code. Closes #355 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Greptile SummaryThis PR fixes a potential panic in
Confidence Score: 5/5Safe to merge — the fix is minimal, correct, and consistent with existing patterns; the only remaining feedback is a P2 suggestion to add a regression test. The change is a one-line targeted replacement of a known panic path with the safe No files require special attention; the single changed file (common/src/record/mod.rs) is straightforward. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["TryFrom<Bytes> for Metered<Record>"] --> B{buf is empty?}
B -- yes --> C["Err(Truncated('MagicByte'))"]
B -- no --> D["buf.get_u8() → MagicByte::try_from(...)"]
D -- invalid --> E["Err(InvalidValue('MagicByte', msg))"]
D -- ok --> F["buf.try_get_uint(metered_size_varlen)"]
F -- "Err (insufficient bytes) — BEFORE: panic / AFTER: error" --> G["Err(Truncated('MeteredSize'))"]
F -- ok --> H{record_type?}
H -- Command --> I["CommandRecord::try_from(buf)"]
H -- Envelope --> J["EnvelopeRecord::try_from(buf)"]
I --> K["Ok(Metered<Record>)"]
J --> K
Prompt To Fix All With AIThis is a comment left during a code review.
Path: common/src/record/mod.rs
Line: 316-318
Comment:
**Missing test for the fixed panic path**
The PR test plan calls out "Confirm truncated input (valid magic byte but missing metered size bytes) returns `Err` instead of panicking", but no corresponding unit test is added to the test module. `envelope.rs` has a dedicated `truncated_returns_error` test that verifies every truncation point returns `Err`; the same pattern would be valuable here to guard against regressions.
Consider adding something like:
```rust
#[test]
fn metered_record_truncated_after_magic_byte_returns_error() {
// Magic byte: Envelope (0b0000_0010), metered_size_varlen = 1 → expects 1 more byte.
let truncated = Bytes::from_static(&[0b0000_0010]);
assert_eq!(
Metered::<Record>::try_from(truncated),
Err(InternalRecordError::Truncated("MeteredSize"))
);
}
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(common): return error instead of pan..." | Re-trigger Greptile |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
get_uintwithtry_get_uintwhen parsing metered size inTryFrom<Bytes> for Metered<Record>, returningInternalRecordError::Truncated("MeteredSize")instead of panicking.envelope.rsanddecode_if_command_record.Closes #355
Test plan
cargo checkpassesErrinstead of panicking🤖 Generated with Claude Code