From 75a5173176adfc4bc417c10df137d9ea8d90d403 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 28 Mar 2017 20:20:54 +0300 Subject: Change memory allocation in Virtfs::LoadFile from calloc to new. --- src/fs/files.cpp | 4 ++-- src/fs/virtfs/virtfstools.cpp | 16 ++++++++-------- src/fs/virtfs/virtfstools.h | 2 +- src/gui/windows/updaterwindow.cpp | 9 +++++---- src/integrity_unittest.cc | 4 ++-- src/utils/translation/poparser.cpp | 4 ++-- src/utils/translation/translationmanager.cpp | 5 ++--- src/utils/xml/libxml.cpp | 9 +++++---- src/utils/xml/pugixml.cpp | 17 ++++++++--------- src/utils/xml/tinyxml2.cpp | 15 ++++++++------- 10 files changed, 43 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/fs/files.cpp b/src/fs/files.cpp index 91433e287..fa9fe2de3 100644 --- a/src/fs/files.cpp +++ b/src/fs/files.cpp @@ -91,11 +91,11 @@ void Files::copyVirtFsFile(const std::string &restrict inFile, const std::string &restrict outFile) { int size = 0; - void *const buf = VirtFs::loadFile(inFile, size); + char *const buf = VirtFs::loadFile(inFile, size); FILE *const file = fopen(outFile.c_str(), "w"); fwrite(buf, 1, size, file); fclose(file); - free(buf); + delete [] buf; #ifdef ANDROID if (mCallbackPtr) { diff --git a/src/fs/virtfs/virtfstools.cpp b/src/fs/virtfs/virtfstools.cpp index 957b73e6e..c58f41b43 100644 --- a/src/fs/virtfs/virtfstools.cpp +++ b/src/fs/virtfs/virtfstools.cpp @@ -36,7 +36,7 @@ namespace VirtFs { - void *loadFile(const std::string &restrict fileName, + char *loadFile(const std::string &restrict fileName, int &restrict fileSize) { // Attempt to open the specified file using PhysicsFS @@ -55,7 +55,9 @@ namespace VirtFs fileSize = CAST_S32(VirtFs::fileLength(file)); // Allocate memory and load the file - void *restrict const buffer = calloc(fileSize, 1); + char *restrict const buffer = new char[fileSize]; + if (fileSize > 0) + buffer[fileSize - 1] = 0; VirtFs::read(file, buffer, 1, fileSize); VirtFs::close(file); @@ -185,8 +187,7 @@ namespace VirtFs std::string loadTextFileString(const std::string &fileName) { int contentsLength; - char *fileContents = static_cast( - VirtFs::loadFile(fileName, contentsLength)); + char *fileContents = VirtFs::loadFile(fileName, contentsLength); if (!fileContents) { @@ -194,7 +195,7 @@ namespace VirtFs return std::string(); } const std::string str = std::string(fileContents, contentsLength); - free(fileContents); + delete [] fileContents; return str; } @@ -202,8 +203,7 @@ namespace VirtFs StringVect &lines) { int contentsLength; - char *fileContents = static_cast( - VirtFs::loadFile(fileName, contentsLength)); + char *fileContents = VirtFs::loadFile(fileName, contentsLength); if (!fileContents) { @@ -217,7 +217,7 @@ namespace VirtFs while (getline(iss, line)) lines.push_back(line); - free(fileContents); + delete [] fileContents; return true; } } // namespace VirtFs diff --git a/src/fs/virtfs/virtfstools.h b/src/fs/virtfs/virtfstools.h index 4f2a77f4c..c9ff1d290 100644 --- a/src/fs/virtfs/virtfstools.h +++ b/src/fs/virtfs/virtfstools.h @@ -29,7 +29,7 @@ namespace VirtFs { - void *loadFile(const std::string &restrict fileName, + char *loadFile(const std::string &restrict fileName, int &restrict fileSize); void searchAndAddArchives(const std::string &restrict path, const std::string &restrict ext, diff --git a/src/gui/windows/updaterwindow.cpp b/src/gui/windows/updaterwindow.cpp index 6dbc03852..c4f085292 100644 --- a/src/gui/windows/updaterwindow.cpp +++ b/src/gui/windows/updaterwindow.cpp @@ -1070,11 +1070,12 @@ bool UpdaterWindow::validateFile(const std::string &filePath, unsigned long UpdaterWindow::getFileHash(const std::string &filePath) { int size = 0; - const char *const buf = static_cast( - VirtFs::loadFile(filePath, size)); - if (!buf) + char *const buf = VirtFs::loadFile(filePath, size); + if (buf == nullptr) return 0; - return Net::Download::adlerBuffer(buf, size); + unsigned long res = Net::Download::adlerBuffer(buf, size); + delete [] buf; + return res; } void UpdaterWindow::loadFile(std::string file) diff --git a/src/integrity_unittest.cc b/src/integrity_unittest.cc index 280aa1712..aea7fe073 100644 --- a/src/integrity_unittest.cc +++ b/src/integrity_unittest.cc @@ -76,7 +76,7 @@ static bool compareBuffers(const unsigned char *const buf2) { bool isCorrect(true); int sz = 0; - unsigned char *buf1 = static_cast( + unsigned char *buf1 = reinterpret_cast( VirtFs::loadFile("hide.png", sz)); REQUIRE(buf1 != nullptr); REQUIRE(sz == 368); @@ -90,7 +90,7 @@ static bool compareBuffers(const unsigned char *const buf2) buf2[f]); } } - free(buf1); + delete [] buf1; return isCorrect; } diff --git a/src/utils/translation/poparser.cpp b/src/utils/translation/poparser.cpp index a24b62343..ce317f185 100644 --- a/src/utils/translation/poparser.cpp +++ b/src/utils/translation/poparser.cpp @@ -47,12 +47,12 @@ PoParser::PoParser() : void PoParser::openFile(const std::string &name) { int size; - char *buf = static_cast(VirtFs::loadFile(getFileName(name), size)); + char *buf = VirtFs::loadFile(getFileName(name), size); if (buf) { mFile.str(std::string(buf, size)); - free(buf); + delete [] buf; } else { diff --git a/src/utils/translation/translationmanager.cpp b/src/utils/translation/translationmanager.cpp index 7c64c1590..5f9b5d587 100644 --- a/src/utils/translation/translationmanager.cpp +++ b/src/utils/translation/translationmanager.cpp @@ -93,8 +93,7 @@ bool TranslationManager::translateFile(const std::string &fileName, return false; int contentsLength; - char *fileContents = static_cast( - VirtFs::loadFile(fileName, contentsLength)); + char *fileContents = VirtFs::loadFile(fileName, contentsLength); if (!fileContents) { @@ -127,6 +126,6 @@ bool TranslationManager::translateFile(const std::string &fileName, while (getline(iss, line)) lines.push_back(line); - free(fileContents); + delete [] fileContents; return true; } diff --git a/src/utils/xml/libxml.cpp b/src/utils/xml/libxml.cpp index 87fb2577d..7abed4a09 100644 --- a/src/utils/xml/libxml.cpp +++ b/src/utils/xml/libxml.cpp @@ -98,8 +98,9 @@ namespace XML valid = true; if (useResman == UseVirtFs_true) { - data = static_cast(VirtFs::loadFile( - filename.c_str(), size)); + data = VirtFs::loadFile( + filename.c_str(), + size); } else { @@ -119,7 +120,7 @@ namespace XML else { file.seekg(0, std::ios::beg); - data = static_cast(malloc(size)); + data = new char[size]; file.read(data, size); } file.close(); @@ -133,7 +134,7 @@ namespace XML if (data) { mDoc = xmlParseMemory(data, size); - free(data); + delete [] data; if (!mDoc) { diff --git a/src/utils/xml/pugixml.cpp b/src/utils/xml/pugixml.cpp index 6fada3605..a1ca7be70 100644 --- a/src/utils/xml/pugixml.cpp +++ b/src/utils/xml/pugixml.cpp @@ -76,8 +76,9 @@ namespace XML valid = true; if (useResman == UseVirtFs_true) { - data = static_cast(VirtFs::loadFile( - filename.c_str(), size)); + data = VirtFs::loadFile( + filename.c_str(), + size); } else { @@ -97,7 +98,7 @@ namespace XML else { file.seekg(0, std::ios::beg); - data = static_cast(malloc(size)); + data = new char[size]; file.read(data, size); } file.close(); @@ -119,7 +120,7 @@ namespace XML if (result.status != pugi::status_ok) { showErrorStatus(result); - free(data); + delete [] data; } else { @@ -145,7 +146,7 @@ namespace XML if (!data) return; - char *buf = static_cast(calloc(size + 1, 1)); + char *buf = new char[size + 1]; strncpy(buf, data, size); buf[size] = 0; pugi::xml_parse_result result = mDoc.load_buffer_inplace(buf, @@ -155,7 +156,7 @@ namespace XML if (result.status != pugi::status_ok) { showErrorStatus(result); - free(buf); + delete [] buf; } else { @@ -165,10 +166,8 @@ namespace XML Document::~Document() { - free(mData); + delete [] mData); mData = nullptr; -// if (mDoc) -// xmlFreeDoc(mDoc); } XmlNodePtr Document::rootNode() diff --git a/src/utils/xml/tinyxml2.cpp b/src/utils/xml/tinyxml2.cpp index 1c1f38493..7e2ef5ebb 100644 --- a/src/utils/xml/tinyxml2.cpp +++ b/src/utils/xml/tinyxml2.cpp @@ -68,8 +68,9 @@ namespace XML valid = true; if (useResman == UseVirtFs_true) { - data = static_cast(VirtFs::loadFile( - filename.c_str(), size)); + data = VirtFs::loadFile( + filename.c_str(), + size); } else { @@ -89,7 +90,7 @@ namespace XML else { file.seekg(0, std::ios::beg); - data = static_cast(malloc(size)); + data = new char[size]; file.read(data, size); } file.close(); @@ -108,7 +109,7 @@ namespace XML if (result != tinyxml2::XML_SUCCESS) { showErrorStatus(mDoc); - free(data); + delete [] data; } else { @@ -132,7 +133,7 @@ namespace XML if (!data) return; - char *buf = static_cast(calloc(size + 1, 1)); + char *buf = new char[size + 1]; strncpy(buf, data, size); buf[size] = 0; @@ -141,7 +142,7 @@ namespace XML if (result != tinyxml2::XML_SUCCESS) { showErrorStatus(mDoc); - free(buf); + delete [] buf; } else { @@ -151,7 +152,7 @@ namespace XML Document::~Document() { - free(mData); + delete [] mData; mData = nullptr; } -- cgit v1.2.3-60-g2f50