From f97ca36eab4e3aadf2666a6130d37680a4f6a7d3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 6 Sep 2018 17:54:13 -0700 Subject: [PATCH] strengthened conditions for using workplace into fse table space ensure that the structure layout is as expected. will trigger an error if it changes in the future. Another solution would be to use a union, this would be cleaner and get rid of these static asserts. However, in order to keep the current code unmodified, it would be necessary to use an un-named unions. And apparently, un-named unions are only possible on "recent" compilers (C99+). --- lib/decompress/zstd_decompress.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index e9fc36331..35ed7c59a 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -2203,8 +2203,13 @@ static size_t ZSTD_loadEntropy(ZSTD_entropyDTables_t* entropy, const BYTE* const dictEnd = dictPtr + dictSize; if (dictSize <= 8) return ERROR(dictionary_corrupted); + assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */ dictPtr += 8; /* skip header = magic + dictID */ + ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, LLTable) == 0); + ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == sizeof(entropy->LLTable)); + ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == sizeof(entropy->LLTable) + sizeof(entropy->OFTable)); + ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, hufTable) == sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable)); ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, hufTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE); { void* const workspace = entropy; /* use fse tables as temporary workspace; implies fse table precede huffTable at beginning of entropy */ size_t const workspaceSize = offsetof(ZSTD_entropyDTables_t, hufTable); -- 2.47.2