Skip to content

Commit

Permalink
Fix GH-17503: Undefined float conversion in mb_convert_variables
Browse files Browse the repository at this point in the history
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()`).
  • Loading branch information
cmb69 committed Feb 3, 2025
1 parent 7e06a81 commit b7524bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3092,7 +3092,8 @@ try_next_encoding:;
}

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

return length;
Expand Down
11 changes: 11 additions & 0 deletions ext/mbstring/tests/gh17503.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
GH-17503 (Undefined float conversion in mb_convert_variables)
--EXTENSIONS--
mbstring
--FILE--
<?php
$a = array_fill(0, 500, "<blah>");
var_dump(mb_convert_variables("ASCII", ["UTF-8", "UTF-16"], $a));
?>
--EXPECT--
string(5) "UTF-8"

0 comments on commit b7524bb

Please sign in to comment.