Bug #61038 [Opn->Nab]: unpack("a5", "str\0") does not work as expected

From: Date: Fri, 10 Feb 2012 16:34:10 +0000
Subject: Bug #61038 [Opn->Nab]: unpack("a5", "str\0") does not work as expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-167391@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61038&edit=1

 ID:                 61038
 Updated by:         cataphract@php.net
 Reported by:        su dot hang at yahoo dot com
 Summary:            unpack("a5", "str\0") does not work as expected
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Linux (3.0.0-15-generic)
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2012-02-10 09:20:26] 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"
}


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61038&edit=1


Thread (6 messages)

« previous php.bugs (#167391) next »