Skip to content

Commit b7524bb

Browse files
committed
Fix GH-17503: Undefined float conversion in mb_convert_variables
Conversion of floating point to integer values is undefined if the integral part of the float value cannot be represented by the integer type. We need to cater to that explicitly (in a manner similar to `zend_dval_to_lval_cap()`).
1 parent 7e06a81 commit b7524bb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

ext/mbstring/mbstring.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3092,7 +3092,8 @@ try_next_encoding:;
30923092
}
30933093

30943094
for (size_t i = 0; i < length; i++) {
3095-
array[i].demerits *= array[i].multiplier;
3095+
float demerits = array[i].demerits * array[i].multiplier;
3096+
array[i].demerits = demerits < (float) UINT64_MAX ? (uint64_t) demerits : UINT64_MAX;
30963097
}
30973098

30983099
return length;

ext/mbstring/tests/gh17503.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
GH-17503 (Undefined float conversion in mb_convert_variables)
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
$a = array_fill(0, 500, "<blah>");
8+
var_dump(mb_convert_variables("ASCII", ["UTF-8", "UTF-16"], $a));
9+
?>
10+
--EXPECT--
11+
string(5) "UTF-8"

0 commit comments

Comments
 (0)