From e66c6d252e5707305c521489ef057fe5f500aaa1 Mon Sep 17 00:00:00 2001 From: oittaa Date: Fri, 16 Oct 2015 10:42:27 +0300 Subject: [PATCH 1/6] Updated UtilityTest.php 64bit tests Added failure cases for 64bit RandomCompat_intval tests and changed boundaries to be exactly at PHP_INT_MAX and ~PHP_INT_MAX. --- tests/unit/UtilityTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/unit/UtilityTest.php b/tests/unit/UtilityTest.php index 54da8c3..4327b61 100644 --- a/tests/unit/UtilityTest.php +++ b/tests/unit/UtilityTest.php @@ -56,11 +56,17 @@ public function testIntval() ); if (PHP_INT_SIZE === 8) { + $this->assertFalse( + is_int(RandomCompat_intval("-9223372036854775809", true)) + ); $this->assertTrue( - is_int(RandomCompat_intval("-9223372036854775807", true)) + is_int(RandomCompat_intval("-9223372036854775808", true)) + ); + $this->assertFalse( + is_int(RandomCompat_intval("9223372036854775808", true)) ); $this->assertTrue( - is_int(RandomCompat_intval("9223372036854775806", true)) + is_int(RandomCompat_intval("9223372036854775807", true)) ); } else { $this->assertFalse( From 41f2f14317da18f27c7045afef2fb5b568bde27c Mon Sep 17 00:00:00 2001 From: oittaa Date: Fri, 16 Oct 2015 11:15:21 +0300 Subject: [PATCH 2/6] cast_to_int.php to support more numeric strings For example: random_int("1337e3","1337e3"); --- lib/cast_to_int.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/cast_to_int.php b/lib/cast_to_int.php index f7f1960..474ce64 100644 --- a/lib/cast_to_int.php +++ b/lib/cast_to_int.php @@ -44,10 +44,7 @@ */ function RandomCompat_intval($number, $fail_open = false) { - if ( - is_string($number) && - preg_match('#^\-?[0-9]+\.?[0-9]*$#', $number) - ) { + if (is_numeric($number)) { $number += 0; } if ( @@ -55,11 +52,7 @@ function RandomCompat_intval($number, $fail_open = false) $number > ~PHP_INT_MAX && $number < PHP_INT_MAX ) { - $number = (int) ( - $number < 0 - ? ceil($number) - : floor($number) - ); + $number = (int) $number; } if (is_int($number) || $fail_open) { return $number; From 6bff307652e17d90ea165f35511b0b4947217205 Mon Sep 17 00:00:00 2001 From: oittaa Date: Fri, 16 Oct 2015 11:17:09 +0300 Subject: [PATCH 3/6] RandomIntTest.php: random_int("1337e3","1337e3") --- tests/unit/RandomIntTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/RandomIntTest.php b/tests/unit/RandomIntTest.php index c61f698..f9fc3c3 100644 --- a/tests/unit/RandomIntTest.php +++ b/tests/unit/RandomIntTest.php @@ -19,7 +19,8 @@ public function testOutput() random_int(0.11111, 0.99999), random_int($half_neg_max, PHP_INT_MAX), random_int(0.0, 255.0), - random_int(-4.5, -4.5) + random_int(-4.5, -4.5), + random_int("1337e3","1337e3") ); $this->assertFalse($integers[0] === $integers[1]); @@ -33,6 +34,7 @@ public function testOutput() $this->assertTrue($integers[7] >= $half_neg_max && $integers[7] <= PHP_INT_MAX); $this->assertTrue($integers[8] >= 0 && $integers[8] <= 255); $this->assertTrue($integers[9] === -4); + $this->assertTrue($integers[10] === 1337000); try { $h = random_int("2147483648", "2147483647"); From bdb3abf464b8a4b4f74afe7a252a01247e87b91c Mon Sep 17 00:00:00 2001 From: oittaa Date: Fri, 16 Oct 2015 11:18:55 +0300 Subject: [PATCH 4/6] Updated UtilityTest.php with "1337e3" and "hello" --- tests/unit/UtilityTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/unit/UtilityTest.php b/tests/unit/UtilityTest.php index 4327b61..6692eb5 100644 --- a/tests/unit/UtilityTest.php +++ b/tests/unit/UtilityTest.php @@ -34,6 +34,9 @@ public function testIntval() $this->assertTrue( is_int(RandomCompat_intval(~PHP_INT_MAX + 1, true)) ); + $this->assertTrue( + is_int(RandomCompat_intval("1337e3", true)) + ); // False $this->assertFalse( @@ -54,6 +57,9 @@ public function testIntval() $this->assertFalse( is_int(RandomCompat_intval(PHP_INT_MAX + 0.1, true)) ); + $this->assertFalse( + is_int(RandomCompat_intval("hello", true)) + ); if (PHP_INT_SIZE === 8) { $this->assertFalse( From 52579a36d1e1d7a0452c95e68fb6cfb78b4afe50 Mon Sep 17 00:00:00 2001 From: oittaa Date: Fri, 16 Oct 2015 11:40:13 +0300 Subject: [PATCH 5/6] random_bytes_libsodium.php $buf wasn't initialized --- lib/random_bytes_libsodium.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/random_bytes_libsodium.php b/lib/random_bytes_libsodium.php index 43317d2..796ba6a 100644 --- a/lib/random_bytes_libsodium.php +++ b/lib/random_bytes_libsodium.php @@ -58,6 +58,7 @@ function random_bytes($bytes) * generated in one invocation. */ if ($bytes > 2147483647) { + $buf = ''; for ($i = 0; $i < $bytes; $i += 1073741824) { $n = ($bytes - $i) > 1073741824 ? 1073741824 From df3a673abd0f3c9a146932de19568dcacf0ac1df Mon Sep 17 00:00:00 2001 From: oittaa Date: Fri, 16 Oct 2015 16:59:53 +0300 Subject: [PATCH 6/6] UtilityTest.php: added test case for "1." --- tests/unit/UtilityTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/unit/UtilityTest.php b/tests/unit/UtilityTest.php index 6692eb5..294a801 100644 --- a/tests/unit/UtilityTest.php +++ b/tests/unit/UtilityTest.php @@ -37,6 +37,9 @@ public function testIntval() $this->assertTrue( is_int(RandomCompat_intval("1337e3", true)) ); + $this->assertTrue( + is_int(RandomCompat_intval("1.", true)) + ); // False $this->assertFalse(