From 582b7f85ed25bf828854f9507d1c75c4d74962bf Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Tue, 15 May 2018 17:23:16 -0400 Subject: [PATCH] Don't Attach Empty Dict Contents In weird corner cases, they produce unexpected results... --- lib/compress/zstd_compress.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index aa7fc1e8..b1d52b9a 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -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; + 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); + /* 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 */