Skip to content

Commit

Permalink
Fixed #167: control characters in strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Oct 23, 2024
1 parent 6165e1e commit fa40d1e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Utf8Stream = require('./utils/Utf8Stream');

const patterns = {
value1: /^(?:[\"\{\[\]\-\d]|true\b|false\b|null\b|\s{1,256})/,
string: /^(?:[^\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
string: /^(?:[^\x00-\x1f\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
key1: /^(?:[\"\}]|\s{1,256})/,
colon: /^(?:\:|\s{1,256})/,
comma: /^(?:[\,\]\}]|\s{1,256})/,
Expand Down
60 changes: 60 additions & 0 deletions tests/test_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,65 @@ unit.add(module, [
eval(t.ASSERT('result.length === 0'));
async.done();
});
},
function test_parser_issue_167_1(t) {
const async = t.startAsync('test_parser_issue_167_1');

const input = `["a\x00a"]`,
pipeline = ReadString.make(input).pipe(Parser.make());

pipeline.on('data', function (chunk) {
// to start the pipeline
});

let errored = false;
pipeline.on('error', function () {
errored = true;
async.done();
});
pipeline.on('end', function () {
t.test(errored);
async.done();
});
},
function test_parser_issue_167_2(t) {
const async = t.startAsync('test_parser_issue_167_2');

const input = `["a\na"]`,
pipeline = ReadString.make(input).pipe(Parser.make());

pipeline.on('data', function (chunk) {
// to start the pipeline
});

let errored = false;
pipeline.on('error', function () {
errored = true;
async.done();
});
pipeline.on('end', function () {
t.test(errored);
async.done();
});
},
function test_parser_issue_167_3(t) {
const async = t.startAsync('test_parser_issue_167_3');

const input = `["a\ta"]`,
pipeline = ReadString.make(input).pipe(Parser.make());

pipeline.on('data', function (chunk) {
// to start the pipeline
});

let errored = false;
pipeline.on('error', function () {
errored = true;
async.done();
});
pipeline.on('end', function () {
t.test(errored);
async.done();
});
}
]);
94 changes: 74 additions & 20 deletions tests/test_verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ unit.add(module, [
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
t.test(!"We shouldn't be here!");
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
async.done();
});
},
Expand All @@ -31,11 +31,11 @@ unit.add(module, [
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
t.test(!"We shouldn't be here!");
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
async.done();
});
},
Expand All @@ -46,11 +46,11 @@ unit.add(module, [
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
t.test(!"We shouldn't be here!");
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
async.done();
});
},
Expand All @@ -61,11 +61,11 @@ unit.add(module, [
verifier = new Verifier({jsonStreaming: true}),
pipeline = ReadString.make(input).pipe(verifier);

pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
t.test(!"We shouldn't be here!");
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
async.done();
});
},
Expand All @@ -76,11 +76,11 @@ unit.add(module, [
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
eval(t.TEST('error.line === 1 && error.pos === 6 && error.offset === 5'));
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
t.test(!"We shouldn't be here!");
async.done();
});
Expand All @@ -92,11 +92,11 @@ unit.add(module, [
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
eval(t.TEST('error.line === 4 && error.pos === 1 && error.offset === 7'));
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
t.test(!"We shouldn't be here!");
async.done();
});
Expand All @@ -108,11 +108,11 @@ unit.add(module, [
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
eval(t.TEST('error.line === 4 && error.pos === 1 && error.offset === 10'));
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
t.test(!"We shouldn't be here!");
async.done();
});
Expand All @@ -125,12 +125,12 @@ unit.add(module, [
pipeline = ReadString.make(input).pipe(verifier);

let gotError = false;
pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
gotError = true;
eval(t.TEST('error.line === 1 && error.pos === 3 && error.offset === 2'));
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
if (!gotError) {
t.test(!"We shouldn't be here!");
async.done();
Expand All @@ -145,12 +145,12 @@ unit.add(module, [
pipeline = ReadString.make(input).pipe(verifier);

let gotError = false;
pipeline.on('error', function(error) {
pipeline.on('error', function (error) {
gotError = true;
eval(t.TEST('error.line === 1 && error.pos === 7 && error.offset === 6'));
async.done();
});
pipeline.on('finish', function() {
pipeline.on('finish', function () {
if (!gotError) {
t.test(!"We shouldn't be here!");
async.done();
Expand All @@ -170,9 +170,63 @@ unit.add(module, [
async.done();
});
Readable.from(
(function*() {
while(true) yield sample;
(function* () {
while (true) yield sample;
})()
).pipe(verifier);
},
function test_verifier_issue_167_1(t) {
const async = t.startAsync('test_verifier_issue_167_1');

const input = '"a\x00a"',
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

let errored = false;
pipeline.on('error', function (error) {
t.test(error);
errored = true;
async.done();
});
pipeline.on('finish', function () {
t.test(errored);
async.done();
});
},
function test_verifier_issue_167_2(t) {
const async = t.startAsync('test_verifier_issue_167_2');

const input = '"a\na"',
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

let errored = false;
pipeline.on('error', function (error) {
t.test(error);
errored = true;
async.done();
});
pipeline.on('finish', function () {
t.test(errored);
async.done();
});
},
function test_verifier_issue_167_3(t) {
const async = t.startAsync('test_verifier_issue_167_3');

const input = '"a\ta"',
verifier = new Verifier(),
pipeline = ReadString.make(input).pipe(verifier);

let errored = false;
pipeline.on('error', function (error) {
t.test(error);
errored = true;
async.done();
});
pipeline.on('finish', function () {
t.test(errored);
async.done();
});
}
]);
2 changes: 1 addition & 1 deletion utils/Verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {StringDecoder} = require('string_decoder');

const patterns = {
value1: /^(?:[\"\{\[\]\-\d]|true\b|false\b|null\b|\s{1,256})/,
string: /^(?:[^\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
string: /^(?:[^\x00-\x1f\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
key1: /^(?:[\"\}]|\s{1,256})/,
colon: /^(?:\:|\s{1,256})/,
comma: /^(?:[\,\]\}]|\s{1,256})/,
Expand Down

0 comments on commit fa40d1e

Please sign in to comment.