php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47842 sscanf() does not support 64-bit values
Submitted: 2009-03-30 21:26 UTC Modified: 2010-05-19 13:28 UTC
From: signe at cothlamadh dot net Assigned: mike (profile)
Status: Closed Package: Strings related
PHP Version: 5.2.9 OS: * (64bit)
Private report: No CVE-ID: None
 [2009-03-30 21:26 UTC] signe at cothlamadh dot net
Description:
------------
On 64-bit operating systems, printf()'s %d and %u formatting codes support 64-bit integers.   sscanf, which supposedly uses the same code (and has the same expected behavior) does not support 64-bit values.

Also, printf is not outputting accurate values when it reaches numbers near the upper bounds of a 64-bit int.

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

sscanf("2147483647", '%d', $int);
echo "sscanf 32-bit signed int '2147483647'           (2^31)-1 = ",$int,"\n";
sscanf("4294967295", '%u', $int);
echo "sscanf 32-bit unsign int '4294967295'           (2^32)-1 = ",$int,"\n";

sscanf("9223372036854775807", '%d', $int);
echo "sscanf 64-bit signed int '9223372036854775807'  (2^63)-1 = ",$int,"\n";
sscanf("18446744073709551615", '%u', $int);
echo "sscanf 64-bit unsign int '18446744073709551615' (2^64)-1 = ",$int,"\n";

printf("printf 64-bit signed int '9223372036854775807'  (2^63)-1 = %d\n", 9223372036854775807);
printf("printf 64-bit signed int '18446744073709551615' (2^64)-1 = %u\n", 18446744073709551615);


echo "\n(2^64)-1 - 100,000,000\n";
printf("printf 64-bit signed int '18446744073609551615' = %u\n", 18446744073609551615);
echo "Output is 257 greater than input\n";
?>

Expected result:
----------------
sscanf 32-bit signed int '2147483647'           (2^31)-1 = 2147483647
sscanf 32-bit unsign int '4294967295'           (2^32)-1 = 4294967295
sscanf 64-bit signed int '9223372036854775807'  (2^63)-1 = 9223372036854775807
sscanf 64-bit unsign int '18446744073709551615' (2^64)-1 = 18446744073709551615
printf 64-bit signed int '9223372036854775807'  (2^63)-1 = 9223372036854775807
printf 64-bit signed int '18446744073709551615' (2^64)-1 = 18446744073709551615

Actual result:
--------------
sscanf 32-bit signed int '2147483647'           (2^31)-1 = 2147483647
sscanf 32-bit unsign int '4294967295'           (2^32)-1 = 4294967295
sscanf 64-bit signed int '9223372036854775807'  (2^63)-1 = -1
sscanf 64-bit unsign int '18446744073709551615' (2^64)-1 = 4294967295
printf 64-bit signed int '9223372036854775807'  (2^63)-1 = 9223372036854775807
printf 64-bit signed int '18446744073709551615' (2^64)-1 = 0


printf 64-bit signed int '18446744073609551615' ((2^64)-1 - 100000000) = 18446744073609551872
Output is 257 greater than input

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-30 22:09 UTC] signe at cothlamadh dot net
Note: sscanf %x and %X have the same behavior as %d and %u
 [2010-05-19 13:28 UTC] mike@php.net
Automatic comment from SVN on behalf of mike
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=299484
Log: * fixed bug #47842      sscanf() does not support 64-bit values
 [2010-05-19 13:28 UTC] mike@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: mike
 [2010-05-19 13:28 UTC] mike@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2013-06-05 17:04 UTC] iam4webwork at hotmail dot com
Yes, but the issue mentioned with printf still remains open, ie:

printf("printf 64-bit signed int '18446744073709551615' (2^64)-1 = %u\n", 
18446744073709551615); 

see http://3v4l.org/ZeAYt
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 07:01:29 2024 UTC