From 07ba551974e4232950f6f79ac3d8403285919a16 Mon Sep 17 00:00:00 2001 From: Jonathan Marshall Date: Mon, 13 May 2013 19:27:04 +1200 Subject: [PATCH] [loadfile] free over-allocated memory from growing buffer --- xbmc/utils/FileUtils.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/xbmc/utils/FileUtils.cpp b/xbmc/utils/FileUtils.cpp index 61dd4c6979d69..948ba8fed3967 100644 --- a/xbmc/utils/FileUtils.cpp +++ b/xbmc/utils/FileUtils.cpp @@ -209,6 +209,20 @@ unsigned int CFileUtils::LoadFile(const std::string &filename, void* &outputBuff return 0; } + if (total_read + 1 < inputBuffSize) + { + /* free extra memory if more than 1 byte (cases 1 and 3) */ + unsigned char *tempinputBuff = (unsigned char *)realloc(inputBuff, total_read); + if (!tempinputBuff) + { + /* just a precaution, shouldn't really happen */ + CLog::Log(LOGERROR, "%s unable to reallocate buffer for file \"%s\"", __FUNCTION__, filename.c_str()); + free(inputBuff); + return 0; + } + inputBuff = tempinputBuff; + } + outputBuffer = (void *) inputBuff; return total_read; }