]> 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 18:13:16 +0000 (14:13 -0400)
committerW. Felix Handte <w@felixhandte.com>
Wed, 13 Jun 2018 18:58:36 +0000 (14:58 -0400)
lib/compress/zstd_lazy.c

index d9695f10a7956569392f5feec5b3f24fe5d8d565..4d4d81e1f14c23b027c7ca73856a363182336101 100644 (file)
@@ -503,15 +503,33 @@ size_t ZSTD_compressBlock_lazy_generic(
         (searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS);
     U32 offset_1 = rep[0], offset_2 = rep[1], savedOffset=0;
 
+    const ZSTD_matchState_t* const dms = ms->dictMatchState;
+    const U32 dictLowestIndex      = dictMode == ZSTD_dictMatchState ?
+                                     dms->window.dictLimit : 0;
+    const BYTE* const dictBase     = dictMode == ZSTD_dictMatchState ?
+                                     dms->window.base : NULL;
+    const BYTE* const dictLowest   = dictMode == ZSTD_dictMatchState ?
+                                     dictBase + dictLowestIndex : NULL;
+    const BYTE* const dictEnd      = dictMode == ZSTD_dictMatchState ?
+                                     dms->window.nextSrc : NULL;
+    const U32 dictAndPrefixLength = (U32)(ip - prefixLowest + dictEnd - dictLowest);
+
     (void)dictMode;
 
     /* init */
-    ip += (ip==prefixLowest);
+    ip += (dictAndPrefixLength == 0);
     ms->nextToUpdate3 = ms->nextToUpdate;
-    {   U32 const maxRep = (U32)(ip-prefixLowest);
+    if (dictMode == ZSTD_noDict) {
+        U32 const maxRep = (U32)(ip - prefixLowest);
         if (offset_2 > maxRep) savedOffset = offset_2, offset_2 = 0;
         if (offset_1 > maxRep) savedOffset = 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);
+    }
 
     /* Match Loop */
     while (ip < ilimit) {