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/virtfs/virtfstools.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/fs/virtfs/virtfstools.cpp') 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 -- cgit v1.2.3-60-g2f50