From: wapmorgan Date: Sat, 24 Jun 2017 19:44:01 +0000 (+0300) Subject: Remove invalid check of dictionary content and add initialization of dictionary if... X-Git-Tag: php-7.0.22RC1~45 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cd1869bcf2c6dc981133b844d915bd93f82f786c;p=php Remove invalid check of dictionary content and add initialization of dictionary if raw compression used --- diff --git a/ext/zlib/tests/bug73944.phpt b/ext/zlib/tests/bug73944.phpt new file mode 100644 index 0000000000..c4291afa4e --- /dev/null +++ b/ext/zlib/tests/bug73944.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #73944: Dictionary option of intflate_init() does not work +--FILE-- + 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 index 0000000000..badc516288 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 index 0000000000..1a9abd6aee Binary files /dev/null and b/ext/zlib/tests/bug73944_fixture2 differ diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 80607b6600..b3c1a88fc5 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -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);