[gen_snapshot] Escape paths when writing loading unit manifests.

Bug: https://github.com/dart-lang/sdk/issues/41974
Change-Id: I030629110026bbc4b1c339764ed805f65f753448
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154827
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
diff --git a/runtime/bin/gen_snapshot.cc b/runtime/bin/gen_snapshot.cc
index 916dd52..2cf7ab9 100644
--- a/runtime/bin/gen_snapshot.cc
+++ b/runtime/bin/gen_snapshot.cc
@@ -24,7 +24,6 @@
 #include "bin/thread.h"
 #include "bin/utils.h"
 #include "bin/vmservice_impl.h"
-#include "platform/syslog.h"
 
 #include "include/dart_api.h"
 #include "include/dart_tools_api.h"
@@ -32,6 +31,8 @@
 #include "platform/globals.h"
 #include "platform/growable_array.h"
 #include "platform/hashmap.h"
+#include "platform/syslog.h"
+#include "platform/text_buffer.h"
 
 namespace dart {
 namespace bin {
@@ -600,13 +601,14 @@
 static void WriteLoadingUnitManifest(File* manifest_file,
                                      intptr_t id,
                                      const char* path) {
-  bool success = true;
+  TextBuffer line(128);
   if (id != 1) {
-    success &= manifest_file->Print(",");
+    line.Printf(",");
   }
-  success &=
-      manifest_file->Print("{ \"id\": %" Pd ", \"path\": \"%s\" }\n", id, path);
-  if (!success) {
+  line.Printf("{ \"id\": %" Pd ", \"path\": \"", id);
+  line.AddEscapedString(path);
+  line.Printf("\" }");
+  if (!manifest_file->Print("%s\n", line.buf())) {
     PrintErrAndExit("Error: Unable to write file: %s\n\n",
                     loading_unit_manifest_filename);
   }