|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-09-01 17:22 UTC] jr-php2 at quo dot to
Description:
------------
On x86-64, unpack('V') sign-extends from 32-bit to 64-bit. In other words, it can return a negative number.
Since 'V' specifies an *unsigned* 32-bit value, this is incorrect; the upper 32 bits of the 64-bit result should always be zero.
This behavior makes unpack() inconsistent with other functions like ip2long() and crc32() which never return negative numbers on 64-bit PHP.
Reproduce code:
---------------
$u = unpack('Vresult', chr(200).chr(200).chr(200).chr(200));
echo "unpack = ", $u['result'], "\n";
echo "ip2long = ", ip2long('200.200.200.200'), "\n";
Expected result:
----------------
unpack = -926365496
ip2long = 3368601800
Actual result:
--------------
unpack = 3368601800
ip2long = 3368601800
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 14:00:01 2025 UTC |
This bug also happens with unpack("N") with PHP versions 5.2.2 to 5.2.5 on x64 systems: [shodan@pulsar tmp]$ cat 1.php <?php $s = "\x8a\x4a\xef\x23"; list(,$i) = unpack ( "N*", $s ); var_dump($i); ?> [shodan@pulsar tmp]$ ~/bin/php525 1.php int(-1974800605) Note that it did not happen on 5.1.x series: [shodan@pulsar tmp]$ php 1.php int(2320166691) [shodan@pulsar tmp]$ php --version PHP 5.1.6 (cli) (built: Jun 17 2007 11:37:40) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies [shodan@pulsar tmp]$