]> granicus.if.org Git - php/commitdiff
Remove invalid check of dictionary content and add initialization of dictionary if...
authorwapmorgan <wapmorgan@gmail.com>
Sat, 24 Jun 2017 19:44:01 +0000 (22:44 +0300)
committerBob Weinand <bobwei9@hotmail.com>
Sun, 25 Jun 2017 08:53:28 +0000 (10:53 +0200)
ext/zlib/tests/bug73944.phpt [new file with mode: 0644]
ext/zlib/tests/bug73944_fixture1 [new file with mode: 0644]
ext/zlib/tests/bug73944_fixture2 [new file with mode: 0644]
ext/zlib/zlib.c

diff --git a/ext/zlib/tests/bug73944.phpt b/ext/zlib/tests/bug73944.phpt
new file mode 100644 (file)
index 0000000..c4291af
--- /dev/null
@@ -0,0 +1,17 @@
+--TEST--
+Bug #73944: Dictionary option of intflate_init() does not work
+--FILE--
+<?php
+
+$in = inflate_init(ZLIB_ENCODING_RAW, array('dictionary' => str_repeat("\00", 32768)));
+$a = inflate_add($in, file_get_contents(__DIR__.'/bug73944_fixture1'));
+echo '1 block: '.strlen($a).PHP_EOL;
+
+$in = inflate_init(ZLIB_ENCODING_RAW, array('dictionary' => $a));
+$b = inflate_add($in, file_get_contents(__DIR__.'/bug73944_fixture2'));
+echo '2 block: '.($b === false ? 'failed' : strlen($b)).PHP_EOL;
+
+?>
+--EXPECTF--
+1 block: 32768
+2 block: 32768
diff --git a/ext/zlib/tests/bug73944_fixture1 b/ext/zlib/tests/bug73944_fixture1
new file mode 100644 (file)
index 0000000..badc516
Binary files /dev/null and b/ext/zlib/tests/bug73944_fixture1 differ
diff --git a/ext/zlib/tests/bug73944_fixture2 b/ext/zlib/tests/bug73944_fixture2
new file mode 100644 (file)
index 0000000..1a9abd6
Binary files /dev/null and b/ext/zlib/tests/bug73944_fixture2 differ
index 80607b6600025dcdf1808c344c225ef1a7c87328..b3c1a88fc508d24eeffdbe0715b2891fac1dfdcd 100644 (file)
@@ -761,24 +761,6 @@ static zend_bool zlib_create_dictionary_string(HashTable *options, char **dict,
                switch (Z_TYPE_P(option_buffer)) {
                        case IS_STRING: {
                                zend_string *str = Z_STR_P(option_buffer);
-                               int i;
-                               zend_bool last_null = 1;
-
-                               for (i = 0; i < ZSTR_LEN(str); i++) {
-                                       if (ZSTR_VAL(str)[i]) {
-                                               last_null = 0;
-                                       } else {
-                                               if (last_null) {
-                                                       php_error_docref(NULL, E_WARNING, "dictionary string must not contain empty entries (two consecutive NULL-bytes or one at the very beginning)");
-                                                       return 0;
-                                               }
-                                               last_null = 1;
-                                       }
-                               }
-                               if (!last_null) {
-                                       php_error_docref(NULL, E_WARNING, "dictionary string must be NULL-byte terminated (each dictionary entry has to be NULL-terminated)");
-                               }
-
                                *dict = emalloc(ZSTR_LEN(str));
                                memcpy(*dict, ZSTR_VAL(str), ZSTR_LEN(str));
                                *dictlen = ZSTR_LEN(str);
@@ -894,6 +876,21 @@ PHP_FUNCTION(inflate_init)
        }
 
        if (Z_OK == inflateInit2(ctx, encoding)) {
+               if (encoding == PHP_ZLIB_ENCODING_RAW && dictlen > 0) {
+                       php_zlib_context *php_ctx = (php_zlib_context *) ctx;
+                       switch (inflateSetDictionary(ctx, (Bytef *) php_ctx->inflateDict, php_ctx->inflateDictlen)) {
+                               case Z_OK:
+                                       efree(php_ctx->inflateDict);
+                                       php_ctx->inflateDict = NULL;
+                                       break;
+                               case Z_DATA_ERROR:
+                                       php_error_docref(NULL, E_WARNING, "dictionary does not match expected dictionary (incorrect adler32 hash)");
+                                       efree(php_ctx->inflateDict);
+                                       php_ctx->inflateDict = NULL;
+                                       RETURN_FALSE;
+                               EMPTY_SWITCH_DEFAULT_CASE()
+                       }
+               }
                RETURN_RES(zend_register_resource(ctx, le_inflate));
        } else {
                efree(ctx);