]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Improved index overflow protection
authorYann Collet <yann.collet.73@gmail.com>
Sun, 20 Mar 2016 15:20:06 +0000 (16:20 +0100)
committerYann Collet <yann.collet.73@gmail.com>
Sun, 20 Mar 2016 15:20:06 +0000 (16:20 +0100)
lib/zstd_compress.c
programs/playTests.sh

index a699f3e4341889efeeb9539723c86f2ad1cabeeb..cfb2519ad2743cb5a94b75e162aba3cf6309cfc6 100644 (file)
@@ -289,22 +289,31 @@ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx)
 }
 
 
-/*! ZSTD_reduceIndex() :
-*   rescale indexes to avoid future overflow (indexes are U32) */
-static void ZSTD_reduceIndex (ZSTD_CCtx* zc,
-                        const U32 reducerValue)
-{
-    const U32 contentLog = (zc->params.strategy == ZSTD_fast) ? 1 : zc->params.contentLog;
-    const U32 tableSpaceU32 = (1 << contentLog) + (1 << zc->params.hashLog);
-    U32* table32 = zc->hashTable;
-    U32 index;
-
-    for (index=0 ; index < tableSpaceU32 ; index++) {
-        if (table32[index] < reducerValue) table32[index] = 0;
-        else table32[index] -= reducerValue;
+/*! ZSTD_reduceTable() :
+*   rescale indexes from a table (indexes are U32) */
+static void ZSTD_reduceTable (U32* const table, U32 const size, U32 const reducerValue)
+{
+    U32 u;
+    for (u=0 ; u < size ; u++) {
+        if (table[u] < reducerValue) table[u] = 0;
+        else table[u] -= reducerValue;
     }
 }
 
+/*! ZSTD_reduceIndex() :
+*   rescale all indexes to avoid future overflow (indexes are U32) */
+static void ZSTD_reduceIndex (ZSTD_CCtx* zc, const U32 reducerValue)
+{
+    { const U32 hSize = 1 << zc->params.hashLog;
+      ZSTD_reduceTable(zc->hashTable, hSize, reducerValue); }
+
+    { const U32 contentSize = (zc->params.strategy == ZSTD_fast) ? 0 : (1 << zc->params.contentLog);
+      ZSTD_reduceTable(zc->contentTable, contentSize, reducerValue); }
+
+    { const U32 h3Size = (zc->params.searchLength == 3) ? (1 << HASHLOG3) : 0;
+      ZSTD_reduceTable(zc->hashTable3, h3Size, reducerValue); }
+}
+
 
 /*-*******************************************************
 *  Block entropic compression
index 891ab9d4ecfe831dc72645da56052857e1522fde..444d91ebe9ca40a9e10a511f200dd53370ddc74a 100755 (executable)
@@ -143,7 +143,7 @@ roundTripTest -g50000000 -P94 18
 roundTripTest -g50000000 -P94 19
 
 roundTripTest -g99000000 -P99 20
-roundTripTest -g6000000000 -P99 q
+roundTripTest -g6000000000 -P99 1
 
 rm tmp*