Skip to content

Commit 524ce90

Browse files
committed
fix GH-7899 Regression in unpack for negative int value
1 parent 7e6558e commit 524ce90

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ PHP NEWS
1616
- pcntl:
1717
. Fixed pcntl_rfork build for DragonFlyBSD. (David Carlier)
1818

19+
- Standard:
20+
. Fixed bug GH-7899 (Regression in unpack for negative int value). (Remi)
21+
1922
06 Jan 2022, PHP 8.1.2RC1
2023

2124
- Core:

ext/standard/pack.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef ZEND_SET_ALIGNED(1, uint16_t unaligned_uint16_t);
6262
typedef ZEND_SET_ALIGNED(1, uint32_t unaligned_uint32_t);
6363
typedef ZEND_SET_ALIGNED(1, uint64_t unaligned_uint64_t);
6464
typedef ZEND_SET_ALIGNED(1, unsigned int unaligned_uint);
65+
typedef ZEND_SET_ALIGNED(1, int unaligned_int);
6566

6667
/* Mapping of byte from char (8bit) to long for machine endian */
6768
static int byte_map[1];
@@ -1043,8 +1044,14 @@ PHP_FUNCTION(unpack)
10431044

10441045
case 'i': /* signed integer, machine size, machine endian */
10451046
case 'I': { /* unsigned integer, machine size, machine endian */
1046-
unsigned int x = *((unaligned_uint*) &input[inputpos]);
1047-
zend_long v = (type == 'i') ? (int) x : x;
1047+
zend_long v;
1048+
if (type == 'i') {
1049+
int x = *((unaligned_int*) &input[inputpos]);
1050+
v = x;
1051+
} else {
1052+
unsigned int x = *((unaligned_uint*) &input[inputpos]);
1053+
v = x;
1054+
}
10481055

10491056
ZVAL_LONG(&val, v);
10501057
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val);

ext/standard/tests/strings/pack64.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ print_r(unpack("q", pack("q", 0)));
3131
print_r(unpack("q", pack("q", 0x8000000000000002)));
3232
print_r(unpack("q", pack("q", -1)));
3333
print_r(unpack("q", pack("q", 0x8000000000000000)));
34+
35+
print_r(unpack("i", pack("i", 2147483647))); // Max int32
36+
print_r(unpack("i", pack("i", -2147483647)));
37+
print_r(unpack("i", pack("i", -2147483648))); // Min int32
38+
print_r(unpack("I", pack("I", 4294967295))); // Max uint32
3439
?>
3540
--EXPECT--
3641
Array
@@ -113,3 +118,19 @@ Array
113118
(
114119
[1] => -9223372036854775808
115120
)
121+
Array
122+
(
123+
[1] => 2147483647
124+
)
125+
Array
126+
(
127+
[1] => -2147483647
128+
)
129+
Array
130+
(
131+
[1] => -2147483648
132+
)
133+
Array
134+
(
135+
[1] => 4294967295
136+
)

0 commit comments

Comments
 (0)