Escape null characters as \0 unless followed by 0-7.

This commit is contained in:
David Bau 2013-12-23 16:05:04 +00:00
parent aa9de76370
commit a21f31d46f
2 changed files with 7 additions and 2 deletions

View File

@ -85,7 +85,8 @@ function OutputStream(options) {
function make_string(str) { function make_string(str) {
var dq = 0, sq = 0; var dq = 0, sq = 0;
str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0]/g, function(s){ str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0]/g,
function(s, i, w) {
switch (s) { switch (s) {
case "\\": return "\\\\"; case "\\": return "\\\\";
case "\b": return "\\b"; case "\b": return "\\b";
@ -96,7 +97,7 @@ function OutputStream(options) {
case "\u2029": return "\\u2029"; case "\u2029": return "\\u2029";
case '"': ++dq; return '"'; case '"': ++dq; return '"';
case "'": ++sq; return "'"; case "'": ++sq; return "'";
case "\0": return "\\x00"; case "\0": return /^[0-7]/.test(w.substr(i+1)) ? "\\x00" : "\\0";
} }
return s; return s;
}); });

View File

@ -11,6 +11,9 @@ concat_1: {
var d = 1 + x() + 2 + 3 + "boo"; var d = 1 + x() + 2 + 3 + "boo";
var e = 1 + x() + 2 + "X" + 3 + "boo"; var e = 1 + x() + 2 + "X" + 3 + "boo";
// be careful with concatentation with "\0" with octal-looking strings.
var f = "\0" + 360 + "\0" + 8 + "\0";
} }
expect: { expect: {
var a = "foobar" + x() + "moofoo" + y() + "xyz" + q(); var a = "foobar" + x() + "moofoo" + y() + "xyz" + q();
@ -18,5 +21,6 @@ concat_1: {
var c = 1 + x() + 2 + "boo"; var c = 1 + x() + 2 + "boo";
var d = 1 + x() + 2 + 3 + "boo"; var d = 1 + x() + 2 + 3 + "boo";
var e = 1 + x() + 2 + "X3boo"; var e = 1 + x() + 2 + "X3boo";
var f = "\x00360\08\0";
} }
} }