|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-03-04 16:43 UTC] laacz at laacz dot lv
Description:
------------
Unpacking unsigned long (32bit; always big endian; "N") on 64bit
system returns 64bit signed int instead of 32bit.
You can do & 0xffffffff on unpacked value, and get desired result,
but that's still a bug.
Reproduce code:
---------------
<?php
list(,$command_id) = unpack('N', chr(0x80) . chr(0x00) . chr(0x00) . chr(0x09));
echo hexdec(dechex($command_id)) . "\n0x" . dechex($command_id) . "\n";
?>
Expected result:
----------------
2147483657
0x80000009
Actual result:
--------------
1.8446744071562E+19
0xffffffff80000009
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 08:00:01 2025 UTC |
Hi, Generally the Zend engine holds any variable as zval which holds the integer as long data type only. 32 bit Platform: long => 4 byte = 32 bit 64 bit Platform: long => 8 byte = 64 bit 64 bit Platform: unsigned int => 4 byte = 32 bit Hence in the 64 bit platform, we have to convert from long to unsigned int only in 64 bit Platform. Hence in php source php5.3-200903301230/ext/standard/pack.c In PHP_FUNCTION(unpack) place, add this convertion immediate after php_unpack function as folows. v |= php_unpack(&input[inputpos], 4, issigned, map); if (sizeof(long) > 4) { v = (unsigned int) v; } I tested in both 32 & 64 bit platform ,it works fine , please close this Bug Regards, vivekanandanHi, Regarding the bug fix, This is tested in Version PHP 5.3 and works fine,for any one needs full source code : http://www.gnudeveloper.com/software/php/pack.c Regards, vivekanandan.Hi, for signed long also work properly,please use code below. v |= php_unpack(&input[inputpos], 4, issigned, map); if (sizeof(long) > 4) { if( type == 'l' ) { v = (signed int) v; }else{ v = (unsigned int) v; } } Full Source Code: http://www.gnudeveloper.com/software/php-5.3/pack.c Regards, vivekanandan.