]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Fix #418 : printing selected segments in zdict debug mode can segfault with certain...
authorYann Collet <cyan@fb.com>
Wed, 2 Nov 2016 20:57:55 +0000 (13:57 -0700)
committerYann Collet <cyan@fb.com>
Wed, 2 Nov 2016 20:57:55 +0000 (13:57 -0700)
lib/dictBuilder/zdict.c

index b3f20b12ba66a1071adb835c5d301a260d580341..ea50b54ad3f78c77f8c4aa45a9b21f82a59cabb6 100644 (file)
@@ -898,12 +898,14 @@ size_t ZDICT_trainFromBuffer_unsafe(
         U32 const nb = MIN(25, dictList[0].pos);
         U32 const dictContentSize = ZDICT_dictSize(dictList);
         U32 u;
-        DISPLAYLEVEL(3, "\n %u segments found, of total size %u \n", dictList[0].pos, dictContentSize);
-        DISPLAYLEVEL(3, "list %u best segments \n", nb);
-        for (u=1; u<=nb; u++) {
-            U32 pos = dictList[u].pos;
-            U32 length = dictList[u].length;
-            U32 printedLength = MIN(40, length);
+        DISPLAYLEVEL(3, "\n %u segments found, of total size %u \n", dictList[0].pos-1, dictContentSize);
+        DISPLAYLEVEL(3, "list %u best segments \n", nb-1);
+        for (u=1; u<nb; u++) {
+            U32 const pos = dictList[u].pos;
+            U32 const length = dictList[u].length;
+            U32 const printedLength = MIN(40, length);
+            if ((pos > samplesBuffSize) || ((pos + length) > samplesBuffSize))
+                return ERROR(GENERIC);   /* should never happen */
             DISPLAYLEVEL(3, "%3u:%3u bytes at pos %8u, savings %7u bytes |",
                          u, length, pos, dictList[u].savings);
             ZDICT_printHex((const char*)samplesBuffer+pos, printedLength);