Skip to content

Commit

Permalink
Fix isWhitespace crashing on large characters
Browse files Browse the repository at this point in the history
As we do not operate on unsigned bytes, anything > 127 would overflow
and become negative, resulting in out of bounds accesses.
  • Loading branch information
SquidDev committed Feb 2, 2018
1 parent 5d73551 commit 72aa88f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/squiddev/cobalt/lib/StringLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ private static int posrelat(int pos, int len) {
private static final byte[] CHAR_TABLE;

public static boolean isWhitespace(byte b) {
return (CHAR_TABLE[b] & MASK_SPACE) != 0;
return (CHAR_TABLE[b & 0xFF] & MASK_SPACE) != 0;
}

static {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/assert/stringIssues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ assert(tostring(1e39) == "1e+39", "Got " .. tostring(1e39))
-- Tiny bits of string concatination
local x, y, z = "foo", "bar", "baz"
assert(x .. y .. z == table.concat { x, y, z })

-- Whitespace on large characters
assert(tonumber(("\128")) == nil)

0 comments on commit 72aa88f

Please sign in to comment.