Skip to content

Commit 55e676e

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()`). Closes GH-17689.
1 parent 88e1917 commit 55e676e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ PHP NEWS
1212
zend.exception_ignore_args=1 into account). (timwolla)
1313
. Fix fallback paths in fast_long_{add,sub}_function. (nielsdos)
1414

15+
- MBString:
16+
. Fixed bug GH-17503 (Undefined float conversion in mb_convert_variables).
17+
(cmb)
18+
1519
- Opcache:
1620
. Fixed bug GH-17654 (Multiple classes using same trait causes function
1721
JIT crash). (nielsdos)

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+
double demerits = array[i].demerits * (double) array[i].multiplier;
3096+
array[i].demerits = demerits < (double) 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)