@@ -691,6 +691,7 @@ const fn ZSTD_startingInputLength(format: Format) -> size_t {
691691fn ZSTD_DCtx_resetParameters ( dctx : & mut MaybeUninit < ZSTD_DCtx > ) {
692692 unsafe {
693693 let dctx = dctx. as_mut_ptr ( ) ;
694+ debug_assert_eq ! ( ( * dctx) . streamStage, StreamStage :: Init ) ;
694695 ( * dctx) . format = Format :: ZSTD_f_zstd1 ;
695696 ( * dctx) . maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT as size_t ;
696697 ( * dctx) . outBufferMode = BufferMode :: Buffered ;
@@ -831,7 +832,9 @@ pub unsafe extern "C" fn ZSTD_copyDCtx(dstDCtx: *mut ZSTD_DCtx, srcDCtx: *const
831832///
832833/// [`ZSTD_d_refMultipleDDicts`] must be enabled for this function to be called.
833834fn ZSTD_DCtx_selectFrameDDict ( dctx : & mut ZSTD_DCtx ) {
834- if !( dctx. ddict ) . is_null ( ) {
835+ debug_assert_eq ! ( dctx. refMultipleDDicts, MultipleDDicts :: Multiple ) ;
836+ debug_assert ! ( !dctx. ddictSet. is_null( ) ) ;
837+ if !dctx. ddict . is_null ( ) {
835838 // FIXME: make safe
836839 let frameDDict = unsafe {
837840 ZSTD_DDictHashSet_getDDict ( dctx. ddictSet . as_mut ( ) . unwrap ( ) , dctx. fParams . dictID )
@@ -1412,6 +1415,9 @@ fn find_frame_size_info(src: &[u8], format: Format) -> ZSTD_frameSizeInfo {
14121415 && is_skippable_frame ( src)
14131416 {
14141417 frameSizeInfo. compressedSize = read_skippable_frame_size ( src) ;
1418+ debug_assert ! (
1419+ ERR_isError ( frameSizeInfo. compressedSize) || frameSizeInfo. compressedSize <= src. len( )
1420+ ) ;
14151421 frameSizeInfo
14161422 } else {
14171423 let mut ip = 0 ;
@@ -1815,6 +1821,7 @@ unsafe fn ZSTD_decompressFrame(
18151821 ) ;
18161822 }
18171823 BlockType :: Compressed => {
1824+ debug_assert ! ( dctx. isFrameDecompression) ;
18181825 decodedSize = ZSTD_decompressBlock_internal (
18191826 dctx,
18201827 op. as_mut_ptr ( ) . cast ( ) ,
@@ -2172,6 +2179,7 @@ unsafe fn decompress_continue(dctx: &mut ZSTD_DCtx, mut dst: Writer<'_>, src: &[
21722179 let mut rSize: size_t = 0 ;
21732180 match dctx. bType {
21742181 BlockType :: Compressed => {
2182+ debug_assert ! ( dctx. isFrameDecompression) ;
21752183 rSize = ZSTD_decompressBlock_internal (
21762184 dctx,
21772185 dst. as_mut_ptr ( ) . cast ( ) ,
@@ -2183,11 +2191,13 @@ unsafe fn decompress_continue(dctx: &mut ZSTD_DCtx, mut dst: Writer<'_>, src: &[
21832191 dctx. expected = 0 ; // streaming not supported
21842192 }
21852193 BlockType :: Raw => {
2194+ debug_assert ! ( src. len( ) <= dctx. expected) ;
21862195 rSize = copy_raw_block_slice ( dst. subslice ( ..) , src) ;
21872196 let err_code_0 = rSize;
21882197 if ERR_isError ( err_code_0) {
21892198 return err_code_0;
21902199 }
2200+ debug_assert_eq ! ( rSize, src. len( ) ) ;
21912201 dctx. expected = ( dctx. expected ) . wrapping_sub ( rSize) ;
21922202 }
21932203 BlockType :: Rle => {
@@ -2241,6 +2251,7 @@ unsafe fn decompress_continue(dctx: &mut ZSTD_DCtx, mut dst: Writer<'_>, src: &[
22412251 rSize
22422252 }
22432253 DecompressStage :: CheckChecksum => {
2254+ debug_assert_eq ! ( src. len( ) , 4 ) ; // guaranteed by dctx.expected
22442255 if dctx. validateChecksum {
22452256 let h32 = ZSTD_XXH64_digest ( & mut dctx. xxhState ) as u32 ;
22462257 let check32 = u32:: from_le_bytes ( * src. first_chunk ( ) . unwrap ( ) ) ;
@@ -2254,6 +2265,7 @@ unsafe fn decompress_continue(dctx: &mut ZSTD_DCtx, mut dst: Writer<'_>, src: &[
22542265 0
22552266 }
22562267 DecompressStage :: DecodeSkippableHeader => {
2268+ debug_assert_ne ! ( dctx. format, Format :: ZSTD_f_zstd1_magicless ) ;
22572269 // complete skippable header
22582270 dctx. headerBuffer [ 8 - src. len ( ) ..] [ ..src. len ( ) ] . copy_from_slice ( src) ;
22592271 dctx. expected =
@@ -2810,6 +2822,7 @@ pub unsafe extern "C" fn ZSTD_DCtx_refDDict(
28102822 return Error :: memory_allocation. to_error_code ( ) ;
28112823 } ;
28122824
2825+ debug_assert_eq ! ( ( * dctx) . staticSize, 0 ) ; // ddictSet cannot have been allocated if static dctx
28132826 let err_code = ZSTD_DDictHashSet_addDDict ( ddictSet, ddict, ( * dctx) . customMem ) ;
28142827 if ERR_isError ( err_code) {
28152828 return err_code;
@@ -3240,6 +3253,8 @@ unsafe fn ZSTD_decompressContinueStream(
32403253 * op = ( * op) . add ( decodedSize) ;
32413254 // flushing is not needed
32423255 zds. streamStage = StreamStage :: Read ;
3256+ debug_assert ! ( * op <= oend) ;
3257+ debug_assert_eq ! ( zds. outBufferMode, BufferMode :: Stable ) ;
32433258 }
32443259 }
32453260
@@ -3403,6 +3418,7 @@ pub unsafe extern "C" fn ZSTD_decompressStream(
34033418 // need more input
34043419 let toLoad = hSize - zds. lhSize ; // if hSize!=0, hSize > zds->lhSize
34053420 let remainingInput = iend. offset_from_unsigned ( ip) ;
3421+ debug_assert ! ( iend >= ip) ;
34063422 if toLoad > remainingInput {
34073423 // not enough input to load full header
34083424 if remainingInput > 0 {
@@ -3614,7 +3630,7 @@ pub unsafe extern "C" fn ZSTD_decompressStream(
36143630 let toLoad_0 = neededInSize. wrapping_sub ( zds. inPos ) ;
36153631 let isSkipFrame = matches ! ( zds. stage, DecompressStage :: SkipFrame ) ;
36163632 // At this point we shouldn't be decompressing a block that we can stream.
3617- assert_eq ! (
3633+ debug_assert_eq ! (
36183634 neededInSize,
36193635 ZSTD_nextSrcSizeToDecompressWithInputSize ( zds, iend. offset_from_unsigned( ip) )
36203636 ) ;
0 commit comments