Skip to content

Commit c3e34a5

Browse files
committed
Verify cache entries are not overwritten in debug builds.
1 parent 3adc278 commit c3e34a5

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

mp4parse_capi/src/lib.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
3636

3737
use byteorder::WriteBytesExt;
38+
use fallible_collections::TryReserveError;
3839
use mp4parse::unstable::rational_scale;
3940
use std::convert::TryFrom;
4041
use std::convert::TryInto;
42+
use std::hash::Hash;
4143

4244
use std::io::Read;
4345

@@ -435,6 +437,24 @@ pub struct Mp4parseAvifParser {
435437
sample_table: TryHashMap<u32, TryVec<Indice>>,
436438
}
437439

440+
trait CacheInsertExt<K, V> {
441+
fn insert_cache_entry(&mut self, key: K, value: V) -> Result<(), TryReserveError>;
442+
}
443+
444+
impl<K, V> CacheInsertExt<K, V> for TryHashMap<K, V>
445+
where
446+
K: Eq + Hash,
447+
{
448+
fn insert_cache_entry(&mut self, key: K, value: V) -> Result<(), TryReserveError> {
449+
let replaced = self.insert(key, value)?;
450+
debug_assert!(
451+
replaced.is_none(),
452+
"cache entries must never be replaced once published"
453+
);
454+
Ok(())
455+
}
456+
}
457+
438458
impl Mp4parseAvifParser {
439459
fn context(&self) -> &AvifContext {
440460
&self.context
@@ -854,7 +874,7 @@ fn get_track_audio_info(
854874
return Err(Mp4parseStatus::Invalid);
855875
}
856876
Ok(_) => {
857-
opus_header.insert((track_index, desc_i), v)?;
877+
opus_header.insert_cache_entry((track_index, desc_i), v)?;
858878
if let Some(v) = opus_header.get(&(track_index, desc_i)) {
859879
if v.len() > u32::MAX as usize {
860880
return Err(Mp4parseStatus::Invalid);
@@ -913,7 +933,7 @@ fn get_track_audio_info(
913933

914934
parser
915935
.audio_track_sample_descriptions
916-
.insert(track_index, audio_sample_infos)?;
936+
.insert_cache_entry(track_index, audio_sample_infos)?;
917937
match parser.audio_track_sample_descriptions.get(&track_index) {
918938
Some(sample_info) => {
919939
if sample_info.len() > u32::MAX as usize {
@@ -1105,7 +1125,7 @@ fn mp4parse_get_track_video_info_safe(
11051125

11061126
parser
11071127
.video_track_sample_descriptions
1108-
.insert(track_index, video_sample_infos)?;
1128+
.insert_cache_entry(track_index, video_sample_infos)?;
11091129
match parser.video_track_sample_descriptions.get(&track_index) {
11101130
Some(sample_info) => {
11111131
if sample_info.len() > u32::MAX as usize {
@@ -1481,7 +1501,7 @@ fn get_indice_table(
14811501

14821502
if let Some(v) = create_sample_table(track, offset_time) {
14831503
indices.set_indices(&v);
1484-
sample_table_cache.insert(track_id, v)?;
1504+
sample_table_cache.insert_cache_entry(track_id, v)?;
14851505
return Ok(());
14861506
}
14871507

0 commit comments

Comments
 (0)