From a21f31d46fb903fe1ce05f391dad299eb2f60b23 Mon Sep 17 00:00:00 2001 From: David Bau Date: Mon, 23 Dec 2013 16:05:04 +0000 Subject: [PATCH] Escape null characters as \0 unless followed by 0-7. --- lib/output.js | 5 +++-- test/compress/concat-strings.js | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/output.js b/lib/output.js index 9ed155b4..bf669516 100644 --- a/lib/output.js +++ b/lib/output.js @@ -85,7 +85,8 @@ function OutputStream(options) { function make_string(str) { 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) { case "\\": return "\\\\"; case "\b": return "\\b"; @@ -96,7 +97,7 @@ function OutputStream(options) { case "\u2029": return "\\u2029"; case '"': ++dq; return '"'; case "'": ++sq; return "'"; - case "\0": return "\\x00"; + case "\0": return /^[0-7]/.test(w.substr(i+1)) ? "\\x00" : "\\0"; } return s; }); diff --git a/test/compress/concat-strings.js b/test/compress/concat-strings.js index 79192987..50eef8b8 100644 --- a/test/compress/concat-strings.js +++ b/test/compress/concat-strings.js @@ -11,6 +11,9 @@ concat_1: { var d = 1 + x() + 2 + 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: { var a = "foobar" + x() + "moofoo" + y() + "xyz" + q(); @@ -18,5 +21,6 @@ concat_1: { var c = 1 + x() + 2 + "boo"; var d = 1 + x() + 2 + 3 + "boo"; var e = 1 + x() + 2 + "X3boo"; + var f = "\x00360\08\0"; } }