Skip to content

Commit

Permalink
Update substring range checking
Browse files Browse the repository at this point in the history
  • Loading branch information
eqrion committed Jan 16, 2024
1 parent 42143e4 commit 206a3a0
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions proposals/js-string-builtins/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,15 @@ func substring(
typeof string !== "string")
trap();
// Ensure the range is within bounds to avoid the complex behavior that
// `substring` performs when that is not the case.
if (start > string.length)
// Ensure the range is ordered to avoid the complex behavior that `substring`
// performs when that is not the case.
if (start > end ||
start > string.length)
return "";
// [1]
// If end > string.length, `substring` is specified to clamp it
// start is guaranteed to be at least zero (as it is unsigned), so there will
// not be any clamping of start.
return string.substring(start, end);
}
```
Expand Down

0 comments on commit 206a3a0

Please sign in to comment.