Skip to content

Commit cd1869b

Browse files
wapmorganbwoebi
authored andcommitted
Remove invalid check of dictionary content and add initialization of dictionary if raw compression used
1 parent 4d61005 commit cd1869b

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

ext/zlib/tests/bug73944.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #73944: Dictionary option of intflate_init() does not work
3+
--FILE--
4+
<?php
5+
6+
$in = inflate_init(ZLIB_ENCODING_RAW, array('dictionary' => str_repeat("\00", 32768)));
7+
$a = inflate_add($in, file_get_contents(__DIR__.'/bug73944_fixture1'));
8+
echo '1 block: '.strlen($a).PHP_EOL;
9+
10+
$in = inflate_init(ZLIB_ENCODING_RAW, array('dictionary' => $a));
11+
$b = inflate_add($in, file_get_contents(__DIR__.'/bug73944_fixture2'));
12+
echo '2 block: '.($b === false ? 'failed' : strlen($b)).PHP_EOL;
13+
14+
?>
15+
--EXPECTF--
16+
1 block: 32768
17+
2 block: 32768

ext/zlib/tests/bug73944_fixture1

14 KB
Binary file not shown.

ext/zlib/tests/bug73944_fixture2

25.9 KB
Binary file not shown.

ext/zlib/zlib.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -761,24 +761,6 @@ static zend_bool zlib_create_dictionary_string(HashTable *options, char **dict,
761761
switch (Z_TYPE_P(option_buffer)) {
762762
case IS_STRING: {
763763
zend_string *str = Z_STR_P(option_buffer);
764-
int i;
765-
zend_bool last_null = 1;
766-
767-
for (i = 0; i < ZSTR_LEN(str); i++) {
768-
if (ZSTR_VAL(str)[i]) {
769-
last_null = 0;
770-
} else {
771-
if (last_null) {
772-
php_error_docref(NULL, E_WARNING, "dictionary string must not contain empty entries (two consecutive NULL-bytes or one at the very beginning)");
773-
return 0;
774-
}
775-
last_null = 1;
776-
}
777-
}
778-
if (!last_null) {
779-
php_error_docref(NULL, E_WARNING, "dictionary string must be NULL-byte terminated (each dictionary entry has to be NULL-terminated)");
780-
}
781-
782764
*dict = emalloc(ZSTR_LEN(str));
783765
memcpy(*dict, ZSTR_VAL(str), ZSTR_LEN(str));
784766
*dictlen = ZSTR_LEN(str);
@@ -894,6 +876,21 @@ PHP_FUNCTION(inflate_init)
894876
}
895877

896878
if (Z_OK == inflateInit2(ctx, encoding)) {
879+
if (encoding == PHP_ZLIB_ENCODING_RAW && dictlen > 0) {
880+
php_zlib_context *php_ctx = (php_zlib_context *) ctx;
881+
switch (inflateSetDictionary(ctx, (Bytef *) php_ctx->inflateDict, php_ctx->inflateDictlen)) {
882+
case Z_OK:
883+
efree(php_ctx->inflateDict);
884+
php_ctx->inflateDict = NULL;
885+
break;
886+
case Z_DATA_ERROR:
887+
php_error_docref(NULL, E_WARNING, "dictionary does not match expected dictionary (incorrect adler32 hash)");
888+
efree(php_ctx->inflateDict);
889+
php_ctx->inflateDict = NULL;
890+
RETURN_FALSE;
891+
EMPTY_SWITCH_DEFAULT_CASE()
892+
}
893+
}
897894
RETURN_RES(zend_register_resource(ctx, le_inflate));
898895
} else {
899896
efree(ctx);

0 commit comments

Comments
 (0)