Bug->Doc #52729 [Opn]: unpack() format I, L, N and V returns negative value

From: Date: Sun, 29 Aug 2010 23:46:10 +0000
Subject: Bug->Doc #52729 [Opn]: unpack() format I, L, N and V returns negative value
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-4947@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52729&edit=1 ID: 52729 Updated by: cataphract@php.net Reported by: hiroaki dot kawai at gmail dot com Summary: unpack() format I, L, N and V returns negative value Status: Open -Type: Bug +Type: Documentation Problem Package: Unknown/Other Function Operating System: Linux PHP Version: 5.3.3 Block user comment: N New Comment: The fact that the 4294967295 integer literal results in a float is irrelevant here. What's relevant is what the documentation for pack says: > Also note that PHP internally stores integer values as signed values of a machine-dependent size. If you give it an unsigned integer value too large to be stored that way it is converted to a float which often yields an undesired result. So it does support your expected behavior. However, I'd call this a documentation bug. It doesn't make sense to unpack an integer into a float; unpack/pack wouldn't be inverse operations anymore. The current implementation still distinguishes between unsigned or signed. Since PHP uses signed longs to store integers, if the size of these longs is larger than the size of the value you're unpacking, the sign bit needs to be moved from its position in the packed value to the correct position in unpacked value. Changing this to the documented behavior would also cause probably cause a lot of breakage. Previous Comments: ------------------------------------------------------------------------ [2010-08-29 10:22:08] hiroaki dot kawai at gmail dot com Description: ------------ According to the documentation, all I, L, N and V formats are defined to be unsigned integer. On my Linux(32bit intel), unpacking FFFFFF returns -1. As you can see the test script, we automatically use float for large integer. So the result should be float(4294967295), otherwise we get wrong number of -1. Test script: --------------- <?php var_dump(unpack("I","\xFF\xFF\xFF\xFF")); var_dump(unpack("L","\xFF\xFF\xFF\xFF")); var_dump(unpack("N","\xFF\xFF\xFF\xFF")); var_dump(unpack("V","\xFF\xFF\xFF\xFF")); var_dump(PHP_INT_MAX); var_dump(PHP_INT_SIZE); var_dump(4294967295); Expected result: ---------------- array(1) { [1]=> float(4294967295) } array(1) { [1]=> float(4294967295) } array(1) { [1]=> float(4294967295) } array(1) { [1]=> float(4294967295) } int(2147483647) int(4) float(4294967295) Actual result: -------------- array(1) { [1]=> int(-1) } array(1) { [1]=> int(-1) } array(1) { [1]=> int(-1) } array(1) { [1]=> int(-1) } int(2147483647) int(4) float(4294967295) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52729&edit=1

« previous php.doc.bugs (#4947) next »