Fixes after cppcheck 92/86292/3
authorPiotr Dabrowski <p.dabrowski2@samsung.com>
Thu, 1 Sep 2016 13:37:33 +0000 (15:37 +0200)
committerPiotr Dabrowski <p.dabrowski2@samsung.com>
Thu, 1 Sep 2016 13:37:33 +0000 (15:37 +0200)
> [wgt-backend/src/wgt/step/encryption/step_encrypt_resources.cc:175]: (error) Resource leak: input
> [wgt-backend/src/wgt/step/encryption/step_encrypt_resources.cc:251]: (error) Deallocating a deallocated pointer: output
> [wgt-backend/src/wgt/extension_config_parser.cc:27]: (performance) Variable 'config_xml_' is assigned in constructor body. Consider performing initialization in initialization list.

Change-Id: I4386dd4b5923566cc2e7ce941144496200fe139b

src/wgt/extension_config_parser.cc
src/wgt/step/encryption/step_encrypt_resources.cc

index 4813d72..1a1258d 100755 (executable)
@@ -23,9 +23,8 @@ const char kExtensionPath[] = "extension.privilege";
 const char kExtensionNameKey[] = "@name";
 }  // namespace
 
-ExtensionConfigParser::ExtensionConfigParser(std::string config_xml) {
-  config_xml_ = config_xml;
-}
+ExtensionConfigParser::ExtensionConfigParser(std::string config_xml)
+    : config_xml_(config_xml) {}
 
 std::unique_ptr<parser::DictionaryValue>
     ExtensionConfigParser::LoadExtensionConfig(const std::string& config_xml) {
index 4d5fb91..b2ec518 100644 (file)
@@ -52,20 +52,22 @@ std::size_t ReadBytes(unsigned char* buffer, std::size_t count, FILE* stream) {
 
 void WriteBytes(unsigned char* buffer, std::size_t count, FILE* stream) {
   // original file is treated as destination!
+  if (count <= 0) {
+    return;
+  }
   std::size_t bytes_written = 0;
-  std::size_t bytes_to_write = 0;
   do {
-    bytes_to_write = count - bytes_written;
-    bytes_written = std::fwrite(buffer + bytes_written,
-                               sizeof(unsigned char),
-                               count - bytes_written,
-                               stream);
-    if ((bytes_written != bytes_to_write)) {
+     std::size_t bytes_appended = std::fwrite(
+         buffer + bytes_written,
+         sizeof(unsigned char),
+         count - bytes_written,
+         stream);
+    if ((bytes_appended == 0)) {
       LOG(ERROR) << "Error while writing data";
-      free(buffer);
-      fclose(stream);
+      return;
     }
-  } while ((bytes_written != bytes_to_write));
+    bytes_written += bytes_appended;
+  } while ((bytes_written < count));
 }
 
 }  // namespace
@@ -172,6 +174,7 @@ bool StepEncryptResources::EncryptFile(const bf::path &src) {
   FILE *output = OpenFile(encFile.string().c_str(), "wb");
   if (output == nullptr) {
     LOG(ERROR) << "Cannot create encrypted file: " << encFile.string();
+    fclose(input);
     return false;
   }