From 7f823abd2ee28cbe9e843950afa68550e6a579ae Mon Sep 17 00:00:00 2001 From: rxi Date: Wed, 19 Aug 2015 19:27:52 +0100 Subject: [PATCH] Fixed parse_string() to handle "\\" correctly, added tests --- json.lua | 11 +++++++---- test/test.lua | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/json.lua b/json.lua index 4115a53..5d6536d 100644 --- a/json.lua +++ b/json.lua @@ -214,11 +214,12 @@ local function parse_string(str, i, chr) has_unicode_escape = true end else + if not escape_chars[x] then + decode_error(str, j, "invalid escape char '" .. x .. "' in string") + end has_escape = true end - if not escape_chars[x] then - decode_error(str, j, "invalid escape char '" .. x .. "' in string") - end + last = nil elseif x == '"' then local s = str:sub(i + 1, j - 1) @@ -232,8 +233,10 @@ local function parse_string(str, i, chr) s = s:gsub("\\.", escape_char_map_inv) end return s, j + 1 + + else + last = x end - last = x end decode_error(str, i, "expected closing quote for string") end diff --git a/test/test.lua b/test/test.lua index 26d019d..50c354a 100644 --- a/test/test.lua +++ b/test/test.lua @@ -152,6 +152,8 @@ test("decode escape", function() [ [["\u263a"]] ] = '☺', [ [["\ud83d\ude02"]] ] = '😂', [ [["\r\n\t\\\""]] ] = '\r\n\t\\"', + [ [["\\"]] ] = '\\', + [ [["\\\\"]] ] = '\\\\', } for k, v in pairs(t) do local res = json.decode(k)