From: Elliot Gorokhovsky Date: Tue, 26 Apr 2022 21:54:25 +0000 (-0400) Subject: Hardcode repcode safety check, fix cosmetic nits X-Git-Tag: v1.5.4^2~215^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=518cb83833074d304dfcaa93cfc16039ea4683c8;p=thirdparty%2Fzstd.git Hardcode repcode safety check, fix cosmetic nits --- diff --git a/lib/compress/zstd_fast.c b/lib/compress/zstd_fast.c index c6e9dd33a..49a2add75 100644 --- a/lib/compress/zstd_fast.c +++ b/lib/compress/zstd_fast.c @@ -581,13 +581,13 @@ size_t ZSTD_compressBlock_fast_dictMatchState( static size_t ZSTD_compressBlock_fast_extDict_generic( ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], - void const* src, size_t srcSize, U32 const mls, U32 const hasStep) + void const* src, size_t srcSize, U32 const mls, U32 const commonScenario) { const ZSTD_compressionParameters* const cParams = &ms->cParams; U32* const hashTable = ms->hashTable; U32 const hlog = cParams->hashLog; /* support stepSize of 0 */ - size_t const stepSize = hasStep ? (cParams->targetLength + !(cParams->targetLength) + 1) : 2; + size_t const stepSize = commonScenario ? 2 : (cParams->targetLength + !(cParams->targetLength) + 1); const BYTE* const base = ms->window.base; const BYTE* const dictBase = ms->window.dictBase; const BYTE* const istart = (const BYTE*)src; @@ -633,9 +633,13 @@ static size_t ZSTD_compressBlock_fast_extDict_generic( { U32 const curr = (U32)(ip0 - base); U32 const maxRep = curr - dictStartIndex; - if (offset_2 >= maxRep) offset_2 = 0; - if (offset_1 >= maxRep) offset_1 = 0; - } + if (commonScenario) { + assert(offset_2 < maxRep); + assert(offset_1 < maxRep); + } else { + if (offset_2 >= maxRep) offset_2 = 0; + if (offset_1 >= maxRep) offset_1 = 0; + } } /* start each op */ _start: /* Requires: ip0 */ @@ -665,7 +669,7 @@ _start: /* Requires: ip0 */ const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base; U32 rval; if ( ((U32)(prefixStartIndex - repIndex) >= 4) /* intentional underflow */ - & (offset_1 > 0) ) { + & (commonScenario | (offset_1 > 0)) ) { rval = MEM_read32(repBase + repIndex); } else { rval = MEM_read32(ip2) ^ 1; /* guaranteed to not match. */ @@ -690,7 +694,9 @@ _start: /* Requires: ip0 */ } } { /* load match for ip[0] */ - U32 const mval = idx >= dictStartIndex ? MEM_read32(idxBase + idx) : MEM_read32(ip0) ^ 1; /* guaranteed not to match */ + U32 const mval = idx >= dictStartIndex ? + MEM_read32(idxBase + idx) : + MEM_read32(ip0) ^ 1; /* guaranteed not to match */ /* check match at ip[0] */ if (MEM_read32(ip0) == mval) { @@ -716,7 +722,9 @@ _start: /* Requires: ip0 */ hashTable[hash0] = current0; { /* load match for ip[0] */ - U32 const mval = idx >= dictStartIndex ? MEM_read32(idxBase + idx) : MEM_read32(ip0) ^ 1; /* guaranteed not to match */ + U32 const mval = idx >= dictStartIndex ? + MEM_read32(idxBase + idx) : + MEM_read32(ip0) ^ 1; /* guaranteed not to match */ /* check match at ip[0] */ if (MEM_read32(ip0) == mval) { @@ -804,7 +812,7 @@ _match: /* Requires: ip0, match0, offcode, matchEnd */ while (ip0 <= ilimit) { U32 const repIndex2 = (U32)(ip0-base) - offset_2; const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2; - if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (offset_2 > 0)) /* intentional underflow */ + if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (commonScenario | (offset_2 > 0))) /* intentional underflow */ && (MEM_read32(repMatch2) == MEM_read32(ip0)) ) { const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; size_t const repLength2 = ZSTD_count_2segments(ip0+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; @@ -836,8 +844,17 @@ size_t ZSTD_compressBlock_fast_extDict( void const* src, size_t srcSize) { U32 const mls = ms->cParams.minMatch; + const BYTE* const istart = (const BYTE*)src; + const BYTE* const base = ms->window.base; + const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); + const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, ms->cParams.windowLog); + U32 const curr = (U32)(istart - base); + U32 const maxRep = curr - lowLimit; + U32 const repOffsetsAreValid = (rep[0] < maxRep) & (rep[1] < maxRep); + + assert((rep[0] > 0) & (rep[1] > 0)); assert(ms->dictMatchState == NULL); - if (ms->cParams.targetLength > 1) { + if ((ms->cParams.targetLength <= 1) & repOffsetsAreValid) { switch (mls) { default: /* includes case 3 */ case 4 :