From 44a2bd767843f018fb73f8a9f09d57f27a39230b Mon Sep 17 00:00:00 2001 From: Jacek Jendrzej Date: Fri, 26 Oct 2012 23:16:30 +0200 Subject: [PATCH] src/system/helpers.cpp: fix resource leak ,use new instead of malloc Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/a03445fe401606f34e7db5d5aab21f5cc9e5cfac Author: Jacek Jendrzej Date: 2012-10-26 (Fri, 26 Oct 2012) --- src/system/helpers.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index d68dcf67a..c993bbf41 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -246,14 +246,14 @@ std::string trim(std::string &str, const std::string &trimChars /*= " \n\r\t"*/) CFileHelpers::CFileHelpers() { FileBufSize = 0xFFFF; - FileBuf = (char*)malloc(FileBufSize); + FileBuf = new char[FileBufSize]; doCopyFlag = true; } CFileHelpers::~CFileHelpers() { if (FileBuf != NULL) - free(FileBuf); + delete [] FileBuf; } CFileHelpers* CFileHelpers::getInstance() @@ -271,7 +271,7 @@ bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode) if ((fd1 = open(Src, O_RDONLY)) < 0) return false; if ((fd2 = open(Dst, O_WRONLY | O_CREAT)) < 0) { - close(fd2); + close(fd1); return false; } @@ -294,8 +294,11 @@ bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode) if (doCopyFlag) { lseek64(fd2, 0, SEEK_SET); off64_t fsizeDst64 = lseek64(fd2, 0, SEEK_END); - if (fsizeSrc64 != fsizeDst64) + if (fsizeSrc64 != fsizeDst64){ + close(fd1); + close(fd2); return false; + } } } else { // < 2GB @@ -316,8 +319,11 @@ bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode) if (doCopyFlag) { lseek(fd2, 0, SEEK_SET); long fsizeDst = lseek(fd2, 0, SEEK_END); - if (fsizeSrc != fsizeDst) + if (fsizeSrc != fsizeDst){ + close(fd1); + close(fd2); return false; + } } } close(fd1);