From 316e7a5b2de6b3bf742e49949b49c45fa69480b0 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Sun, 19 Nov 2006 18:21:50 +0000 Subject: [PATCH] MFB: Fixed bug #38770 (unpack() broken with longs on 64 bit machines). --- ext/standard/pack.c | 16 ++++++++++----- ext/standard/tests/strings/bug38770.phpt | 25 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 ext/standard/tests/strings/bug38770.phpt diff --git a/ext/standard/pack.c b/ext/standard/pack.c index f8498588ae..747037e1e5 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -752,14 +752,16 @@ PHP_FUNCTION(unpack) case 'i': case 'I': { - long v; + long v = 0; int issigned = 0; if (type == 'i') { issigned = input[inputpos + (machine_little_endian ? (sizeof(int) - 1) : 0)] & 0x80; - } + } else if (sizeof(long) > 4 && (input[inputpos + machine_endian_long_map[3]] & 0x80) == 0x80) { + v = ~INT_MAX; + } - v = php_unpack(&input[inputpos], sizeof(int), issigned, int_map); + v |= php_unpack(&input[inputpos], sizeof(int), issigned, int_map); add_assoc_long(return_value, n, v); break; } @@ -770,7 +772,7 @@ PHP_FUNCTION(unpack) case 'V': { int issigned = 0; int *map = machine_endian_long_map; - long v; + long v = 0; if (type == 'l') { issigned = input[inputpos + (machine_little_endian ? 3 : 0)] & 0x80; @@ -780,7 +782,11 @@ PHP_FUNCTION(unpack) map = little_endian_long_map; } - v = php_unpack(&input[inputpos], 4, issigned, map); + if (sizeof(long) > 4 && (input[inputpos + machine_endian_long_map[3]] & 0x80) == 0x80) { + v = ~INT_MAX; + } + + v |= php_unpack(&input[inputpos], 4, issigned, map); add_assoc_long(return_value, n, v); break; } diff --git a/ext/standard/tests/strings/bug38770.phpt b/ext/standard/tests/strings/bug38770.phpt new file mode 100644 index 0000000000..2494a911bd --- /dev/null +++ b/ext/standard/tests/strings/bug38770.phpt @@ -0,0 +1,25 @@ +--TEST-- +Bug #38770 (unpack() broken with longs on 64 bit machines) +--FILE-- + +--EXPECT-- +Array +( + [1] => -30000 +) +Array +( + [1] => -30000 +) +Array +( + [1] => -30000 +) +Done -- 2.40.0