php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72315 Unpack 'J' & 'P' doesn't produce expected results
Submitted: 2016-06-02 10:56 UTC Modified: 2016-06-02 11:20 UTC
From: ads at tristanleboss dot net Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 7.0.7 OS: Windows 7 x64
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ads at tristanleboss dot net
New email:
PHP Version: OS:

 

 [2016-06-02 10:56 UTC] ads at tristanleboss dot net
Description:
------------
Hello,

While doing some tests, I discovered the 'P' format (unsigned long long (always 64 bit, little endian byte order)) and the 'J' format (unsigned long long (always 64 bit, big endian byte order)) are not giving the expected results on my Windows 7 x64 test machine with PHP 7.0.7RC1.

Indeed, they don't account for the "unsigned" nature and output the signed version.

Indeed:

unpack('P', "\xC0\x00\xF8\x90\x32\x04\x00\x90") will output "-8070445917020880704".

The expected result is "13835331353480265872".

unpack('J', "\xC0\x00\xF8\x90\x32\x04\x00\x90") will output "-4611412720229285744".

The expected result is "10376298156688670912".


No error/warning message will be displayed.




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

var_dump( unpack('P', "\xC0\x00\xF8\x90\x32\x04\x00\x90") );

var_dump( unpack('J', "\xC0\x00\xF8\x90\x32\x04\x00\x90") );


Expected result:
----------------
array(1) {
  [1]=>
  int(13835331353480265872)
}
array(1) {
  [1]=>
  int(10376298156688670912)
}


Actual result:
--------------
array(1) {
  [1]=>
  int(-8070445917020880704)
}
array(1) {
  [1]=>
  int(-4611412720229285744)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-02 10:57 UTC] ads at tristanleboss dot net
-Package: *Mail Related +Package: *Math Functions
 [2016-06-02 10:57 UTC] ads at tristanleboss dot net
Package: Math functions
 [2016-06-02 11:20 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-06-02 11:20 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

There's a yellow Caution note on the docs page that says PHP uses signed ints, so if you unpack() a large enough number you'll get one signed even if you asked for unsigned. And your two numbers (10e18 and 13e18) are beyond the limit of a signed integer (PHP_INT_MAX=9e18).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 12:01:27 2024 UTC