audio/claxon: remove some panic points

This remove some possible panic points due to unwrapping and
underflowing integer maths.

Signed-off-by: Luca BRUNO <lucab@lucabruno.net>
This commit is contained in:
Luca BRUNO 2020-04-11 09:30:35 +00:00
parent 281aad2243
commit 02935b7005
No known key found for this signature in database
GPG key ID: A9834A2252078E4E

View file

@ -226,21 +226,15 @@ impl ClaxonDec {
state: &mut State, state: &mut State,
indata: &[u8], indata: &[u8],
) -> Result<gst::FlowSuccess, gst::FlowError> { ) -> Result<gst::FlowSuccess, gst::FlowError> {
let streaminfo = match get_claxon_streaminfo(indata) { let streaminfo = get_claxon_streaminfo(indata).map_err(|e| {
Ok(v) => v, gst_element_error!(element, gst::StreamError::Decode, [e]);
Err(error) => { gst::FlowError::Error
gst_element_error!(element, gst::StreamError::Decode, [error]); })?;
return Err(gst::FlowError::Error);
}
};
let audio_info = match get_gstaudioinfo(streaminfo) { let audio_info = get_gstaudioinfo(streaminfo).map_err(|e| {
Ok(v) => v, gst_element_error!(element, gst::StreamError::Decode, [&e]);
Err(error) => { gst::FlowError::Error
gst_element_error!(element, gst::StreamError::Decode, [error]); })?;
return Err(gst::FlowError::Error);
}
};
gst_debug!( gst_debug!(
self.cat, self.cat,
@ -355,26 +349,30 @@ fn get_claxon_streaminfo(indata: &[u8]) -> Result<claxon::metadata::StreamInfo,
fn get_gstaudioinfo( fn get_gstaudioinfo(
streaminfo: claxon::metadata::StreamInfo, streaminfo: claxon::metadata::StreamInfo,
) -> Result<gst_audio::AudioInfo, &'static str> { ) -> Result<gst_audio::AudioInfo, String> {
let format = match streaminfo.bits_per_sample { let format = match streaminfo.bits_per_sample {
8 => gst_audio::AudioFormat::S8, 8 => gst_audio::AudioFormat::S8,
16 => gst_audio::AUDIO_FORMAT_S16, 16 => gst_audio::AUDIO_FORMAT_S16,
24 => gst_audio::AUDIO_FORMAT_S2432, 24 => gst_audio::AUDIO_FORMAT_S2432,
32 => gst_audio::AUDIO_FORMAT_S32, 32 => gst_audio::AUDIO_FORMAT_S32,
_ => return Err("format not supported"), _ => return Err("format not supported".to_string()),
}; };
if streaminfo.channels > 8 { let index = match streaminfo.channels as usize {
return Err("more than 8 channels not supported yet"); 0 => return Err("no channels".to_string()),
} n if n > 8 => return Err("more than 8 channels, not supported yet".to_string()),
let mut audio_info = n => n,
gst_audio::AudioInfo::new(format, streaminfo.sample_rate, streaminfo.channels); };
let index = streaminfo.channels as usize;
let to = &FLAC_CHANNEL_POSITIONS[index - 1][..index]; let to = &FLAC_CHANNEL_POSITIONS[index - 1][..index];
audio_info = audio_info.positions(to); let info_builder =
gst_audio::AudioInfo::new(format, streaminfo.sample_rate, streaminfo.channels)
.positions(to);
Ok(audio_info.build().unwrap()) let audio_info = info_builder
.build()
.map_err(|e| format!("failed to build audio info: {}", e))?;
Ok(audio_info)
} }
// http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9 // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9