Fix issues from AddressSanitizer 11/147311/5
authorSangyoon Jang <jeremy.jang@samsung.com>
Mon, 4 Sep 2017 02:28:30 +0000 (11:28 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Tue, 12 Sep 2017 01:34:04 +0000 (01:34 +0000)
- Fix memory leak
- Fix null pointer dereference
- Fix LogCatcher to escape '%'

Change-Id: Ie26bf66f8fe9788b576703b66a2274b01816fc0b
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/manifest_parser/manifest_util.cc
src/manifest_parser/utils/logging.h

index 9bb05857ebaba218f417cea1afa77d2a44e1e186..8e20bded63b4f8b8f0764fca94a410600a0f7887 100644 (file)
@@ -256,6 +256,8 @@ std::unique_ptr<DictionaryValue> LoadXMLNode(
         reinterpret_cast<const char*>(root->ns->href));
 
   for (xmlNode* node = root->children; node; node = node->next) {
+    if (!node->name)
+      continue;
     std::string sub_node_name(reinterpret_cast<const char*>(node->name));
     std::unique_ptr<DictionaryValue> sub_value =
         LoadXMLNode(node, constraints, current_dir);
@@ -322,6 +324,7 @@ std::shared_ptr<Manifest> LoadManifest(const std::string& manifest_path,
   std::unique_ptr<DictionaryValue> result(new DictionaryValue);
   if (dv)
     result->Set(reinterpret_cast<const char*>(root_node->name), dv.release());
+  xmlFreeDoc(doc);
   return std::make_shared<parser::Manifest>(std::move(result));
 }
 
index 2ed3ba06a83914eb5a3adae35d49af934eb7e56f..adb13fa643123b716be757e6d5ee2dc24d6d58e7 100644 (file)
@@ -70,7 +70,8 @@ class LogCatcher {
     : level_(level), tag_(tag) { }
 
   void operator&(const StringStream<char>& str) const {
-    dlog_print(LogLevelToPriority(level_), tag_.c_str(), str.str().c_str());
+    dlog_print(LogLevelToPriority(level_), tag_.c_str(),
+        Escape(str.str()).c_str());
 
     static const char* app_installer_log = getenv("APP_INSTALLERS_LOG");
     if (level_ == LogLevel::LOG_ERROR || app_installer_log != nullptr) {
@@ -79,6 +80,21 @@ class LogCatcher {
     }
   }
  private:
+  // Since LogCatcher passes input to dlog_print(), the input which contains
+  // format string(such as %d, %n) can cause unexpected result.
+  // This is simple function to escape '%'.
+  // NOTE: Is there any gorgeous way instead of this?
+  std::string Escape(const std::string& str) const {
+    std::string escaped = std::string(str);
+    size_t start_pos = 0;
+    std::string from = "%";
+    std::string to = "%%";
+    while ((start_pos = escaped.find(from, start_pos)) != std::string::npos) {
+      escaped.replace(start_pos, from.length(), to);
+      start_pos += to.length();
+    }
+    return escaped;
+  }
   LogLevel level_;
   std::string tag_;
 };