#47842 [Opn]: sscanf does not support 64-bit values

[email protected] ("signe at cothlamadh dot net")
Newsgroups php.bugs
Message-ID <[email protected]>
 ID:               47842
 User updated by:  signe at cothlamadh dot net
 Reported By:      signe at cothlamadh dot net
 Status:           Open
 Bug Type:         Unknown/Other Function
 Operating System: FreeBSD 64-bit, Linux 64-bit
 PHP Version:      5.2.9
 New Comment:

Note: sscanf %x and %X have the same behavior as %d and %u


Previous Comments:
------------------------------------------------------------------------

[2009-03-30 21:26:23] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=47842&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.