[geary/bug/728002-webkit2: 136/140] Fix brown-paper-bag bug in Geary.JS::escape_string.
- From: Michael Gratton <mjog src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [geary/bug/728002-webkit2: 136/140] Fix brown-paper-bag bug in Geary.JS::escape_string.
- Date: Tue, 31 Jan 2017 23:09:58 +0000 (UTC)
commit 97709785d7956af40dfc52a15b9e4a3270957f96
Author: Michael James Gratton <mike vee net>
Date: Mon Jan 30 23:35:32 2017 +1100
Fix brown-paper-bag bug in Geary.JS::escape_string.
* src/engine/util/util-js.vala (Geary.JS): Correctly append escaped char
to the string. Add unit tests.
src/engine/util/util-js.vala | 42 ++++++++++++++++++++++++++++++++++------
test/CMakeLists.txt | 1 +
test/engine/util-js-test.vala | 25 ++++++++++++++++++++++++
test/main.vala | 1 +
4 files changed, 62 insertions(+), 7 deletions(-)
---
diff --git a/src/engine/util/util-js.vala b/src/engine/util/util-js.vala
index 4f65c14..ad4d94b 100644
--- a/src/engine/util/util-js.vala
+++ b/src/engine/util/util-js.vala
@@ -157,22 +157,50 @@ namespace Geary.JS {
}
/**
- * Escapes a string so as to be safte to use as a JS string literal.
+ * Escapes a string so as to be safe to use as a JS string literal.
*
* This does not append opening or closing quotes.
*/
public string escape_string(string value) {
- const unichar[] RESERVED = {
- '\x00', '\'', '"', '\\', '\n', '\r', '\x0b', '\t', '\b', '\f'
- };
StringBuilder builder = new StringBuilder.sized(value.length);
for (int i = 0; i < value.length; i++) {
if (value.valid_char(i)) {
unichar c = value.get_char(i);
- if (c in RESERVED) {
- builder.append_c('\\');
+ switch (c) {
+ case '\x00':
+ builder.append("\x00");
+ break;
+ case '\'':
+ builder.append("""\'""");
+ break;
+ case '"':
+ builder.append("""\"""");
+ break;
+ case '\\':
+ builder.append("""\\""");
+ break;
+ case '\n':
+ builder.append("""\n""");
+ break;
+ case '\r':
+ builder.append("""\r""");
+ break;
+ case '\x0b':
+ builder.append("\x0b");
+ break;
+ case '\t':
+ builder.append("""\t""");
+ break;
+ case '\b':
+ builder.append("""\b""");
+ break;
+ case '\f':
+ builder.append("""\f""");
+ break;
+ default:
+ builder.append_unichar(c);
+ break;
}
- builder.append_unichar(c);
}
}
return (string) builder.data;
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 3980e58..3c688d4 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -12,6 +12,7 @@ set(TEST_SRC
engine/rfc822-utils-test.vala
engine/util-html-test.vala
engine/util-inet-test.vala
+ engine/util-js-test.vala
engine/util-timeout-manager-test.vala
client/application/geary-configuration-test.vala
diff --git a/test/engine/util-js-test.vala b/test/engine/util-js-test.vala
new file mode 100644
index 0000000..e908105
--- /dev/null
+++ b/test/engine/util-js-test.vala
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2017 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+public class Geary.JS.Test : Gee.TestCase {
+
+ public Test() {
+ base("Geary.JS.Test");
+ add_test("escape_string", escape_string);
+ }
+
+ public void escape_string() {
+ print("\ndata: %s\n", Geary.JS.escape_string("\n"));
+ assert(Geary.JS.escape_string("\n") == """\n""");
+ assert(Geary.JS.escape_string("\r") == """\r""");
+ assert(Geary.JS.escape_string("\t") == """\t""");
+ assert(Geary.JS.escape_string("\'") == """\'""");
+ assert(Geary.JS.escape_string("\"") == """\"""");
+
+ assert(Geary.JS.escape_string("something…\n") == """something…\n""");
+ }
+}
diff --git a/test/main.vala b/test/main.vala
index 4cd1256..74382e8 100644
--- a/test/main.vala
+++ b/test/main.vala
@@ -38,6 +38,7 @@ int main(string[] args) {
TestSuite engine = new TestSuite("engine");
engine.add_suite(new Geary.HTML.UtilTest().get_suite());
+ engine.add_suite(new Geary.JS.Test().get_suite());
engine.add_suite(new Geary.Inet.Test().get_suite());
engine.add_suite(new Geary.RFC822.MailboxAddressTest().get_suite());
engine.add_suite(new Geary.RFC822.MessageTest().get_suite());
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]