From 7ad9eade2d2eb02a8cc2d1049d64d537049010aa Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 8 Jan 2016 09:46:45 -0600 Subject: [PATCH] BigInteger: fix issue with doing bitwise not on 0 --- phpseclib/Math/BigInteger.php | 3 +++ tests/Unit/Math/BigInteger/TestCase.php | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 98ec4a93..0a9a2915 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -2922,6 +2922,9 @@ class Math_BigInteger // calculuate "not" without regard to $this->precision // (will always result in a smaller number. ie. ~1 isn't 1111 1110 - it's 0) $temp = $this->toBytes(); + if ($temp == '') { + return ''; + } $pre_msb = decbin(ord($temp[0])); $temp = ~$temp; $msb = decbin(ord($temp[0])); diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index 7f7473b9..b42c0ca6 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -220,6 +220,11 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $z = $this->getInstance('11111111111111111111111', 16); $this->assertSame($z->toHex(), $x->bitwise_NOT()->toHex()); + + $a = $this->getInstance(0); + $a->bitwise_not(); + + $this->assertSame($a->toString(), '0'); } public function testBitwiseLeftShift()