Skip to content

Commit

Permalink
LUAFDN-1514 Fix out-of-bounds error in charCodeAt (#182)
Browse files Browse the repository at this point in the history
* Fix charCodeAt edge case

* Add comment

* Update rotriever and changelog
  • Loading branch information
RoFlection Bot committed Feb 9, 2023
1 parent 4e8e55f commit d807a0a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

## 1.2.3
* Fix an edge case in String.charCodeAt introduced in 1.2.2

## 1.2.2

* Optimize charCodeAt by removing an iterations over all codepoints in the input string.
Expand Down
1 change: 1 addition & 0 deletions modules/string/src/__tests__/charCodeAt.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ return function()
local body = "\u{8123}a"

jestExpect(charCodeAt(body, 0)).toBe(Number.NaN)
jestExpect(charCodeAt(body, 3)).toBe(Number.NaN)
jestExpect(charCodeAt(body, 100)).toBe(Number.NaN)
end)

Expand Down
7 changes: 5 additions & 2 deletions modules/string/src/charCodeAt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ return function(str: string, index: number): number
but it is cheaper to check string.len and handle utf8.offset than to check
utf.len, which iterates over all codepoints.
]]
if index < 1 or index > string.len(str) then
local length = string.len(str)
if index < 1 or index > length then
return NaN
end

-- utf8.offset returns nil for out of bounds
local offset = utf8.offset(str, index)
if offset == nil then

-- check that offset is not greater than the length of the string
if offset == nil or offset > length then
return NaN
end

Expand Down
2 changes: 1 addition & 1 deletion rotriever.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
version = "1.2.2"
version = "1.2.3"
members = ["modules/*"]
default_member = "LuauPolyfill"
authors = [
Expand Down

0 comments on commit d807a0a

Please sign in to comment.