-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-16326: Memory management is broken for bad dictionaries #16335
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
GH-16326 (Memory management is broken for bad dictionaries) | ||
--EXTENSIONS-- | ||
zlib | ||
--FILE-- | ||
<?php | ||
try { | ||
deflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => [" ", ""]]); | ||
} catch (ValueError $ex) { | ||
echo $ex->getMessage(), "\n"; | ||
} | ||
try { | ||
deflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => ["hello", "wor\0ld"]]); | ||
} catch (ValueError $ex) { | ||
echo $ex->getMessage(), "\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
deflate_init(): Argument #2 ($options) must not contain empty strings | ||
deflate_init(): Argument #2 ($options) must not contain strings with null bytes |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -807,7 +807,7 @@ static bool zlib_create_dictionary_string(HashTable *options, char **dict, size_ | |
if (zend_hash_num_elements(dictionary) > 0) { | ||
char *dictptr; | ||
zval *cur; | ||
zend_string **strings = emalloc(sizeof(zend_string *) * zend_hash_num_elements(dictionary)); | ||
zend_string **strings = safe_emalloc(zend_hash_num_elements(dictionary), sizeof(zend_string *), 0); | ||
zend_string **end, **ptr = strings - 1; | ||
|
||
ZEND_HASH_FOREACH_VAL(dictionary, cur) { | ||
|
@@ -816,10 +816,10 @@ static bool zlib_create_dictionary_string(HashTable *options, char **dict, size_ | |
*++ptr = zval_get_string(cur); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future we should use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't make it better, I'm afraid. Now we have to check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but it is easier to miss checking for |
||
if (!*ptr || ZSTR_LEN(*ptr) == 0 || EG(exception)) { | ||
if (*ptr) { | ||
efree(*ptr); | ||
zend_string_release(*ptr); | ||
} | ||
while (--ptr >= strings) { | ||
efree(ptr); | ||
zend_string_release(*ptr); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this can be adjusted to a do-while loop for consistency with the NUL check case below. Or perhaps this can be unified into a single place with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, since Regarding goto see https://xkcd.com/292/ ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer a well-placed goto over duplicating error handling all over the place. In fact it's already a little messy with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, it's much better than it was, and I'm fine with. Maybe we get a third opinion. :) |
||
efree(strings); | ||
if (!EG(exception)) { | ||
|
@@ -830,7 +830,7 @@ static bool zlib_create_dictionary_string(HashTable *options, char **dict, size_ | |
for (i = 0; i < ZSTR_LEN(*ptr); i++) { | ||
if (ZSTR_VAL(*ptr)[i] == 0) { | ||
TimWolla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
do { | ||
efree(ptr); | ||
zend_string_release(*ptr); | ||
} while (--ptr >= strings); | ||
efree(strings); | ||
zend_argument_value_error(2, "must not contain strings with null bytes"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I prefer using
sizeof
with an expression (in which case I also prefer to omit the parentheses), but I think I've seen it most of the time using a type in php-src. Do we have a style guideline about that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not aware of anything.