php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61038 unpack("a5", "str\0\0") does not work as expected
Submitted: 2012-02-10 09:20 UTC Modified: 2012-04-17 21:44 UTC
From: su dot hang at yahoo dot com Assigned: cataphract
Status: Closed Package: *General Issues
PHP Version: Irrelevant OS: Linux (3.0.0-15-generic)
Private report: No CVE-ID:
 [2012-02-10 09:20 UTC] su dot hang at yahoo dot com
Description:
------------
related bug: https://bugs.php.net/bug.php?id=39588

if unpacking from a binary string ending with any number of "\0"'s, and the unpack string length is given, the resulted string does not have the right length. 

for example, unpack("a5", "str\0") should result in a 5-byte-long string, but it returns a 3-byte-long instead. 

Test script:
---------------
<?php
$var = "str";
$packed = pack("a5", $var); // 5 bytes long NULL-padded string
var_dump(unpack("a5", $packed));


Expected result:
----------------
array(1) {
  [1]=>
  string(5) "str\0\0"
}

Actual result:
--------------
array(1) {
  [1]=>
  string(3) "str"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-10 16:34 UTC] cataphract@php.net
I don't see anything wrong here. 

unpack("a5", "foo\0\0")

removes the NULL padding from the input string. The 5 specifies only the size of the input to consume:

php > var_dump(unpack("a5", "foo\0"));
PHP Warning:  unpack(): Type a: not enough input, need 5, have 4 in php shell code on line 1
 [2012-02-10 16:34 UTC] cataphract@php.net
-Status: Open +Status: Not a bug
 [2012-02-10 16:53 UTC] su dot hang at yahoo dot com
sorry, please use the test script instead of the subject.

It is a bug because it is against the spec that "a" is an arbitrary binary string. 
If specified length is 5 then it should be null-padded at the end, rather than being 
truncated.

try unpack('a5', "\x12\x34\x00\x56\x00").
at least, to be consistent, the result string should terminate at the first "\0", 
instead of the trailing one.

btw, perl is handling it correctly.
 [2012-02-10 16:53 UTC] su dot hang at yahoo dot com
-Summary: unpack("a5", "str\0") does not work as expected +Summary: unpack("a5", "str\0\0") does not work as expected
 [2012-02-13 08:39 UTC] cataphract@php.net
I'm not sure I understand. Is the problem that 'a' (as opposed to 'A') on unpack should leave the string as is, as is the case with perl?
 [2012-02-13 08:39 UTC] cataphract@php.net
-Status: Not a bug +Status: Open
 [2012-02-13 09:50 UTC] su dot hang at yahoo dot com
yes, that's right. 

"a", as in the spec, represents arbitrary binary string, with null-padding. so, if the specified unpack length is fixed, such as a constant number (like in "unpack('a10')"), then the unpacked string from "a" should have that length (unless, of course, if the input is insufficient). 

since "a" is arbitrary binary string, if it has several "\0"s at the end, php should not remove them as if it was a regular null-terminated string.
 [2012-04-17 21:23 UTC] cataphract@php.net
Automatic comment on behalf of googleguy@googleguy-virtualbox.(none)
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4968fa644b0849321e1761e52b8db15dd46f9b75
Log: Fixed bug #61038; &quot;Z&quot; and better behavior for unpack()
 [2012-04-17 21:44 UTC] cataphract@php.net
Fixed in master only due to BC concerns.

Thank you.
 [2012-04-17 21:44 UTC] cataphract@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cataphract
 [2012-04-18 09:44 UTC] laruence@php.net
Automatic comment on behalf of googleguy@googleguy-virtualbox.(none)
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4968fa644b0849321e1761e52b8db15dd46f9b75
Log: Fixed bug #61038; &quot;Z&quot; and better behavior for unpack()
 [2012-07-24 23:36 UTC] rasmus@php.net
Automatic comment on behalf of googleguy@googleguy-virtualbox.(none)
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4968fa644b0849321e1761e52b8db15dd46f9b75
Log: Fixed bug #61038; &quot;Z&quot; and better behavior for unpack()
 [2013-11-17 09:32 UTC] laruence@php.net
Automatic comment on behalf of googleguy@googleguy-virtualbox.(none)
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4968fa644b0849321e1761e52b8db15dd46f9b75
Log: Fixed bug #61038; &quot;Z&quot; and better behavior for unpack()
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 10:02:09 2014 UTC