php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68225 unpack and X format code
Submitted: 2014-10-14 10:22 UTC Modified: 2014-10-14 10:30 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mberchtold at gmail dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: master-Git-2014-10-14 (snap) OS: Windows 64-bit
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mberchtold at gmail dot com
New email:
PHP Version: OS:

 

 [2014-10-14 10:22 UTC] mberchtold at gmail dot com
Description:
------------
unpack with the X format code is broken.

There are 2 related issues:

1. Unexpected warning
PHP Warning:  unpack(): Type X: outside of string

The bug is here:
File: php-src / ext / standard / pack.c

In the following snipped size is -1 for the X format code, therefore the first condition if (inputpos < size) is always true.

case 'X':
						if (inputpos < size) {
							inputpos = -size;
							i = arg - 1;		/* Break out of for loop */

							if (arg >= 0) {
								php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: outside of string", type);
							}
						}
						break;

2. unpack returns false and additional warning:
HP Warning:  unpack(): Type X: not enough input, need -1, have 0


Test script:
---------------
<?php

$data = pack('VV', 1, 2);

$result = unpack('Va/X' ,$data);
// PHP Warning:  unpack(): Type X: outside of string

$result = unpack('Va/X4' ,$data);
// PHP Warning:  unpack(): Type X: outside of string

$result = unpack('V1a/X4/V1b/V1c/X4/V1d', $data);
// PHP Warning:  unpack(): Type X: outside of string
// PHP Warning:  unpack(): Type X: not enough input, need -1, have 0
// unpack returns false

var_dump($result);
?>

Expected result:
----------------
No warnings.
No failures

Actual result:
--------------
Warnings:
PHP Warning:  unpack(): Type X: outside of string
PHP Warning:  unpack(): Type X: not enough input, need -1, have 0

And wrong result.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-14 10:30 UTC] mberchtold at gmail dot com
Please disregard the comment regarding: "if (inputpos < size)"

I also would like to note that this happens with PHP 64-bit NTS.
 [2014-11-28 09:23 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e7a6c15b93ab35bb148fa2ab31e905bb9a3afb51
Log: Fixed bug #68225 unpack and X format code
 [2014-11-28 09:23 UTC] ab@php.net
-Status: Open +Status: Closed
 [2014-12-05 07:00 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e7a6c15b93ab35bb148fa2ab31e905bb9a3afb51
Log: Fixed bug #68225 unpack and X format code
 [2016-07-20 11:40 UTC] davey@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e7a6c15b93ab35bb148fa2ab31e905bb9a3afb51
Log: Fixed bug #68225 unpack and X format code
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Sep 28 13:01:27 2024 UTC