util.json: Fixed handling of truncated JSON.
authorWaqas Hussain <waqas20@gmail.com>
Thu, 2 Jun 2011 00:36:15 +0000 (05:36 +0500)
committerWaqas Hussain <waqas20@gmail.com>
Thu, 2 Jun 2011 00:36:15 +0000 (05:36 +0500)
util/json.lua

index 05453703369e35f642b11acd61c33f93c5e2a97e..cfa84a4b24b88bf6c7daf41a1754492cfef261be 100644 (file)
@@ -134,12 +134,14 @@ end
 
 
 function json.decode(json)
+       json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
        local pos = 1;
        local current = {};
        local stack = {};
        local ch, peek;
        local function next()
                ch = json:sub(pos, pos);
+               if ch == "" then error("Unexpected EOF"); end
                pos = pos+1;
                peek = json:sub(pos, pos);
                return ch;