From b8136f019a7dc50b51fc51e68cfd38bace101057 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 27 May 2017 00:03:08 -0700 Subject: [PATCH] static dctx is incompatible with legacy support documented, and runtime tested --- doc/zstd_manual.html | 3 ++- lib/decompress/zstd_decompress.c | 9 ++++++++- lib/zstd.h | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/zstd_manual.html b/doc/zstd_manual.html index a4b59bc4..156e7dd9 100644 --- a/doc/zstd_manual.html +++ b/doc/zstd_manual.html @@ -718,7 +718,8 @@ size_t ZSTD_CDict_loadDictionary(ZSTD_CDict* cdict, const void* dict, size_t dic @return : pointer to ZSTD_DCtx*, or NULL if error (size too small) Note : zstd will never resize nor malloc() when using a static dctx. If it needs more memory than available, it will simply error out. - Note 2 : there is no corresponding "free" function. + Note 2 : static dctx is incompatible with legacy support + Note 3 : there is no corresponding "free" function. Since workspace was allocated externally, it must be freed externally. Limitation : currently not compatible with internal DDict creation, such as ZSTD_initDStream_usingDict(). diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index fe7446ab..a5972438 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -1586,6 +1586,8 @@ static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx, size_t decodedSize; size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); if (ZSTD_isError(frameSize)) return frameSize; + /* legacy support is incompatible with static dctx */ + if (dctx->staticSize) return ERROR(memory_allocation); decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize); @@ -2258,8 +2260,11 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB U32 someMoreWork = 1; #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) - if (zds->legacyVersion) + if (zds->legacyVersion) { + /* legacy support is incompatible with static dctx */ + if (zds->staticSize) return ERROR(memory_allocation); return ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); + } #endif while (someMoreWork) { @@ -2277,6 +2282,8 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB if (legacyVersion) { const void* const dict = zds->ddict ? zds->ddict->dictContent : NULL; size_t const dictSize = zds->ddict ? zds->ddict->dictSize : 0; + /* legacy support is incompatible with static dctx */ + if (zds->staticSize) return ERROR(memory_allocation); CHECK_F(ZSTD_initLegacyStream(&zds->legacyContext, zds->previousLegacyVersion, legacyVersion, dict, dictSize)); zds->legacyVersion = zds->previousLegacyVersion = legacyVersion; diff --git a/lib/zstd.h b/lib/zstd.h index b1d8ecad..3bbdb76f 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -844,7 +844,8 @@ ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); * @return : pointer to ZSTD_DCtx*, or NULL if error (size too small) * Note : zstd will never resize nor malloc() when using a static dctx. * If it needs more memory than available, it will simply error out. - * Note 2 : there is no corresponding "free" function. + * Note 2 : static dctx is incompatible with legacy support + * Note 3 : there is no corresponding "free" function. * Since workspace was allocated externally, it must be freed externally. * Limitation : currently not compatible with internal DDict creation, * such as ZSTD_initDStream_usingDict().