From 8025ed1bc03c28742308743f57a7c3baa1830bf8 Mon Sep 17 00:00:00 2001 From: Jordy van Dortmont Date: Mon, 3 Jul 2017 16:56:36 +0200 Subject: [PATCH 1/2] Fixed greedy comment parser --- index.js | 22 +++++++++++++++++++--- test/sjson.spec.js | 10 ++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e153eaa..ab9f202 100644 --- a/index.js +++ b/index.js @@ -37,11 +37,27 @@ class SJSON { function ws() { while (i < s.length) { if (s[i] === 47) { // "/" + const start = i; ++i; - if (s[i] === 47) + if (s[i] === 47) // "/" while (s[++i] !== 10); // "\n" - else if (s[i] === 42) // "*" - while (s[++i] !== 42); + else if (s[i] === 42) { // "*" + while (s[++i] !== 42); // "*" + if (s[i] === 47) { // "/" + i++; + break; + } + // No multi-line comment + else { + i = start; + break; + } + } + // No single-line comment + else { + i = start; + break; + } } else if (!hasChar(WHITESPACE, s[i])) { break; } diff --git a/test/sjson.spec.js b/test/sjson.spec.js index 0afdf84..c98d3bd 100644 --- a/test/sjson.spec.js +++ b/test/sjson.spec.js @@ -142,6 +142,16 @@ describe('simplified-json', function () { expect(parsedSJSON.nullable).to.be.null; }); + /** + * Validate that the SJSON Parser correctly parses keys with a leading solidus. + */ + it('parses keys with a leading solidus', function () { + const parsedSJSON = SJSON.parse('/key_with_a_leading_solidus = true'); + + expect(parsedSJSON["/key_with_a_leading_solidus"]).to.be.a('Boolean'); + expect(parsedSJSON["/key_with_a_leading_solidus"]).to.be.true; + }); + /** * Validate that the SJSON Parser throws a SyntaxError when encountering an * unexpected token. From 009c4725a2ebb75373c55e71517ba325a4ac430b Mon Sep 17 00:00:00 2001 From: Jordy van Dortmont Date: Mon, 3 Jul 2017 18:00:14 +0200 Subject: [PATCH 2/2] Fixed non-terminating loops --- index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index ab9f202..ff32c7c 100644 --- a/index.js +++ b/index.js @@ -40,9 +40,9 @@ class SJSON { const start = i; ++i; if (s[i] === 47) // "/" - while (s[++i] !== 10); // "\n" + while (++i < s.length && s[i] !== 10); // "\n" else if (s[i] === 42) { // "*" - while (s[++i] !== 42); // "*" + while (++i < s.length && s[i] !== 42); // "*" if (s[i] === 47) { // "/" i++; break; @@ -113,7 +113,7 @@ class SJSON { if (s[i] === 34 && s[i+1] === 34 && s[i+2] === 34) { i += 3; const start = i; - for (; s[i] !== 34 || s[i+1] !== 34 || s[i+2] !== 34; ++i); + for (; i+2 < s.length && (s[i] !== 34 || s[i+1] !== 34 || s[i+2] !== 34); ++i); i += 3; return s.toString('utf8', start, i-3); } @@ -121,7 +121,7 @@ class SJSON { const start = i; let escape = false; consume(34); - for (; s[i] !== 34; ++i) { // unescaped " + for (; i < s.length && s[i] !== 34; ++i) { // unescaped " if (s[i] === 92) { ++i; escape = true; @@ -134,7 +134,7 @@ class SJSON { i = start; var octets = []; consume(34); - for (; s[i] !== 34; ++i) { // unescaped " + for (; i < s.length && s[i] !== 34; ++i) { // unescaped " if (s[i] === 92) { ++i; if (s[i] == 98) octets.push(8); // \b @@ -162,7 +162,7 @@ class SJSON { ws(); consume(91); // "[" ws(); - for (; s[i] !== 93; ws()) // "]" + for (; i < s.length && s[i] !== 93; ws()) // "]" ar.push(pvalue()); consume(93); @@ -177,7 +177,7 @@ class SJSON { return pstring(); const start = i; - for (; !hasChar(ID_TERM, s[i]); ++i); + for (; i < s.length && !hasChar(ID_TERM, s[i]); ++i); return s.toString("utf8", start, i); } @@ -185,7 +185,7 @@ class SJSON { const object = Object.setPrototypeOf({}, null); consume(123); // "{" ws(); - for (; s[i] !== 125; ws()) { // "}" + for (; i < s.length && s[i] !== 125; ws()) { // "}" const key = pidentifier(); ws(); (s[i] === 58) ? consume(58) : consume(61); // ":" or "="