php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73333 2147483647 is fetched as string
Submitted: 2016-10-17 14:20 UTC Modified: 2016-10-17 14:20 UTC
From: cmb@php.net Assigned: cmb (profile)
Status: Closed Package: SQLite related
PHP Version: 5.6.27 OS: *
Private report: No CVE-ID: None
 [2016-10-17 14:20 UTC] cmb@php.net
Description:
------------
ext/sqlite3 fetches integers greater than or equal to 2147483647
as strings. Even for 32bit PHP 2147483647 should be returned as
int; for 64bit PHP the supported range should be larger, of
course.

The culprit is sqlite_value_to_zval()[1] where comparisons are
made for >= INT_MAX (and <= INT_MIN), what should actually be
> ZEND_LONG_MAX (and < ZEND_LONG_MIN).

[1] <https://github.com/php/php-src/blob/PHP-5.6.27/ext/sqlite3/sqlite3.c#L597>


Test script:
---------------
<?php

$db = new SQLite3(':memory:');
$db->exec('CREATE TABLE foo (bar INT)');
$stmt = $db->prepare('INSERT INTO foo VALUES (?)');
foreach ([-2147483648, -2147483647, 2147483646, 2147483647] as $num) {
    $stmt->bindValue(1, $num, SQLITE3_INTEGER);
    $stmt->execute();
}
$res = $db->query('SELECT bar FROM foo');
while (($row = $res->fetchArray(SQLITE3_ASSOC)) !== false) {
    var_dump($row);
}


Expected result:
----------------
array(1) {
  ["bar"]=>
  int(-2147483648)
}
array(1) {
  ["bar"]=>
  int(-2147483647)
}
array(1) {
  ["bar"]=>
  int(2147483646)
}
array(1) {
  ["bar"]=>
  int(2147483647)
}

Actual result:
--------------
array(1) {
  ["bar"]=>
  string(11) "-2147483648"
}
array(1) {
  ["bar"]=>
  int(-2147483647)
}
array(1) {
  ["bar"]=>
  int(2147483646)
}
array(1) {
  ["bar"]=>
  string(10) "2147483647"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-17 14:20 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2016-10-17 22:02 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=86e603a664afdc3a12ead0eaca5d37fa8a379381
Log: Fix #73333: 2147483647 is fetched as string
 [2016-10-17 22:02 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2016-10-25 15:21 UTC] krakjoe@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=86e603a664afdc3a12ead0eaca5d37fa8a379381
Log: Fix #73333: 2147483647 is fetched as string
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 07:01:31 2024 UTC