Fix static analysis issue 66/281466/3
authorSangyoon Jang <jeremy.jang@samsung.com>
Mon, 19 Sep 2022 08:15:01 +0000 (17:15 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Tue, 20 Sep 2022 03:56:14 +0000 (12:56 +0900)
Change-Id: I6a8a2b568828b4556da97099f9ba0488aeb1cb97
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/tpk/step/pkgmgr/step_manifest_adjustment.cc

index 8587f8cd6212fa4fcc1f3faa4f383dbdb818e997..64c5912c3fa16dac3f96d1e3a3e5398df44a537d 100644 (file)
@@ -15,6 +15,7 @@
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
 #include <string>
+#include <memory>
 
 #include "common/utils/profile_util.h"
 #include "common/installer_context.h"
@@ -74,43 +75,43 @@ common_installer::Step::Status StepManifestAdjustment::precheck() {
 }
 
 common_installer::Step::Status StepManifestAdjustment::process() {
-  xmlDocPtr doc = xmlParseFile(xml_path_.c_str());
+  std::unique_ptr<xmlDoc, decltype(xmlFreeDoc)*> doc(
+      xmlParseFile(xml_path_.c_str()), xmlFreeDoc);
 
-  if (doc == nullptr) {
+  if (doc == nullptr || doc.get() == nullptr) {
     LOG(ERROR) << "Could not parse file";
     return Step::Status::ERROR;
   }
 
-  xmlNodePtr node = xmlDocGetRootElement(doc);
+  xmlNodePtr node = xmlDocGetRootElement(doc.get());
+  if (node == nullptr) {
+    LOG(ERROR) << "Could not get root element";
+    return Step::Status::ERROR;
+  }
 
   if (!SetProperty(node, "type", context_->manifest_data.get()->type)) {
-    xmlFreeDoc(doc);
     return Step::Status::ERROR;
   }
 
   if (!SetProperty(node, "readonly",
       context_->manifest_data.get()->readonly)) {
-    xmlFreeDoc(doc);
     return Step::Status::ERROR;
   }
 
   if (!SetProperty(node, "preload",
       context_->manifest_data.get()->preload)) {
-    xmlFreeDoc(doc);
     return Step::Status::ERROR;
   }
 
   if (!SetProperty(node, "removable",
       context_->manifest_data.get()->removable)) {
-    xmlFreeDoc(doc);
     return Step::Status::ERROR;
   }
 
   if (IsNonPrivilegedPackage() && IsAdjustmentNecessary()) {
-    xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc);
+    xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc.get());
     if (!xpath_ctx) {
       LOG(ERROR) << "Failed to create XPath context";
-      xmlFreeDoc(doc);
       return Step::Status::ERROR;
     }
 
@@ -135,7 +136,6 @@ common_installer::Step::Status StepManifestAdjustment::process() {
           !SetManifestElement(&(app->onboot), "false")) {
         xmlXPathFreeObject(xpath_obj);
         xmlXPathFreeContext(xpath_ctx);
-        xmlFreeDoc(doc);
         return Step::Status::ERROR;
       }
 
@@ -144,14 +144,11 @@ common_installer::Step::Status StepManifestAdjustment::process() {
     xmlXPathFreeContext(xpath_ctx);
   }
 
-  if (xmlSaveFile(xml_path_.c_str(), doc) == -1) {
+  if (xmlSaveFile(xml_path_.c_str(), doc.get()) == -1) {
     LOG(ERROR) << "Failed to modify xml file";
-    xmlFreeDoc(doc);
     return Step::Status::ERROR;
   }
 
-  xmlFreeDoc(doc);
-
   return Step::Status::OK;
 }