diff --git a/src/lib_ccx/file_functions.c b/src/lib_ccx/file_functions.c index 2876dfb13..01ed30b63 100644 --- a/src/lib_ccx/file_functions.c +++ b/src/lib_ccx/file_functions.c @@ -224,10 +224,13 @@ int init_file_buffer(struct ccx_demuxer *ctx) { ctx->filebuffer_start = 0; ctx->filebuffer_pos = 0; + // Called for every newly opened file. Whatever the buffer still holds + // belongs to the previous file and has been consumed; keeping its byte + // count while rewinding the position would replay it as new data. + ctx->bytesinbuffer = 0; if (ctx->filebuffer == NULL) { ctx->filebuffer = (unsigned char *)malloc(FILEBUFFERSIZE); - ctx->bytesinbuffer = 0; } if (ctx->filebuffer == NULL) { diff --git a/src/rust/src/demuxer/demux.rs b/src/rust/src/demuxer/demux.rs index 2b5ded8db..b770890c5 100755 --- a/src/rust/src/demuxer/demux.rs +++ b/src/rust/src/demuxer/demux.rs @@ -556,12 +556,16 @@ mod tests { let mut ctx = dummy_demuxer(); let buf = vec![1u8; unsafe { FILEBUFFERSIZE } as usize].into_boxed_slice(); ctx.filebuffer = Box::into_raw(buf) as *mut u8; + let original = ctx.filebuffer; ctx.bytesinbuffer = 123; + ctx.filebuffer_pos = 123; let res = init_file_buffer(&mut ctx); assert_eq!(res, 0); - assert!(!ctx.filebuffer.is_null()); - // bytesinbuffer remains unchanged. - assert_eq!(ctx.bytesinbuffer, 123); + assert_eq!(ctx.filebuffer, original); + // The bytes belong to the previously opened file: they must not be + // replayed as data of the new one. + assert_eq!(ctx.filebuffer_pos, 0); + assert_eq!(ctx.bytesinbuffer, 0); // Clean up. unsafe { let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut( diff --git a/src/rust/src/file_functions/file.rs b/src/rust/src/file_functions/file.rs index a0b49b9e4..2de7b5c43 100644 --- a/src/rust/src/file_functions/file.rs +++ b/src/rust/src/file_functions/file.rs @@ -40,11 +40,14 @@ pub const FILEBUFFERSIZE: usize = 1024 * 1024 * 16; // 16 Mbytes no less. Minimi pub fn init_file_buffer(ctx: &mut CcxDemuxer) -> i32 { ctx.filebuffer_start = 0; ctx.filebuffer_pos = 0; + // Called for every newly opened file. Whatever the buffer still holds + // belongs to the previous file and has been consumed; keeping its byte + // count while rewinding the position would replay it as new data. + ctx.bytesinbuffer = 0; if ctx.filebuffer.is_null() { let mut buf = vec![0u8; FILEBUFFERSIZE].into_boxed_slice(); ctx.filebuffer = buf.as_mut_ptr(); mem::forget(buf); - ctx.bytesinbuffer = 0; } if ctx.filebuffer.is_null() { return -1;