Skip to content

Commit 3928d47

Browse files
committed
Unify error handling of bad dictionary elements
As is, we have separate error handling and reporting for empty strings and strings containing NUL bytes. For simpler error handling we merge both cases; while that is slightly worse regarding the error reporting, it shouldn't matter in practice, since users need to check the validity of the dictionary upfront anyway.
1 parent 150599e commit 3928d47

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

ext/zlib/tests/gh16326.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ try {
2121
}
2222
?>
2323
--EXPECT--
24-
deflate_init(): Argument #2 ($options) must not contain empty strings
25-
deflate_init(): Argument #2 ($options) must not contain strings with null bytes
24+
deflate_init(): Argument #2 ($options) must not contain empty strings or strings with NUL bytes
25+
deflate_init(): Argument #2 ($options) must not contain empty strings or strings with NUL bytes
2626
Object of class stdClass could not be converted to string

ext/zlib/zlib.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -808,24 +808,15 @@ static bool zlib_create_dictionary_string(HashTable *options, char **dict, size_
808808
zend_string **end, **ptr = strings - 1;
809809

810810
ZEND_HASH_FOREACH_VAL(dictionary, cur) {
811-
*++ptr = zval_get_string(cur);
812-
ZEND_ASSERT(*ptr);
813-
if (ZSTR_LEN(*ptr) == 0 || EG(exception)) {
811+
*++ptr = zval_try_get_string(cur);
812+
if (*ptr == NULL || ZSTR_LEN(*ptr) == 0 || zend_str_has_nul_byte(*ptr)) {
814813
do {
815-
zend_string_release(*ptr);
814+
if (*ptr != NULL) {
815+
zend_string_release(*ptr);
816+
}
816817
} while (--ptr >= strings);
817818
efree(strings);
818-
if (!EG(exception)) {
819-
zend_argument_value_error(2, "must not contain empty strings");
820-
}
821-
return 0;
822-
}
823-
if (zend_str_has_nul_byte(*ptr)) {
824-
do {
825-
zend_string_release(*ptr);
826-
} while (--ptr >= strings);
827-
efree(strings);
828-
zend_argument_value_error(2, "must not contain strings with null bytes");
819+
zend_argument_value_error(2, "must not contain empty strings or strings with NUL bytes");
829820
return 0;
830821
}
831822

0 commit comments

Comments
 (0)