]> granicus.if.org Git - php/commitdiff
Fixed Bug #63738 unpack: back result with Z format
authorRemi Collet <remi@php.net>
Tue, 11 Dec 2012 15:30:08 +0000 (16:30 +0100)
committerRemi Collet <remi@php.net>
Tue, 11 Dec 2012 15:30:08 +0000 (16:30 +0100)
Fix result for empty string.
Same output as perl
perl -e 'print unpack("Z2","\0\0");' => ""
perl -e 'print unpack("Z2","A\0");'  => "A"
perl -e 'print unpack("Z2","AB\0");' => "AB"
perl -e 'print unpack("Z2","ABC\0");'=> "AB"

ext/standard/pack.c
ext/standard/tests/strings/pack_Z.phpt

index 9894746f7767f6e7ce8e2139f30490191b06621d..0472cb24a9c5089090bc9553d27c21b5ce14683b 100644 (file)
@@ -729,8 +729,7 @@ PHP_FUNCTION(unpack)
                                                size = len;
 
                                                /* Remove everything after the first null */
-                                               s = 0;
-                                               while (s++ <= len) {
+                                               for (s=0 ; s < len ; s++) {
                                                        if (input[inputpos + s] == pad)
                                                                break;
                                                }
index 8a2ee6776765401e87571142b500e66b1bb6a734..4fd007ae0f6abd1ad699e546858b18584367b635 100644 (file)
@@ -9,9 +9,15 @@ var_dump(
     pack("Z4", "foo"),
        pack("Z*", "foo"),
     unpack("Z*", "foo\0\rbar\0 \t\r\n"),
-    unpack("Z9", "foo\0\rbar\0 \t\r\n")
+    unpack("Z9", "foo\0\rbar\0 \t\r\n"),
+    unpack("Z2", "\0"),
+    unpack("Z2", "\0\0"),
+    unpack("Z2", "A\0"),
+    unpack("Z2", "AB\0"),
+    unpack("Z2", "ABC")
 );
 --EXPECTF--
+Warning: unpack(): Type Z: not enough input, need 2, have 1 in %s on line %d
 string(0) ""
 string(5) "foo%c%c"
 string(4) "foo%c"
@@ -25,3 +31,20 @@ array(1) {
   [1]=>
   string(3) "foo"
 }
+bool(false)
+array(1) {
+  [1]=>
+  string(0) ""
+}
+array(1) {
+  [1]=>
+  string(1) "A"
+}
+array(1) {
+  [1]=>
+  string(2) "AB"
+}
+array(1) {
+  [1]=>
+  string(2) "AB"
+}