Skip to content

Commit

Permalink
Beginning a version 2.0.8: More psalm fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Mar 3, 2017
1 parent 42c21a5 commit 748221e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version 2.0.9 - 2017-03-03

* More Psalm integration fixes.

### Version 2.0.8 - 2017-03-03

* Prevent function already declared error for `random_int()` caused by misusing
Expand Down
15 changes: 7 additions & 8 deletions lib/cast_to_int.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
* @param int|float $number The number we want to convert to an int
* @param boolean $fail_open Set to true to not throw an exception
*
* @return int|float
* @return float|int
*
* @throws TypeError
*/
function RandomCompat_intval($number, $fail_open = false)
{
if (is_numeric($number)) {
if (is_int($number) || is_float($number)) {
$number += 0;
}

Expand All @@ -62,12 +62,11 @@ function RandomCompat_intval($number, $fail_open = false)

if (is_int($number)) {
return (int) $number;
} elseif ($fail_open) {
return (float) $number;
} elseif (!$fail_open) {
throw new TypeError(
'Expected an integer.'
);
}

throw new TypeError(
'Expected an integer.'
);
return $number;
}
}
8 changes: 6 additions & 2 deletions lib/random_bytes_libsodium_legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,24 @@ function random_bytes($bytes)
);
}

/**
* @var string
*/
$buf = '';

/**
* \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
* generated in one invocation.
*/
if ($bytes > 2147483647) {
$buf = '';
for ($i = 0; $i < $bytes; $i += 1073741824) {
$n = ($bytes - $i) > 1073741824
? 1073741824
: $bytes - $i;
$buf .= Sodium::randombytes_buf($n);
}
} else {
$buf = Sodium::randombytes_buf($bytes);
$buf .= Sodium::randombytes_buf($bytes);
}

if (is_string($buf)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/random_int.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function random_int($min, $max)
$valueShift = $min;
}

$val = 0;
/**
* Now that we have our parameters set up, let's begin generating
* random integers until one falls between $min and $max
Expand Down Expand Up @@ -164,7 +165,7 @@ function random_int($min, $max)
* 159 + 27904 + 3276800 + 201326592 =>
* 204631455
*/
$val = 0;
$val &= 0;
for ($i = 0; $i < $bytes; ++$i) {
$val |= ord($randomByteString[$i]) << ($i * 8);
}
Expand Down
9 changes: 1 addition & 8 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
<directory name="lib" />
</projectFiles>
<issueHandlers>
<DuplicateClass errorLevel="info" />
<FailedTypeResolution errorLevel="info" />
<UndefinedConstant errorLevel="info" />
<InvalidArgument errorLevel="info" />
<InvalidOperand errorLevel="info" />
<InvalidPropertyAssignment errorLevel="info" />
<TypeCoercion errorLevel="info" />
<TypeDoesNotContainType errorLevel="info" />
<PossiblyUndefinedVariable errorLevel="info" />
<MissingReturnType errorLevel="info" />
</issueHandlers>
</psalm>

0 comments on commit 748221e

Please sign in to comment.