Validate SIDX reference field limits during encoding - #215
Conversation
WalkthroughThe extended Sidx body encoder now rejects SegmentReference values that exceed the bit widths of reference_size, sap_type, or sap_delta_time, returning Error::TooLarge(Sidx::KIND). SAP field packing no longer masks validated values before shifting and combining them. A unit test verifies encoding fails for each oversized field. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@CodeRabbit review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/sidx.rs (1)
117-122: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse branchless bitwise logic to simplify packing.
You can eliminate the
matchexpression to avoid potentialclippy::match_boolviolations. As per coding guidelines, code must be clippy-clean. Casting the boolean to au32and shifting it directly makes the packing branchless, mathematically equivalent, and more concise.♻️ Proposed refactor
- let sap_flag_and_type_and_delta_time = match reference.starts_with_sap { - true => { - 0x8000_0000 | ((reference.sap_type as u32) << 28) | reference.sap_delta_time - } - false => ((reference.sap_type as u32) << 28) | reference.sap_delta_time, - }; + let sap_flag_and_type_and_delta_time = ((reference.starts_with_sap as u32) << 31) + | ((reference.sap_type as u32) << 28) + | reference.sap_delta_time;(Note: You can apply the same branchless simplification to
reference_type_and_sizeon lines 111-114.)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/sidx.rs` around lines 117 - 122, Replace the boolean match used to compute sap_flag_and_type_and_delta_time with branchless bitwise packing by casting reference.starts_with_sap to u32 and shifting it into the high flag bit, preserving the existing sap_type and sap_delta_time fields. Apply the same match_bool refactor to reference_type_and_size, using its corresponding boolean flag and bit layout.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/sidx.rs`:
- Around line 117-122: Replace the boolean match used to compute
sap_flag_and_type_and_delta_time with branchless bitwise packing by casting
reference.starts_with_sap to u32 and shifting it into the high flag bit,
preserving the existing sap_type and sap_delta_time fields. Apply the same
match_bool refactor to reference_type_and_size, using its corresponding boolean
flag and bit layout.
Summary
reference_size,sap_type, andsap_delta_timevalues before encodingsidxreferences.TooLargeerror path.Testing