mysqli bind_result() incorrectly handles unsigned int
| Bug #35103 | mysqli bind_result() incorrectly handles unsigned int | ||||
|---|---|---|---|---|---|
| Submitted: | 2005-11-04 11:17 UTC | Modified: | 2005-12-02 09:49 UTC | ||
| From: | php at pjberkel dot com | Assigned: | andrey (profile) | ||
| Status: | Closed | Package: | MySQLi related | ||
| PHP Version: | 5.1CVS-2005-12-02 (cvs) | OS: | * | ||
| Private report: | No | CVE-ID: | None | ||
[2005-11-04 11:17 UTC] php at pjberkel dot com
Description: ------------ When using "mysqli_stmt_bind_result" to retrieve a 32bit unsigned integer value from a mysql database (version 4.1.13-standard) that is greater than the maximum *signed* value but less than the maximum *unsigned* value (i.e. 2147483647 < int <= 4294967295), the integer is returned incorrectly as a signed value. I did read in the manual that php does not support unsigned integers (http://www.php.net/manual/en/language.types.integer.php), however in this case, mysqli_stmt_bind_result should probably cast the result to a float. Reproduce code: --------------- <?php $mysqli = new mysqli("host", "user", "pass", "db"); $mysqli->query("CREATE TABLE temp (id INT UNSIGNED NOT NULL)"); $mysqli->query("INSERT INTO temp (id) VALUES (2147483647),(2147483648),(2147483649),(3800001532),(3900002281),(4294967295)"); /* BEGIN EXAMPLE OF BUG */ $stmt = $mysqli->prepare("SELECT id FROM temp"); $stmt->execute(); $stmt->bind_result($id); while ($stmt->fetch()) { print $id . "<br>\n"; } $stmt->close(); /* END EXAMPLE OF BUG */ $mysqli->query("DROP TABLE temp"); $mysqli->close(); ?> Expected result: ---------------- 2147483647 2147483648 2147483649 3800001532 3900002281 4294967295 Actual result: -------------- 2147483647 -2147483648 -2147483647 -494965764 -394965015 -1
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2005-11-04 15:53 UTC] andrey@php.net
if your code is aware that the variable is unsigned you can get the unsigned value by using sprintf() with %u as format specificator andrey@lmy004:~/test> php -r '$a=-2; printf("%d %u\n", $a, $a);' -2 4294967294 However I think it is good idea to make that implicit so mysqli to return a string (on 32bit) and normal int (on 64bit).[2005-11-05 23:05 UTC] iliaa@php.net
[2005-11-07 08:34 UTC] php at pjberkel dot com
Thanks for the suggested workaround, while this does patch the problem it will be preferable to have a permanent fix as updating my entire codebase to deal with this problem will be quite time-consuming. Note that mysqli->query() / mysqli->fetch_row() does not appear to suffer from this bug, changing the prepared statement in the example to the following code: $result = $mysqli->query("SELECT id FROM temp"); while ($row = $result->fetch_row()) { var_dump($row[0]); } $result->close(); Shows that the values are correctly returned as variable type string (show by the results below): string(10) "2147483647" string(10) "2147483648" string(10) "2147483649" string(10) "3800001532" string(10) "3900002281" string(10) "4294967295" For the sake of consistency, it would be a good idea for both mysqli->query() and mysqli->prepare() to return the results using the same variable types.[2005-11-09 14:44 UTC] andrey@php.net
[2005-11-10 04:36 UTC] php at pjberkel dot com
[2005-11-21 21:52 UTC] andrey@php.net
[2005-11-30 17:26 UTC] sniper@php.net