php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49991 json_decode integer truncation on 32-bit machines
Submitted: 2009-10-25 13:33 UTC Modified: 2009-10-26 00:09 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: warren at guy dot net dot au Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.2.11 OS: FreeBSD 6.3
Private report: No CVE-ID: None
 [2009-10-25 13:33 UTC] warren at guy dot net dot au
Description:
------------
json_decode truncates numbers greater than 32 bit int 2147483647 on 32 bit system

Reproduce code:
---------------
<?

$json_encoded = '{"test_id_small_int":12345,"test_id_maxint":2147483647,"test_id_maxint_plus_two":2147483649,"test_id_huge_int":80000000000}';

var_dump(json_decode($json_encoded));

?>


Expected result:
----------------
object(stdClass)#1 (4) {
  ["test_id_small_int"]=>
  int(12345)
  ["test_id_maxint"]=>
  int(2147483647)
  ["test_id_maxint_plus_two"]=>
  float(2147483649)
  ["test_id_huge_int"]=>
  float(80000000000)
}


or even strings instead of floats would work for me.

Actual result:
--------------
object(stdClass)#1 (4) {
  ["test_id_small_int"]=>
  int(12345)
  ["test_id_maxint"]=>
  int(2147483647)
  ["test_id_maxint_plus_two"]=>
  int(2147483647)
  ["test_id_huge_int"]=>
  int(2147483647)
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-25 16:00 UTC] mattwil@php.net
Are you cross-compiling for 32-bit, on a 64-bit system...?
 [2009-10-25 16:05 UTC] warren at guy dot net dot au
Nope, built on same machine it's running. 32 bit machine and OS.

FreeBSD [redacted] 6.3-RELEASE-p11 FreeBSD 6.3-RELEASE-p11 #0: Wed Jun 10 02:25:37 UTC 2009     root@i386-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  i386
 [2009-10-25 20:19 UTC] jani@php.net
How exactly would it work when max int size on 32bit is 2147483647 ? 
Expected behaviour -> bogus.
 [2009-10-25 22:55 UTC] shupp@php.net
FWIW, it works properly on my 32 bit linux machine (debian lenny):

$ php -v
PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 26 2009 22:16:23) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
 [2009-10-26 00:07 UTC] warren at guy dot net dot au
I am not asking json_decode to represent a number as an integer, nor any specific type. It is detecting a numeric value and turning it in to an integer for me. It seems that if it is able to detect that it is a numeric value, perhaps it should also be aware that the numeric value is greater than the max value of the type it is about to be assigned and use a more appropriate type instead?

It seems counter intuitive that JSON numeric input on a 32 bit machine should be limited to a 32 bit integer. If for some reason this limitation is required, it should be obviously documented (it is not).

Side note: if I add '.0' to the end of a number, it becomes a float and can represent numbers greater than 2147483647.
 [2009-10-26 00:09 UTC] warren at guy dot net dot au
Additionally, from a comment on http://php.net/manual/en/function.json-decode.php:

> Nick Telford
> 25-Jun-2009 11:06
> In PHP <= 5.1.6 trying to decode an integer value that's > PHP_INT_MAX 
> will result in an intger of PHP_INT_MAX.
>
> In PHP 5.2+ decoding an integer > PHP_INT_MAX will cause a conversion
> to a float.

which might implie that the expected behavior is not the behavior that is being exhibited here.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:22 2019 UTC