Skip to content

Fix memory leak when encoding check fails #17788

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions ext/zlib/tests/leak_invalid_encoding_with_dict.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Memory leak when passing a dictionary with invalid encoding
--EXTENSIONS--
zlib
--FILE--
<?php
try {
inflate_init(123456, ["dictionary" => "dict"]);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
deflate_init(123456, ["dictionary" => "dict"]);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
Encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE
deflate_init(): Argument #1 ($encoding) must be one of ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP, or ZLIB_ENCODING_DEFLATE
16 changes: 8 additions & 8 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,10 +879,6 @@ PHP_FUNCTION(inflate_init)
RETURN_THROWS();
}

if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
RETURN_THROWS();
}

switch (encoding) {
case PHP_ZLIB_ENCODING_RAW:
case PHP_ZLIB_ENCODING_GZIP:
Expand All @@ -893,6 +889,10 @@ PHP_FUNCTION(inflate_init)
RETURN_THROWS();
}

if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
RETURN_THROWS();
}

object_init_ex(return_value, inflate_context_ce);
ctx = Z_INFLATE_CONTEXT_P(return_value);

Expand Down Expand Up @@ -1132,10 +1132,6 @@ PHP_FUNCTION(deflate_init)
RETURN_THROWS();
}

if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
RETURN_THROWS();
}

switch (encoding) {
case PHP_ZLIB_ENCODING_RAW:
case PHP_ZLIB_ENCODING_GZIP:
Expand All @@ -1146,6 +1142,10 @@ PHP_FUNCTION(deflate_init)
RETURN_THROWS();
}

if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
RETURN_THROWS();
}

object_init_ex(return_value, deflate_context_ce);
ctx = Z_DEFLATE_CONTEXT_P(return_value);

Expand Down
Loading