]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Add proper bounds check on adding ldms
authorsenhuang42 <senhuang96@fb.com>
Mon, 28 Sep 2020 21:58:23 +0000 (17:58 -0400)
committersenhuang42 <senhuang96@fb.com>
Wed, 7 Oct 2020 17:56:25 +0000 (13:56 -0400)
lib/compress/zstd_ldm.c
lib/compress/zstd_opt.c

index 5f44ce78acd83d0d4924b02640d8bc56eddd8bfb..e31c6838d533910901c323bdb2b2623ed48e28d8 100644 (file)
@@ -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);
index 70ec8a9419b7e4755a6d9866fbbcabcb29bc1b1d..bc6436a1acba3e22ed4ae8261ef62856ef2602b4 100644 (file)
@@ -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);