Skip to content

Commit c23efeb

Browse files
authored
Zlib: Fix the bug when zval_get_long silently cast $strategy into long in deflate_init (php#21841)
1 parent 6c7ef56 commit c23efeb

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,8 @@ PHP NEWS
197197
. Added ZipArchive::openString() method.
198198
(Tim Starling, Soner Sayakci, Ghaith Olabi)
199199

200+
- Zlib:
201+
. deflate_init() now raises a TypeError when the value for option
202+
"strategy" is not of type int. (Weilin Du)
203+
200204
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ PHP 8.6 UPGRADE NOTES
101101
files argument if one or more of the entries is not
102102
a string.
103103

104+
- Zlib:
105+
. deflate_init() now raises a TypeError when the value for option
106+
"strategy" is not of type int.
107+
104108
========================================
105109
2. New Features
106110
========================================
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
deflate_init(): strategy option type validation
3+
--EXTENSIONS--
4+
zlib
5+
--FILE--
6+
<?php
7+
8+
try {
9+
deflate_init(ZLIB_ENCODING_DEFLATE, ['strategy' => []]);
10+
} catch (TypeError $e) {
11+
echo $e->getMessage(), PHP_EOL;
12+
}
13+
14+
?>
15+
--EXPECT--
16+
deflate_init(): Argument #2 ($options) the value for option "strategy" must be of type int, array given

ext/zlib/zlib.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,8 +1115,14 @@ PHP_FUNCTION(deflate_init)
11151115
}
11161116

11171117
if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("strategy"))) != NULL) {
1118+
bool failed = false;
1119+
11181120
ZVAL_DEINDIRECT(option_buffer);
1119-
strategy = zval_get_long(option_buffer);
1121+
strategy = zval_try_get_long(option_buffer, &failed);
1122+
if (UNEXPECTED(failed)) {
1123+
zend_argument_type_error(2, "the value for option \"strategy\" must be of type int, %s given", zend_zval_value_name(option_buffer));
1124+
RETURN_THROWS();
1125+
}
11201126
}
11211127
switch (strategy) {
11221128
case Z_FILTERED:

0 commit comments

Comments
 (0)