From: senhuang42 Date: Mon, 28 Sep 2020 21:58:23 +0000 (-0400) Subject: Add proper bounds check on adding ldms X-Git-Tag: v1.4.7~57^2~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=28394b64f242dd6efcdda2dd3f4b8d926933fd97;p=thirdparty%2Fzstd.git Add proper bounds check on adding ldms --- diff --git a/lib/compress/zstd_ldm.c b/lib/compress/zstd_ldm.c index 5f44ce78a..e31c6838d 100644 --- a/lib/compress/zstd_ldm.c +++ b/lib/compress/zstd_ldm.c @@ -562,6 +562,13 @@ static rawSeq maybeSplitSequence(rawSeqStore_t* rawSeqStore, return sequence; } +static void printSeqStore(rawSeqStore_t* rawSeqStore) { + printf("rawSeqStore: pos: %zu, bytesDiscarded: %zu\n", rawSeqStore->pos); + for (int i = 0; i < rawSeqStore->size; ++i) { + printf("(of:%u ml:%u ll: %u)\n", rawSeqStore->seq[i].offset, rawSeqStore->seq[i].matchLength, rawSeqStore->seq[i].litLength); + } +} + size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore, ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], void const* src, size_t srcSize) @@ -578,6 +585,7 @@ size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore, if (cParams->strategy >= ZSTD_btopt) { size_t lastLLSize; + printSeqStore(rawSeqStore); ms->ldmSeqStore = *rawSeqStore; /* copy current seqStore */ const BYTE* const prevBase = (BYTE const*)ms->window.base; lastLLSize = blockCompressor(ms, seqStore, rep, src, srcSize); diff --git a/lib/compress/zstd_opt.c b/lib/compress/zstd_opt.c index 70ec8a941..bc6436a1a 100644 --- a/lib/compress/zstd_opt.c +++ b/lib/compress/zstd_opt.c @@ -828,6 +828,7 @@ static void ldm_skipSequences(rawSeqStore_t* rawSeqStore, size_t srcSize, U32 co } srcSize -= seq->matchLength; seq->matchLength = 0; + printf("ldm_skipSequences(): final: (of: %u, ml: %u, ll: %u)\n", seq->offset, seq->matchLength, seq->litLength); rawSeqStore->pos++; } } @@ -867,10 +868,17 @@ static int ldm_getNextMatch(rawSeqStore_t* ldmSeqStore, U32* matchStartPosInBlock, U32* matchEndPosInBlock, U32* matchOffset, U32 currPosInBlock, U32 remainingBytes, U32 sbi) { + if (ldmSeqStore->pos >= ldmSeqStore->size) { + // Don't use the LDM for the rest of the block (there is none) + printf("No ldm left in the block, pos max reached\n"); + *matchStartPosInBlock = UINT32_MAX; + *matchEndPosInBlock = UINT32_MAX; + return 1; + } rawSeq seq = ldm_splitSequence(ldmSeqStore, remainingBytes); - if (seq.offset == 0 || ldmSeqStore->pos > ldmSeqStore->size) { + if (seq.offset == 0) { // Don't use the LDM for the rest of the block (there is none) - printf("No ldm left in the block\n"); + printf("No ldm left in the block, offset = 0\n"); *matchStartPosInBlock = UINT32_MAX; *matchEndPosInBlock = UINT32_MAX; return 1; @@ -898,11 +906,8 @@ static void ldm_maybeAddLdm(ZSTD_match_t* matches, U32* nbMatches, U32 candidateOffCode = matchOffset + posDiff + ZSTD_REP_MOVE; if ((*nbMatches == 0 || candidateMatchLength >= matches[*nbMatches-1].len) && *nbMatches < ZSTD_OPT_NUM) { - printf("large enough, adding\n"); - /*matches[*nbMatches].len = candidateMatchLength; - matches[*nbMatches].off = candidateOffCode; - (*nbMatches)++;*/ - + printf("large enough: curr: %u currposinblock: %u (ofcode: %u, ml: %u)\n", curr, currPosInBlock, candidateOffCode, candidateMatchLength); + if (*nbMatches == 0) { matches[*nbMatches].len = candidateMatchLength; matches[*nbMatches].off = candidateOffCode; @@ -947,14 +952,10 @@ static void ldm_handleLdm(rawSeqStore_t* ldmSeqStore, ZSTD_match_t* matches, U32 U32 posOvershoot = currPosInBlock - *matchEndPosInBlock; printf("Overshot position by: %u\n", posOvershoot); ldm_skipSequences(ldmSeqStore, posOvershoot, MINMATCH); - int noMoreLdms = ldm_getNextMatch(ldmSeqStore, matchStartPosInBlock, - matchEndPosInBlock, matchOffset, - currPosInBlock, remainingBytes, sbi); - } else { - int noMoreLdms = ldm_getNextMatch(ldmSeqStore, matchStartPosInBlock, - matchEndPosInBlock, matchOffset, - currPosInBlock, remainingBytes, sbi); - } + } + int noMoreLdms = ldm_getNextMatch(ldmSeqStore, matchStartPosInBlock, + matchEndPosInBlock, matchOffset, + currPosInBlock, remainingBytes, sbi); } ldm_maybeAddLdm(matches, nbMatches, *matchStartPosInBlock, *matchEndPosInBlock, *matchOffset, currPosInBlock, curr, sbi); } @@ -1018,7 +1019,7 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms, U32 ldmEndPosInBlock = 0; U32 ldmOffset = 0; - printf("SBI for this block: %u\n", sbi); + printf("SBI for this block: %u, base: %u\n", sbi, base); if (ms->ldmSeqStore.size != 0) { ldm_getNextMatch(&ms->ldmSeqStore, &ldmStartPosInBlock, &ldmEndPosInBlock, &ldmOffset, (U32)(ip-istart), (U32)(iend-ip), sbi);