Skip to content

Unify error handling of bad dictionary elements #16407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 34 additions & 33 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -796,61 +796,62 @@ static bool zlib_create_dictionary_string(HashTable *options, char **dict, size_
*dict = emalloc(ZSTR_LEN(str));
memcpy(*dict, ZSTR_VAL(str), ZSTR_LEN(str));
*dictlen = ZSTR_LEN(str);
} break;

return 1;
}

case IS_ARRAY: {
HashTable *dictionary = Z_ARR_P(option_buffer);
bool result = 1;

if (zend_hash_num_elements(dictionary) > 0) {
char *dictptr;
zval *cur;
zend_string **strings = safe_emalloc(zend_hash_num_elements(dictionary), sizeof(zend_string *), 0);
zend_string **end, **ptr = strings - 1;
size_t total = 0;

zval *cur;
ZEND_HASH_FOREACH_VAL(dictionary, cur) {
*++ptr = zval_get_string(cur);
ZEND_ASSERT(*ptr);
if (ZSTR_LEN(*ptr) == 0 || EG(exception)) {
do {
zend_string_release(*ptr);
} while (--ptr >= strings);
efree(strings);
if (!EG(exception)) {
zend_argument_value_error(2, "must not contain empty strings");
}
return 0;
zend_string *string = zval_try_get_string(cur);
if (string == NULL) {
result = 0;
break;
}
*dictlen += ZSTR_LEN(string) + 1;
strings[total++] = string;
if (ZSTR_LEN(string) == 0) {
result = 0;
zend_argument_value_error(2, "must not contain empty strings");
break;
}
if (zend_str_has_nul_byte(*ptr)) {
do {
zend_string_release(*ptr);
} while (--ptr >= strings);
efree(strings);
if (zend_str_has_nul_byte(string)) {
result = 0;
zend_argument_value_error(2, "must not contain strings with null bytes");
return 0;
break;
}

*dictlen += ZSTR_LEN(*ptr) + 1;
} ZEND_HASH_FOREACH_END();

dictptr = *dict = emalloc(*dictlen);
ptr = strings;
end = strings + zend_hash_num_elements(dictionary);
do {
memcpy(dictptr, ZSTR_VAL(*ptr), ZSTR_LEN(*ptr));
dictptr += ZSTR_LEN(*ptr);
char *dictptr = emalloc(*dictlen);
*dict = dictptr;
for (size_t i = 0; i < total; i++) {
zend_string *string = strings[i];
dictptr = zend_mempcpy(dictptr, ZSTR_VAL(string), ZSTR_LEN(string));
*dictptr++ = 0;
zend_string_release_ex(*ptr, 0);
} while (++ptr != end);
zend_string_release(string);
}
efree(strings);
if (!result) {
efree(*dict);
*dict = NULL;
}
}
} break;

return result;
}

default:
zend_argument_type_error(2, "must be of type zero-terminated string or array, %s given", zend_zval_value_name(option_buffer));
return 0;
}
}

return 1;
}

Expand Down