Bug 1467759 - Escape all the property names in JSONWriter. r=froydnj
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Fri, 08 Jun 2018 19:50:25 +0200
changeset 422381 faeec6285083317d76e9ef9385594146cd00005d
parent 422380 afd31895b88a3ef2f750bce89b530d8e992de410
child 422382 ee2f1c495bc1c200108ffd927effcaf34e209bd5
push id34131
push usercsabou@mozilla.com
push dateWed, 13 Jun 2018 09:21:21 +0000
treeherdermozilla-central@5182bca90d06 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1467759
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1467759 - Escape all the property names in JSONWriter. r=froydnj Scalar property names (e.g. IntProperty) are already escaped correctly. This patch escapes the collection property names (e.g. StartObjectProperty) as well (and adds test coverage for it). MozReview-Commit-ID: 68kkjPb2ZN7
mfbt/JSONWriter.h
mfbt/tests/TestJSONWriter.cpp
--- a/mfbt/JSONWriter.h
+++ b/mfbt/JSONWriter.h
@@ -308,19 +308,17 @@ protected:
     mNeedNewlines[mDepth] = true;
   }
 
   void StartCollection(const char* aMaybePropertyName, const char* aStartChar,
                        CollectionStyle aStyle = MultiLineStyle)
   {
     Separator();
     if (aMaybePropertyName) {
-      mWriter->Write("\"");
-      mWriter->Write(aMaybePropertyName);
-      mWriter->Write("\": ");
+      PropertyNameAndColon(aMaybePropertyName);
     }
     mWriter->Write(aStartChar);
     mNeedComma[mDepth] = true;
     mDepth++;
     NewVectorEntries();
     mNeedNewlines[mDepth] =
       mNeedNewlines[mDepth - 1] && aStyle == MultiLineStyle;
   }
--- a/mfbt/tests/TestJSONWriter.cpp
+++ b/mfbt/tests/TestJSONWriter.cpp
@@ -522,18 +522,60 @@ void TestDeepNesting()
       w.EndArray();
     }
   }
   w.End();
 
   Check(w.WriteFunc(), expected);
 }
 
+void TestEscapedPropertyNames()
+{
+  const char* expected = "\
+{\"i\\t\": 1, \"array\\t\": [null, [{}], {\"o\\t\": {}}, \"s\"], \"d\\t\": 3.33}\n\
+";
+
+  JSONWriter w(MakeUnique<StringWriteFunc>());
+
+  w.Start(w.SingleLineStyle);
+
+  w.IntProperty("i\t", 1);
+
+  w.StartArrayProperty("array\t");
+  {
+    w.NullElement();
+
+    w.StartArrayElement(w.MultiLineStyle);  // style overridden from above
+    {
+      w.StartObjectElement();
+      w.EndObject();
+    }
+    w.EndArray();
+
+    w.StartObjectElement();
+    {
+      w.StartObjectProperty("o\t");
+      w.EndObject();
+    }
+    w.EndObject();
+
+    w.StringElement("s");
+  }
+  w.EndArray();
+
+  w.DoubleProperty("d\t", 3.33);
+
+  w.End();
+
+  Check(w.WriteFunc(), expected);
+}
+
 int main(void)
 {
   TestBasicProperties();
   TestBasicElements();
   TestOneLineObject();
   TestStringEscaping();
   TestDeepNesting();
+  TestEscapedPropertyNames();
 
   return 0;
 }