diff options
| author | Ilya Leoshkevich <iii@linux.ibm.com> | 2019-06-24 16:57:19 +0200 |
|---|---|---|
| committer | Hans Kristian Rosbach <hk-github@circlestorm.org> | 2019-07-18 11:22:52 +0200 |
| commit | bca48626c5f7ee279a9440e0f1bad752a50a7f2f (patch) | |
| tree | 124c8a9df2e1f007578e189eeec2362d6168bcda /test | |
| parent | d4b927b3462711795dde8cd7d787b9dabbf3f3bd (diff) | |
| download | Project-Tick-bca48626c5f7ee279a9440e0f1bad752a50a7f2f.tar.gz Project-Tick-bca48626c5f7ee279a9440e0f1bad752a50a7f2f.zip | |
longest_match: avoid using negative indices
Fixes #364
Diffstat (limited to 'test')
| -rw-r--r-- | test/.gitignore | 3 | ||||
| -rw-r--r-- | test/GH-364/test.bin | bin | 0 -> 8 bytes | |||
| -rw-r--r-- | test/INDEX | 2 | ||||
| -rw-r--r-- | test/Makefile.in | 14 | ||||
| -rw-r--r-- | test/switchlevels.c | 112 |
5 files changed, 128 insertions, 3 deletions
diff --git a/test/.gitignore b/test/.gitignore index 2c3af0a08c..96a3cad070 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,2 +1,5 @@ # ignore Makefiles; they're all automatically generated Makefile +/switchlevels +/switchlevels.dSYM/ +/switchlevels.exe diff --git a/test/GH-364/test.bin b/test/GH-364/test.bin Binary files differnew file mode 100644 index 0000000000..1b1cb4d44c --- /dev/null +++ b/test/GH-364/test.bin diff --git a/test/INDEX b/test/INDEX index a1e6b72808..9b66ef6cb9 100644 --- a/test/INDEX +++ b/test/INDEX @@ -7,6 +7,8 @@ CVE-2005-1849 : CVE-2005-2096 : test cases for the relevant CVEs GH-361 : test case for overlapping matches https://github.com/zlib-ng/zlib-ng/issues/361 +GH-364 : test case for switching compression levels + https://github.com/zlib-ng/zlib-ng/issues/364 testCVEinputs.sh: script to run tests for CVEs where input data is supplied diff --git a/test/Makefile.in b/test/Makefile.in index caf49496db..79e79e0153 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -7,7 +7,8 @@ CFLAGS= EXE= SRCDIR= SRCTOP= -TEST_LDFLAGS=-L.. ../libz.a +LIBNAME= +TEST_LDFLAGS=-L.. ../$(LIBNAME).a WITH_FUZZERS= COMPATTESTS = @@ -97,15 +98,22 @@ CVE-2003-0107$(EXE): CVE-2003-0107.o $(CC) $(CFLAGS) -o $@ CVE-2003-0107.o $(TEST_LDFLAGS) .PHONY: ghtests -ghtests: testGH-361 +ghtests: testGH-361 testGH-364 .PHONY: testGH-361 testGH-361: $(QEMU_RUN) ../minigzip$(EXE) -4 <$(SRCDIR)/GH-361/test.txt >/dev/null +switchlevels$(EXE): $(SRCDIR)/switchlevels.c + $(CC) $(CFLAGS) -I.. -I$(SRCTOP) -o $@ $< $(TEST_LDFLAGS) + +.PHONY: testGH-364 +testGH-364: switchlevels$(EXE) + $(QEMU_RUN) ./switchlevels$(EXE) 1 5 9 3 <$(SRCDIR)/GH-364/test.bin >/dev/null + clean: rm -f *.o *.gcda *.gcno *.gcov - rm -f CVE-2003-0107$(EXE) + rm -f CVE-2003-0107$(EXE) switchlevels$(EXE) distclean: rm -f Makefile diff --git a/test/switchlevels.c b/test/switchlevels.c new file mode 100644 index 0000000000..67bb1524a0 --- /dev/null +++ b/test/switchlevels.c @@ -0,0 +1,112 @@ +/* Compresses a user-specified number of chunks from stdin into stdout as a single gzip stream. + * Each chunk is compressed with a user-specified level. + */ + +#include "zbuild.h" +#ifdef ZLIB_COMPAT +# include "zlib.h" +#else +# include "zlib-ng.h" +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static int read_all(unsigned char *buf, size_t size) +{ + for (size_t total_read = 0; total_read < size;) { + size_t n_read = fread(buf + total_read, 1, size - total_read, stdin); + if (ferror(stdin)) { + perror("fread\n"); + return 1; + } + if (n_read == 0) { + fprintf(stderr, "Premature EOF\n"); + return 1; + } + total_read += n_read; + } + return 0; +} + +static int write_all(unsigned char *buf, size_t size) +{ + for (size_t total_written = 0; total_written < size;) { + size_t n_written = fwrite(buf + total_written, 1, size - total_written, stdout); + if (ferror(stdout)) { + perror("fwrite\n"); + return 1; + } + total_written += n_written; + } + return 0; +} + +static int compress_chunk(PREFIX3(stream) *strm, int level, int size, int last) +{ + int ret = 1; + int err = PREFIX(deflateParams)(strm, level, Z_DEFAULT_STRATEGY); + if (err != Z_OK) { + fprintf(stderr, "deflateParams() failed with code %d\n", err); + goto done; + } + unsigned long compsize = 100 + 2 * PREFIX(deflateBound)(strm, size); + unsigned char *buf = malloc(size + compsize); + if (buf == NULL) { + fprintf(stderr, "Out of memory\n"); + goto done; + } + if (read_all(buf, size) != 0) { + goto free_buf; + } + strm->next_in = buf; + strm->avail_in = size; + strm->next_out = buf + size; + strm->avail_out = compsize; + err = PREFIX(deflate)(strm, last ? Z_FINISH : Z_SYNC_FLUSH); + if ((!last && err != Z_OK) || (last && err != Z_STREAM_END)) { + fprintf(stderr, "deflate() failed with code %d\n", err); + goto free_buf; + } + if (strm->avail_in != 0) { + fprintf(stderr, "deflate() did not consume %d bytes of input\n", strm->avail_in); + goto free_buf; + } + if (write_all(buf + size, compsize - strm->avail_out) != 0) { + goto free_buf; + } + ret = 0; +free_buf: + free(buf); +done: + return ret; +} + +/* =========================================================================== + * Usage: switchlevels level1 size1 [level2 size2 ...] + */ + +int main(int argc, char **argv) +{ + int ret = EXIT_FAILURE; + PREFIX3(stream) strm; + memset(&strm, 0, sizeof(strm)); + int err = PREFIX(deflateInit2)(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY); + if (err != Z_OK) { + fprintf(stderr, "deflateInit() failed with code %d\n", err); + goto done; + } + for (int i = 1; i < argc - 1; i += 2) { + int level = atoi(argv[i]); + int size = atoi(argv[i + 1]); + if (compress_chunk(&strm, level, size, i + 2 >= argc - 1) != 0) { + goto deflate_end; + } + } + ret = EXIT_SUCCESS; +deflate_end: + PREFIX(deflateEnd)(&strm); +done: + return ret; +} |
