]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Allocate memory for DDict hash set when parameter is set
authorsenhuang42 <senhuang96@fb.com>
Wed, 23 Dec 2020 21:33:57 +0000 (16:33 -0500)
committersenhuang42 <senhuang96@fb.com>
Thu, 7 Jan 2021 17:29:11 +0000 (12:29 -0500)
lib/decompress/zstd_decompress.c
lib/decompress/zstd_decompress_internal.h

index 3cb94eae939ebce6151553d2c7e561287b0964c8..21fae1ae6f58c6fc81e5be03b6d1ec36139c07c6 100644 (file)
@@ -122,6 +122,8 @@ static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
     dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
     ZSTD_DCtx_resetParameters(dctx);
     dctx->validateChecksum = 1;
+    dctx->refMultipleDDicts = 0;
+    dctx->ddictSet = NULL;
 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
     dctx->dictContentEndForFuzzing = NULL;
 #endif
@@ -178,6 +180,10 @@ size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
         if (dctx->legacyContext)
             ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
 #endif
+        if (dctx->ddictSet) {
+            ZSTD_freeDDictHashSet(dctx->ddictSet, cMem);
+            dctx->ddictSet = NULL;
+        }
         ZSTD_customFree(dctx, cMem);
         return 0;
     }
@@ -1400,6 +1406,10 @@ static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) {
     return ret;
 }
 
+static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) {
+    if (hashSet->ddictPtrTable) ZSTD_customFree(hashSet->ddictPtrTable, customMem);
+    ZSTD_customFree(hashSet, customMem);
+}
 
 size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
                                    const void* dict, size_t dictSize,
@@ -1602,6 +1612,18 @@ size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value
         case ZSTD_d_refMultipleDDicts:
             CHECK_DBOUNDS(ZSTD_d_refMultipleDDicts, value);
             dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value;
+            if (dctx->refMultipleDDicts == ZSTD_d_refMultipleDicts) {
+                if (dctx->ddictSet == NULL) {
+                    if (dctx->staticSize != 0) {
+                        RETURN_ERROR(memory_allocation, "Multiple DDicts are not allowed with static dctx!");
+                    }
+                    dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem);
+                }
+            } else {
+                ZSTD_freeDDictHashSet(dctx->ddictSet, dctx->customMem);
+                dctx->ddictSet = NULL;
+            }
+            return 0;
         default:;
     }
     RETURN_ERROR(parameter_unsupported, "");
index 3724521e596f76c1721d2f6b2b9d8cb7741a41cf..053ecc16257f37bdb188c20d9299a1b0d29c3ebf 100644 (file)
@@ -136,6 +136,7 @@ struct ZSTD_DCtx_s
     U32 dictID;
     int ddictIsCold;             /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */
     ZSTD_dictUses_e dictUses;
+    ZSTD_DDictHashSet* ddictSet;                    /* Hash set for multiple ddicts */
     ZSTD_refMultipleDDicts_e refMultipleDDicts;     /* User specified: if == 1, will allow references to multiple DDicts. Default == 0 (disabled) */
 
     /* streaming */