]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
[programs] Fix infinite loop when empty input is passed to trainer 3081/head
authorNick Terrell <terrelln@fb.com>
Wed, 2 Mar 2022 19:04:04 +0000 (11:04 -0800)
committerNick Terrell <terrelln@fb.com>
Wed, 2 Mar 2022 19:05:26 +0000 (11:05 -0800)
When an empty input file was passed to the dictionary trainer, it would infinite loop.
The added test case exposes the bug, and is fixed with this PR.

programs/dibio.c
tests/cli-tests/dict-builder/empty-input.sh [new file with mode: 0755]
tests/cli-tests/dict-builder/empty-input.sh.stderr.exact [new file with mode: 0644]

index fddbc9e5769849e42742c5a866293a5668be3711..f6757dd34880fa943cf99a7f6d4aa7364b44a872 100644 (file)
@@ -128,8 +128,11 @@ static int DiB_loadFiles(
     while ( nbSamplesLoaded < sstSize && fileIndex < nbFiles ) {
         size_t fileDataLoaded;
         S64 const fileSize = DiB_getFileSize(fileNamesTable[fileIndex]);
-        if (fileSize <= 0) /* skip if zero-size or file error */
+        if (fileSize <= 0) {
+            /* skip if zero-size or file error */
+            ++fileIndex;
             continue;
+        }
 
         f = fopen( fileNamesTable[fileIndex], "rb");
         if (f == NULL)
diff --git a/tests/cli-tests/dict-builder/empty-input.sh b/tests/cli-tests/dict-builder/empty-input.sh
new file mode 100755 (executable)
index 0000000..b500bfe
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+set -e
+for i in $(seq 50); do
+    datagen -s$i > file$i
+done
+touch empty
+
+set -v
+zstd -q --train empty file*
diff --git a/tests/cli-tests/dict-builder/empty-input.sh.stderr.exact b/tests/cli-tests/dict-builder/empty-input.sh.stderr.exact
new file mode 100644 (file)
index 0000000..2747e76
--- /dev/null
@@ -0,0 +1 @@
+zstd -q --train empty file*