Skip to content

Commit 902e01f

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix memory leak when encoding check fails
2 parents 34d8bef + a54af45 commit 902e01f

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ PHP NEWS
5555
- Zlib:
5656
. Fixed bug GH-17745 (zlib extension incorrectly handles object arguments).
5757
(nielsdos)
58+
. Fix memory leak when encoding check fails. (nielsdos)
5859

5960
30 Jan 2025, PHP 8.4.4
6061

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Memory leak when passing a dictionary with invalid encoding
3+
--EXTENSIONS--
4+
zlib
5+
--FILE--
6+
<?php
7+
try {
8+
inflate_init(123456, ["dictionary" => "dict"]);
9+
} catch (ValueError $e) {
10+
echo $e->getMessage(), "\n";
11+
}
12+
try {
13+
deflate_init(123456, ["dictionary" => "dict"]);
14+
} catch (ValueError $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
?>
18+
--EXPECT--
19+
Encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE
20+
deflate_init(): Argument #1 ($encoding) must be one of ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP, or ZLIB_ENCODING_DEFLATE

ext/zlib/zlib.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,6 @@ PHP_FUNCTION(inflate_init)
878878
RETURN_THROWS();
879879
}
880880

881-
if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
882-
RETURN_THROWS();
883-
}
884-
885881
switch (encoding) {
886882
case PHP_ZLIB_ENCODING_RAW:
887883
case PHP_ZLIB_ENCODING_GZIP:
@@ -892,6 +888,10 @@ PHP_FUNCTION(inflate_init)
892888
RETURN_THROWS();
893889
}
894890

891+
if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
892+
RETURN_THROWS();
893+
}
894+
895895
object_init_ex(return_value, inflate_context_ce);
896896
ctx = Z_INFLATE_CONTEXT_P(return_value);
897897

@@ -1131,10 +1131,6 @@ PHP_FUNCTION(deflate_init)
11311131
RETURN_THROWS();
11321132
}
11331133

1134-
if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
1135-
RETURN_THROWS();
1136-
}
1137-
11381134
switch (encoding) {
11391135
case PHP_ZLIB_ENCODING_RAW:
11401136
case PHP_ZLIB_ENCODING_GZIP:
@@ -1145,6 +1141,10 @@ PHP_FUNCTION(deflate_init)
11451141
RETURN_THROWS();
11461142
}
11471143

1144+
if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
1145+
RETURN_THROWS();
1146+
}
1147+
11481148
object_init_ex(return_value, deflate_context_ce);
11491149
ctx = Z_DEFLATE_CONTEXT_P(return_value);
11501150

0 commit comments

Comments
 (0)