From 72aa88ffa713d960432be425bc516c06adff6b70 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Fri, 2 Feb 2018 10:43:10 +0000 Subject: [PATCH] Fix isWhitespace crashing on large characters As we do not operate on unsigned bytes, anything > 127 would overflow and become negative, resulting in out of bounds accesses. --- src/main/java/org/squiddev/cobalt/lib/StringLib.java | 2 +- src/test/resources/assert/stringIssues.lua | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/squiddev/cobalt/lib/StringLib.java b/src/main/java/org/squiddev/cobalt/lib/StringLib.java index 882ab94c..ff23b827 100644 --- a/src/main/java/org/squiddev/cobalt/lib/StringLib.java +++ b/src/main/java/org/squiddev/cobalt/lib/StringLib.java @@ -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 { diff --git a/src/test/resources/assert/stringIssues.lua b/src/test/resources/assert/stringIssues.lua index 5200e256..de488c9a 100644 --- a/src/test/resources/assert/stringIssues.lua +++ b/src/test/resources/assert/stringIssues.lua @@ -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)