fix GH-7899 Regression in unpack for negative int value

This commit is contained in:
Remi Collet 2022-01-07 15:04:36 +01:00 committed by Remi Collet
parent 7e6558edf1
commit 524ce90418
No known key found for this signature in database
GPG key ID: DC9FF8D3EE5AF27F
3 changed files with 33 additions and 2 deletions

3
NEWS
View file

@ -16,6 +16,9 @@ PHP NEWS
- pcntl: - pcntl:
. Fixed pcntl_rfork build for DragonFlyBSD. (David Carlier) . Fixed pcntl_rfork build for DragonFlyBSD. (David Carlier)
- Standard:
. Fixed bug GH-7899 (Regression in unpack for negative int value). (Remi)
06 Jan 2022, PHP 8.1.2RC1 06 Jan 2022, PHP 8.1.2RC1
- Core: - Core:

View file

@ -62,6 +62,7 @@ typedef ZEND_SET_ALIGNED(1, uint16_t unaligned_uint16_t);
typedef ZEND_SET_ALIGNED(1, uint32_t unaligned_uint32_t); typedef ZEND_SET_ALIGNED(1, uint32_t unaligned_uint32_t);
typedef ZEND_SET_ALIGNED(1, uint64_t unaligned_uint64_t); typedef ZEND_SET_ALIGNED(1, uint64_t unaligned_uint64_t);
typedef ZEND_SET_ALIGNED(1, unsigned int unaligned_uint); typedef ZEND_SET_ALIGNED(1, unsigned int unaligned_uint);
typedef ZEND_SET_ALIGNED(1, int unaligned_int);
/* Mapping of byte from char (8bit) to long for machine endian */ /* Mapping of byte from char (8bit) to long for machine endian */
static int byte_map[1]; static int byte_map[1];
@ -1043,8 +1044,14 @@ PHP_FUNCTION(unpack)
case 'i': /* signed integer, machine size, machine endian */ case 'i': /* signed integer, machine size, machine endian */
case 'I': { /* unsigned integer, machine size, machine endian */ case 'I': { /* unsigned integer, machine size, machine endian */
unsigned int x = *((unaligned_uint*) &input[inputpos]); zend_long v;
zend_long v = (type == 'i') ? (int) x : x; if (type == 'i') {
int x = *((unaligned_int*) &input[inputpos]);
v = x;
} else {
unsigned int x = *((unaligned_uint*) &input[inputpos]);
v = x;
}
ZVAL_LONG(&val, v); ZVAL_LONG(&val, v);
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val);

View file

@ -31,6 +31,11 @@ print_r(unpack("q", pack("q", 0)));
print_r(unpack("q", pack("q", 0x8000000000000002))); print_r(unpack("q", pack("q", 0x8000000000000002)));
print_r(unpack("q", pack("q", -1))); print_r(unpack("q", pack("q", -1)));
print_r(unpack("q", pack("q", 0x8000000000000000))); print_r(unpack("q", pack("q", 0x8000000000000000)));
print_r(unpack("i", pack("i", 2147483647))); // Max int32
print_r(unpack("i", pack("i", -2147483647)));
print_r(unpack("i", pack("i", -2147483648))); // Min int32
print_r(unpack("I", pack("I", 4294967295))); // Max uint32
?> ?>
--EXPECT-- --EXPECT--
Array Array
@ -113,3 +118,19 @@ Array
( (
[1] => -9223372036854775808 [1] => -9223372036854775808
) )
Array
(
[1] => 2147483647
)
Array
(
[1] => -2147483647
)
Array
(
[1] => -2147483648
)
Array
(
[1] => 4294967295
)