]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Disallow Too-Long Repcodes When Using an Attached Dict
authorW. Felix Handte <w@felixhandte.com>
Wed, 23 May 2018 16:51:09 +0000 (12:51 -0400)
committerW. Felix Handte <w@felixhandte.com>
Fri, 25 May 2018 17:13:28 +0000 (13:13 -0400)
lib/compress/zstd_double_fast.c

index d415791be3cd728560d40d5210b95d93421c6bba..711bf41abe1b56e6eb35339daa133bd1fab0b6d3 100644 (file)
@@ -85,17 +85,23 @@ size_t ZSTD_compressBlock_doubleFast_generic(
     const U32 dictIndexDelta       = dictMode == ZSTD_dictMatchState ?
                                      prefixLowestIndex - (U32)(dictEnd - dictBase) :
                                      0;
+    const U32 dictAndPrefixLength  = (U32)(ip - prefixLowest + dictEnd - dictLowest);
 
     assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
 
     /* init */
-    ip += (dictMode == ZSTD_noDict && ip == prefixLowest);
-    {   U32 const maxRep = dictMode == ZSTD_dictMatchState ?
-                           (U32)(ip - dictLowest) :
-                           (U32)(ip - prefixLowest);
+    ip += (dictAndPrefixLength == 0);
+    if (dictMode == ZSTD_noDict) {
+        U32 const maxRep = (U32)(ip - prefixLowest);
         if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
         if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
     }
+    if (dictMode == ZSTD_dictMatchState) {
+        /* dictMatchState repCode checks don't currently handle repCode == 0
+         * disabling. */
+        assert(offset_1 <= dictAndPrefixLength);
+        assert(offset_2 <= dictAndPrefixLength);
+    }
 
     /* Main Search Loop */
     while (ip < ilimit) {   /* < instead of <=, because repcode check at (ip+1) */