Permalink
Browse files

[loadfile] free over-allocated memory from growing buffer

  • Loading branch information...
1 parent 1a2e6ae commit 07ba551974e4232950f6f79ac3d8403285919a16 Jonathan Marshall committed with May 13, 2013
Showing with 14 additions and 0 deletions.
  1. +14 −0 xbmc/utils/FileUtils.cpp
View
@@ -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;
}

0 comments on commit 07ba551

Please sign in to comment.