]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Add test for issue #1235.
authorMika Lindqvist <postmaster@raasu.org>
Wed, 6 Apr 2022 20:49:24 +0000 (23:49 +0300)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Fri, 22 Apr 2022 11:50:22 +0000 (13:50 +0200)
* Test both compressBound() and deflateBound() as those share same code fragment.

test/Makefile.in
test/gh1235.c [new file with mode: 0644]

index 4078d60d458d5d1f950bdbca0d0e3418965d5de5..119eb6802eb7b78285da6eb203f6a58e7f03bf86 100644 (file)
@@ -55,7 +55,7 @@ testCVEinputs: check_cross_dep
        @EXE=$(EXE) QEMU_RUN="${QEMU_RUN}" $(SRCDIR)/testCVEinputs.sh
 
 .PHONY: ghtests
-ghtests: testGH-361 testGH-364 testGH-751
+ghtests: testGH-361 testGH-364 testGH-751 testGH-1235
 
 .PHONY: testGH-361
 testGH-361:
@@ -72,9 +72,16 @@ testGH-364: switchlevels$(EXE)
 testGH-751:
        $(QEMU_RUN) ../minigzip$(EXE) <$(SRCDIR)/GH-751/test.txt | $(QEMU_RUN) ../minigzip$(EXE) -d >/dev/null
 
+gh1235$(EXE): $(SRCDIR)/gh1235.c
+       $(CC) $(CFLAGS) -I.. -I$(SRCTOP) -o $@ $< $(TEST_LDFLAGS)
+
+.PHONY: testGH-1235
+testGH-1235: gh1235$(EXE)
+       $(QEMU_RUN) ./gh1235$(EXE)
+
 clean:
        rm -f *.o *.gcda *.gcno *.gcov
-       rm -f switchlevels$(EXE)
+       rm -f switchlevels$(EXE) gh1235$(EXE)
 
 distclean:
        rm -f Makefile
diff --git a/test/gh1235.c b/test/gh1235.c
new file mode 100644 (file)
index 0000000..472282d
--- /dev/null
@@ -0,0 +1,39 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include "zutil.h"
+
+int main(void) {
+    unsigned char plain[32];
+    unsigned char compressed[130];
+    PREFIX3(stream) strm;
+    int bound;
+    z_size_t bytes;
+
+    for (int i = 0; i <= 32; i++) {
+        memset(plain, 6, i);
+        memset(&strm, 0, sizeof(strm));
+        PREFIX(deflateInit2)(&strm, 0, 8, 31, 1, Z_DEFAULT_STRATEGY);
+        bound = PREFIX(deflateBound)(&strm, i);
+        strm.next_in = plain;
+        strm.next_out = compressed;
+        strm.avail_in = i;
+        strm.avail_out = sizeof(compressed);
+        if (PREFIX(deflate)(&strm, Z_FINISH) != Z_STREAM_END) return -1;
+        if (strm.avail_in != 0) return -1;
+        printf("bytes = %2i, deflateBound = %2i, total_out = %2zi\n", i, bound, strm.total_out);
+        if (bound < strm.total_out) return -1;
+        if (PREFIX(deflateEnd)(&strm) != Z_OK) return -1;
+    }
+    for (int i = 0; i <= 32; i++) {
+        bytes = sizeof(compressed);
+        for (int j = 0; j < i; j++) {
+            plain[j] = j;
+        }
+        bound = PREFIX(compressBound)(i);
+        if (PREFIX(compress2)(compressed, &bytes, plain, i, 1) != Z_OK) return -1;
+        printf("bytes = %2i, compressBound = %2i, total_out = %2zi\n", i, bound, (size_t)bytes);
+        if (bytes > bound) return -1;
+    }
+    return 0;
+}