Skip to content

Commit f32583d

Browse files
committed
Restricts the size of format/fourcc byte-sequences to 4 using [u8; 4] instead of len() runtime check
1 parent 31fe0b4 commit f32583d

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/lib.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub struct Config<'a> {
112112
pub resolution: (u32, u32),
113113
/// FourCC of format (e.g. `b"RGB3"`). Note that case matters.
114114
/// Default is `b"YUYV"`.
115-
pub format: &'a [u8],
115+
pub format: &'a [u8; 4],
116116
/// Storage method of interlaced video. See `FIELD_*` constants.
117117
/// [Details](http://linuxtv.org/downloads/v4l-dvb-apis/field-order.html#v4l2-field).
118118
/// Default is `FIELD_NONE` (progressive).
@@ -160,7 +160,7 @@ impl FormatInfo {
160160
}
161161
}
162162

163-
fn fourcc(fmt: &[u8]) -> u32 {
163+
fn fourcc(fmt: &[u8; 4]) -> u32 {
164164
u32::from(fmt[0])
165165
| (u32::from(fmt[1])) << 8
166166
| (u32::from(fmt[2])) << 16
@@ -315,11 +315,7 @@ impl Camera {
315315
}
316316

317317
/// Get detailed info about the available resolutions.
318-
pub fn resolutions(&self, format: &[u8]) -> Result<ResolutionInfo> {
319-
if format.len() != 4 {
320-
return Err(Error::BadFormat);
321-
}
322-
318+
pub fn resolutions(&self, format: &[u8; 4]) -> Result<ResolutionInfo> {
323319
let fourcc = FormatInfo::fourcc(format);
324320
let mut size = v4l2::Frmsizeenum::new(fourcc);
325321

@@ -354,11 +350,7 @@ impl Camera {
354350
}
355351

356352
/// Get detailed info about the available intervals.
357-
pub fn intervals(&self, format: &[u8], resolution: (u32, u32)) -> Result<IntervalInfo> {
358-
if format.len() != 4 {
359-
return Err(Error::BadFormat);
360-
}
361-
353+
pub fn intervals(&self, format: &[u8; 4], resolution: (u32, u32)) -> Result<IntervalInfo> {
362354
let fourcc = FormatInfo::fourcc(format);
363355
let mut ival = v4l2::Frmivalenum::new(fourcc, resolution);
364356

@@ -619,11 +611,7 @@ impl Camera {
619611
Ok(())
620612
}
621613

622-
fn tune_format(&self, resolution: (u32, u32), format: &[u8], field: u32) -> Result<()> {
623-
if format.len() != 4 {
624-
return Err(Error::BadFormat);
625-
}
626-
614+
fn tune_format(&self, resolution: (u32, u32), format: &[u8; 4], field: u32) -> Result<()> {
627615
let fourcc = FormatInfo::fourcc(format);
628616
let mut fmt = v4l2::Format::new(resolution, fourcc, field as u32);
629617

0 commit comments

Comments
 (0)