Skip to content

Commit

Permalink
Added length to invariant message in flib/core/hack/lib/private.php::…
Browse files Browse the repository at this point in the history
…validate_offset

Summary: Improved ability to debug code when an invariant isn't met by outputting all relevant values, not just 1.

Reviewed By: kmeht, viratyosin

Differential Revision: D26350710

fbshipit-source-id: 548e8d4ca15eacd0cf0ade9f9bf47accaa191686
  • Loading branch information
jarredsimmer-fb authored and facebook-github-bot committed Feb 10, 2021
1 parent 4e81237 commit e594954
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/private.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
* offset as a positive integer.
*/
<<__Pure>>
function validate_offset(
int $offset,
int $length,
)[]: int {
function validate_offset(int $offset, int $length)[]: int {
$original_offset = $offset;
if ($offset < 0) {
$offset += $length;
}
invariant(
$offset >= 0 && $offset <= $length,
'Offset (%d) was out-of-bounds.',
'Offset %d was out-of-bounds for length %d',
$original_offset,
$length,
);
return $offset;
}
Expand All @@ -36,15 +34,17 @@ function validate_offset(
* offset as a positive integer.
*/
<<__Pure>>
function validate_offset_lower_bound(
int $offset,
int $length,
)[]: int {
function validate_offset_lower_bound(int $offset, int $length)[]: int {
$original_offset = $offset;
if ($offset < 0) {
$offset += $length;
}
invariant($offset >= 0, 'Offset (%d) was out-of-bounds.', $original_offset);
invariant(
$offset >= 0,
'Offset %d was out-of-bounds for length %d',
$original_offset,
$length,
);
return $offset;
}

Expand Down

0 comments on commit e594954

Please sign in to comment.