| Bug #40749 | pack and unpack erroneous behavior on 64bits hosts | ||||
|---|---|---|---|---|---|
| Submitted: | 7 Mar 2007 5:12pm UTC | Modified: | 5 Apr 2007 7:59pm UTC | ||
| From: | ben at ateor dot com | Assigned to: | iliaa | ||
| Status: | Closed | Category: | Unknown/Other Function | ||
| Version: | 5.2.1 | OS: | OpenBSD amd64 and sparc64 | ||
| Votes: | 19 | Avg. Score: | 4.8 ± 0.4 | Reproduced: | 18 of 18 (100.0%) |
| Same Version: | 18 (100.0%) | Same OS: | 6 (33.3%) | ||
[7 Mar 2007 5:12pm UTC] ben at ateor dot com
[9 Mar 2007 9:06am UTC] windeler at mediafinanz dot de
Here is another example of a problem with unpack on 64bit systems. It
worked in 5.1.6, but with 5.2.1 the results are bogus.
The expected value from the file content is 200, but PHP says
-2147483448 when I echo $a['i'].
<?php
$f = fopen('test.pdf','rb');
//Read a 4-byte integer from file
$a = unpack('Ni',fread($f,4));
echo $a['i'];
fclose($f);
?>
[14 Mar 2007 8:57pm UTC] pz at mysqlperformanceblog dot com
In any case if you call it bug or a feature this is serious behavior change for something which a lot of people could be depending on. It breaks in MySQL 5.2.0 -> 5.2.1 which is minor version upgrade.
[16 Mar 2007 2:03pm UTC] martin at netimage dot dk
It appears that the sign bit is taken from LSB instead of MSB
> php -r 'print_r( unpack('N',pack('N',127)));'
Array
(
[1] => 127
)
> php -r 'print_r( unpack('N',pack('N',128)));'
Array
(
[1] => -2147483520
)
The last number is 2's complement of -128 for 32 bit integers
Cheers
[3 Apr 2007 10:00pm UTC] iliaa@php.net
Please try using this CVS snapshot: http://snaps.php.net/php5.2-latest.tar.gz For Windows: http://snaps.php.net/win32/php5.2-win32-latest.zip
[5 Apr 2007 1:49pm UTC] ben at ateor dot com
Fix the problem for me, thanks. (ps: meanwhile, I can't verify there's no regressions, given how the testsuite has changed).
