Safely read from slices in wrap_frame#1324
Conversation
WalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
crates/media-info/src/lib.rs (3)
160-172: LGTM: Correct bounds-checked channel reduction for packed data.The logic correctly reduces channels by:
- Calculating the output position based on
out_channels(line 162)- Copying only the first
out_channelsworth of samples from each input chunk (line 164)- Using bounds-checked operations to prevent panics (lines 166-171)
The mathematics is sound: each output chunk of
sample_size * out_channelsbytes fits within the allocated frame buffer.Optional: Consider logging when bounds checks fail.
If the
get()/get_mut()calls returnNone, the copy is silently skipped. While this prevents panics, it could hide calculation errors during development. Consider adding a debug assertion or warning log:if let (Some(chunk_slice), Some(frame_slice)) = ( packed_chunk.get(0..copy_len), frame.data_mut(0).get_mut(start..start + copy_len), ) { frame_slice.copy_from_slice(chunk_slice); + } else { + debug_assert!(false, "Bounds check failed in packed channel reduction"); }
173-188: LGTM: Correct bounds-checked de-interleaving for planar data.The planar data path correctly:
- Limits the channel loop to
out_channels(line 177), preventing writes to unallocated channels- Extracts the appropriate samples for each channel from the packed input (lines 178-179)
- Uses bounds-checked operations to safely copy data (lines 180-185)
The indexing is sound: each channel receives
samplesworth of data at the correct planar positions.Optional: Consider logging when bounds checks fail.
Similar to the packed case, silent failures on bounds checks could hide bugs:
if let (Some(chunk_slice), Some(frame_slice)) = ( packed_chunk.get(channel_start..channel_end), frame.data_mut(channel).get_mut(start..start + sample_size), ) { frame_slice.copy_from_slice(chunk_slice); + } else { + debug_assert!(false, "Bounds check failed in planar de-interleaving"); }
330-383: LGTM: Good test coverage for main scenarios.The tests effectively cover:
- Both packed and planar formats
- Normal wrapping without channel reduction
- Channel reduction via
max_channelsparameter- Correct data extraction in all cases
Optional: Consider adding edge case tests.
While current coverage is solid, consider tests for:
- Empty or single-sample inputs
- Boundary conditions (e.g.,
MAX_AUDIO_CHANNELS)- Mono audio (1 channel) which takes a special fast path
Example:
#[test] fn wrap_mono_frame() { let info = AudioInfo::new_raw(Sample::U8(Type::Packed), 2, 1); let input = &[1, 2]; let frame = info.wrap_frame(input); assert_eq!(&frame.data(0)[0..input.len()], input); } #[test] fn wrap_empty_frame() { let info = AudioInfo::new_raw(Sample::U8(Type::Packed), 2, 2); let input = &[]; let frame = info.wrap_frame(input); // Verify no panic occurs }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/media-info/src/lib.rs(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.rs
📄 CodeRabbit inference engine (AGENTS.md)
**/*.rs: Format Rust code usingrustfmtand ensure all Rust code passes workspace-level clippy lints.
Rust modules should be named with snake_case, and crate directories should be in kebab-case.
Files:
crates/media-info/src/lib.rs
crates/*/src/**/*
📄 CodeRabbit inference engine (AGENTS.md)
Rust crates should place tests within the
src/and/or a siblingtests/directory for each crate insidecrates/*.
Files:
crates/media-info/src/lib.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Clippy
- GitHub Check: Build Desktop (aarch64-apple-darwin, macos-latest)
- GitHub Check: Build Desktop (x86_64-pc-windows-msvc, windows-latest)
- GitHub Check: Analyze (rust)
🔇 Additional comments (2)
crates/media-info/src/lib.rs (2)
142-142: LGTM: Correct channel capping.The
out_channelscalculation correctly ensures the output respects themax_channelslimit while never exceeding the available input channels. This value is consistently used for frame allocation and subsequent operations.
157-159: LGTM: Safe direct copy for non-reducing cases.The condition correctly identifies cases where no channel reduction occurs:
- Mono audio (1 channel) can be copied directly
- Packed frames where
self.channels <= out_channelsmeansself.channels == out_channels(sinceout_channels = min(self.channels, max_channels)), so all data fitsThe frame allocation and data sizes align correctly in both cases.
Summary by CodeRabbit