From b217fd3f8d69401c7cd5a4b1279c37ffb12e7002 Mon Sep 17 00:00:00 2001
From: Piotr Dabrowski
Date: Thu, 1 Sep 2016 15:37:33 +0200
Subject: [PATCH] Fixes after cppcheck
> [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 | 5 ++--
.../step/encryption/step_encrypt_resources.cc | 23 +++++++++++--------
2 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/src/wgt/extension_config_parser.cc b/src/wgt/extension_config_parser.cc
index 4813d72..1a1258d 100755
--- a/src/wgt/extension_config_parser.cc
+++ b/src/wgt/extension_config_parser.cc
@@ -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
ExtensionConfigParser::LoadExtensionConfig(const std::string& config_xml) {
diff --git a/src/wgt/step/encryption/step_encrypt_resources.cc b/src/wgt/step/encryption/step_encrypt_resources.cc
index 4d5fb91..b2ec518 100644
--- a/src/wgt/step/encryption/step_encrypt_resources.cc
+++ b/src/wgt/step/encryption/step_encrypt_resources.cc
@@ -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;
}
--
2.34.1