]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Don't Attach Empty Dict Contents
authorW. Felix Handte <w@felixhandte.com>
Tue, 15 May 2018 21:23:16 +0000 (17:23 -0400)
committerW. Felix Handte <w@felixhandte.com>
Wed, 23 May 2018 21:53:03 +0000 (17:53 -0400)
In weird corner cases, they produce unexpected results...

lib/compress/zstd_compress.c

index aa7fc1e8db827709fd419a31394b41f229196cca..b1d52b9aa3f052bc6821261f72e0d475f987c2a2 100644 (file)
@@ -1227,20 +1227,25 @@ static size_t ZSTD_resetCCtx_usingCDict(ZSTD_CCtx* cctx,
     }
 
     if (attachDict) {
-        DEBUGLOG(4, "attaching dictionary into context");
-        cctx->blockState.matchState.dictMatchState = &cdict->matchState;
-
-        /* prep working match state so dict matches never have negative indices
-         * when they are translated to the working context's index space. */
-        if (cctx->blockState.matchState.window.dictLimit <
-            (U32)(cdict->matchState.window.nextSrc - cdict->matchState.window.base)) {
-            cctx->blockState.matchState.window.nextSrc =
-                cctx->blockState.matchState.window.base +
-                ( cdict->matchState.window.nextSrc
-                - cdict->matchState.window.base);
-            ZSTD_window_clear(&cctx->blockState.matchState.window);
+        if (cdict->matchState.window.nextSrc - cdict->matchState.window.base == 0) {
+            /* don't even attach dictionaries with no contents */
+            DEBUGLOG(4, "skipping attaching empty dictionary");
+        } else {
+            DEBUGLOG(4, "attaching dictionary into context");
+            cctx->blockState.matchState.dictMatchState = &cdict->matchState;
+
+            /* prep working match state so dict matches never have negative indices
+             * when they are translated to the working context's index space. */
+            if (cctx->blockState.matchState.window.dictLimit <
+                (U32)(cdict->matchState.window.nextSrc - cdict->matchState.window.base)) {
+                cctx->blockState.matchState.window.nextSrc =
+                    cctx->blockState.matchState.window.base +
+                    ( cdict->matchState.window.nextSrc
+                    - cdict->matchState.window.base);
+                ZSTD_window_clear(&cctx->blockState.matchState.window);
+            }
+            cctx->blockState.matchState.loadedDictEnd = cctx->blockState.matchState.window.dictLimit;
         }
-        cctx->blockState.matchState.loadedDictEnd = cctx->blockState.matchState.window.dictLimit;
     } else {
         DEBUGLOG(4, "copying dictionary into context");
         /* copy tables */