Package: php5
Version: 5.2.6.dfsg.1-1+lenny9

I realise this is an old version, but on looking through the bug
reports and changelogs I see no mention of any similar bugs or actions
to fix them, so I consider it possible that the bug still exists.

json_decode() does not always correctly decode integer values on large
integers such as twitter status IDs. It subtracts 1, or sometimes 2,
from the decoded value. This does not always happen and appears to be
unpredictable.

This is not an overflow error as the integers concerned, though large,
are still below the limit for the 64-bit system on which I discovered
the bug.

Here are some examples of incorrect output, generated using
command-line PHP with sample truncated twitter JSON strings:

Subtracting 1:

<?php
$j='[{"created_at":"Mon Jan 14 01:01:28 +0000 
2013","id":190624640131428353,"id_str":"190624640131428353"}]';
$a=190624640131428353;
$b='190624640131428353';
$c=intval($a);
$d=json_decode($j, true);
$e=json_decode($j, false);
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
int(190624640131428353)
string(18) "190624640131428353"
int(190624640131428353)
array(1) {
  [0]=>
  array(3) {
    ["created_at"]=>
    string(30) "Mon Jan 14 01:01:28 +0000 2013"
    ["id"]=>
    int(190624640131428352)
    ["id_str"]=>
    string(18) "190624640131428353"
  }
}
array(1) {
  [0]=>
  object(stdClass)#1 (3) {
    ["created_at"]=>
    string(30) "Mon Jan 14 01:01:28 +0000 2013"
    ["id"]=>
    int(190624640131428352)
    ["id_str"]=>
    string(18) "190624640131428353"
  }
}


Subtracting 2:

<?php
$j='[{"created_at":"Mon Jan 14 01:01:28 +0000 
2013","id":190624590269513730,"id_str":"190624590269513730"}]';
$a=190624590269513730;
$b='190624590269513730';
$c=intval($a);
$d=json_decode($j, true);
$e=json_decode($j, false);
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
int(190624590269513730)
string(18) "190624590269513730"
int(190624590269513730)
array(1) {
  [0]=>
  array(3) {
    ["created_at"]=>
    string(30) "Mon Jan 14 01:01:28 +0000 2013"
    ["id"]=>
    int(190624590269513728)
    ["id_str"]=>
    string(18) "190624590269513730"
  }
}
array(1) {
  [0]=>
  object(stdClass)#1 (3) {
    ["created_at"]=>
    string(30) "Mon Jan 14 01:01:28 +0000 2013"
    ["id"]=>
    int(190624590269513728)
    ["id_str"]=>
    string(18) "190624590269513730"
  }
}
                                                
By comparison, the (larger) integer 190694799109677056 is handled correctly.

-- 
Pigeon

Be kind to pigeons        - -        Pigeon's Nest: http://pigeonsnest.co.uk/
                          - -      Lucy Pinder TV: http://www.lucy-pinder.tv/
GPG key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x21C61F7F

Attachment: signature.asc
Description: Digital signature

Reply via email to