Skip to content

Commit e74bf42

Browse files
committed
ext/mbstring: Check conversion map only has integers
1 parent 193b22f commit e74bf42

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ PHP 8.4 UPGRADE NOTES
3131
will not affect you. As a result DOMImplementation::createDocument() now has
3232
a tentative return type of DOMDocument instead of DOMDocument|false.
3333

34+
- MBString:
35+
. mb_encode_numericentity() and mb_decode_numericentity() now check that
36+
the $map is only composed of integers, if not a ValueError is thrown.
37+
3438
- PDO_DBLIB:
3539
. setAttribute, DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER and DBLIB_ATTR_DATETIME_CONVERT
3640
have been changed to set value as a bool.

ext/mbstring/mbstring.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3878,7 +3878,14 @@ static uint32_t *make_conversion_map(HashTable *target_hash, size_t *conversion_
38783878
uint32_t *mapelm = convmap;
38793879

38803880
ZEND_HASH_FOREACH_VAL(target_hash, hash_entry) {
3881-
*mapelm++ = zval_get_long(hash_entry);
3881+
bool failed = true;
3882+
zend_long tmp = zval_try_get_long(hash_entry, &failed);
3883+
if (failed) {
3884+
efree(convmap);
3885+
zend_argument_value_error(2, "must only be composed of values of type int");
3886+
return NULL;
3887+
}
3888+
*mapelm++ = tmp;
38823889
} ZEND_HASH_FOREACH_END();
38833890

38843891
return convmap;

ext/mbstring/tests/mb_decode_numericentity.phpt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ echo "12: " . bin2hex(mb_decode_numericentity(mb_convert_encoding('�',
6161
$convmap = [];
6262
echo "13: " . mb_decode_numericentity('föo', $convmap, "UTF-8") . "\n";
6363

64-
$convmap = array(0x0, 0x2FFFF, 0); // 3 elements
65-
try {
66-
echo "14: " . mb_decode_numericentity($str3, $convmap, "UTF-8") . "\n";
67-
} catch (ValueError $ex) {
68-
echo "14: " . $ex->getMessage()."\n";
69-
}
70-
7164
echo "15: " . bin2hex(mb_decode_numericentity('�', [0, 1, 0, 0xFFFF], 'UTF-8')) . "\n";
7265
echo "16: " . bin2hex(mb_decode_numericentity('�', [0, 1, 0, 0xFFFF], 'UTF-8')) . "\n";
7366

@@ -182,7 +175,6 @@ for ($i = 12; $i < 256; $i++) {
182175
11e: &#x000000000
183176
12: 00bc614e
184177
13: f&ouml;o
185-
14: mb_decode_numericentity(): Argument #2 ($map) must have a multiple of 4 elements
186178
15: 00
187179
16: 00
188180
17: föo
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
mb_decode_numericentity() map errors
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
try {
8+
$convmap = array(0xFF, 0x2FFFF, 0); // 3 elements
9+
echo mb_decode_numericentity('str', $convmap, "UTF-8") . "\n";
10+
} catch (ValueError $ex) {
11+
echo $ex->getMessage(), "\n";
12+
}
13+
try {
14+
$convmap = array(0xFF, "not an int", 0, 0); // 3 elements
15+
echo mb_decode_numericentity('str', $convmap, "UTF-8") . "\n";
16+
} catch (ValueError $ex) {
17+
echo $ex->getMessage(), "\n";
18+
}
19+
20+
?>
21+
--EXPECT--
22+
mb_decode_numericentity(): Argument #2 ($map) must have a multiple of 4 elements
23+
mb_decode_numericentity(): Argument #2 ($map) must only be composed of values of type int

ext/mbstring/tests/mb_encode_numericentity.phpt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ echo "3: " . mb_encode_numericentity('aŒbœcŠdše€fg', $convmap, "UTF-8") .
1717
$convmap = [];
1818
echo "4: " . mb_encode_numericentity('föo', $convmap, "UTF-8") . "\n";
1919

20-
try {
21-
$convmap = array(0xFF, 0x2FFFF, 0); // 3 elements
22-
echo "5: " . mb_encode_numericentity('aŒbœcŠdše€fg', $convmap, "UTF-8") . "\n";
23-
} catch (ValueError $ex) {
24-
echo "5: " . $ex->getMessage() . "\n";
25-
}
26-
2720
// HTML-encode a null byte
2821
echo "6: " . mb_encode_numericentity("\x00", array(0, 1, 0, 0xFFFF), "UTF-8", false) . "\n";
2922
echo "6 (hex): " . mb_encode_numericentity("\x00", array(0, 1, 0, 0xFFFF), "UTF-8", true) . "\n";
@@ -72,7 +65,6 @@ echo "13: " . mb_encode_numericentity("\xFF", $convmap, "ASCII", true) . "\n";
7265
2: &#402;&#913;&#914;&#915;&#916;&#917;&#918;&#919;&#920;&#921;&#922;&#923;&#924;&#925;&#926;&#927;&#928;&#929;&#931;&#932;&#933;&#934;&#935;&#936;&#937;&#945;&#946;&#947;&#948;&#949;&#950;&#951;&#952;&#953;&#954;&#955;&#956;&#957;&#958;&#959;&#960;&#961;&#962;&#963;&#964;&#965;&#966;&#967;&#968;&#969;&#977;&#978;&#982;&#8226;&#8230;&#8242;&#8243;&#8254;&#8260;&#8472;&#8465;&#8476;&#8482;&#8501;&#8592;&#8593;&#8594;&#8595;&#8596;&#8629;&#8656;&#8657;&#8658;&#8659;&#8660;&#8704;&#8706;&#8707;&#8709;&#8711;&#8712;&#8713;&#8715;&#8719;&#8721;&#8722;&#8727;&#8730;&#8733;&#8734;&#8736;&#8743;&#8744;&#8745;&#8746;&#8747;&#8756;&#8764;&#8773;&#8776;&#8800;&#8801;&#8804;&#8805;&#8834;&#8835;&#8836;&#8838;&#8839;&#8853;&#8855;&#8869;&#8901;&#8968;&#8969;&#8970;&#8971;&#9001;&#9002;&#9674;&#9824;&#9827;&#9829;&#9830;
7366
3: a&#338;b&#339;c&#352;d&#353;e&#8364;fg
7467
4: föo
75-
5: mb_encode_numericentity(): Argument #2 ($map) must have a multiple of 4 elements
7668
6: &#0;
7769
6 (hex): &#x0;
7870
7: föo
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
mb_encode_numericentity() map errors
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
try {
8+
$convmap = array(0xFF, 0x2FFFF, 0); // 3 elements
9+
echo mb_encode_numericentity('str', $convmap, "UTF-8") . "\n";
10+
} catch (ValueError $ex) {
11+
echo $ex->getMessage(), "\n";
12+
}
13+
try {
14+
$convmap = array(0xFF, "not an int", 0, 0); // 3 elements
15+
echo mb_encode_numericentity('str', $convmap, "UTF-8") . "\n";
16+
} catch (ValueError $ex) {
17+
echo $ex->getMessage(), "\n";
18+
}
19+
20+
?>
21+
--EXPECT--
22+
mb_encode_numericentity(): Argument #2 ($map) must have a multiple of 4 elements
23+
mb_encode_numericentity(): Argument #2 ($map) must only be composed of values of type int

0 commit comments

Comments
 (0)