helpers/readFile: limit memory allocations

Origin commit data
------------------
Commit: 2f39081708
Author: Stefan Seyfried <seife@tuxbox-git.slipkontur.de>
Date: 2017-10-01 (Sun, 01 Oct 2017)
This commit is contained in:
Stefan Seyfried
2017-10-01 14:38:52 +02:00
committed by Jacek Jendrzej
parent c9d4fdd9cc
commit 80df05e950

View File

@@ -1455,8 +1455,16 @@ string readFile(string file)
if (tmpData.is_open()) { if (tmpData.is_open()) {
tmpData.seekg(0, tmpData.end); tmpData.seekg(0, tmpData.end);
int length = tmpData.tellg(); int length = tmpData.tellg();
if (length > 0xffff) { /* longer than 64k? better read in chunks! */
cerr << __func__ << ": file " << file << " too big (" << length << " bytes)" << endl;
return "";
}
tmpData.seekg(0, tmpData.beg); tmpData.seekg(0, tmpData.beg);
char* buffer = new char[length+1]; char* buffer = new char[length+1];
if (! buffer) {
cerr << __func__ << ": allocating " << (length + 1) << " bytes for buffer failed" << endl;
return "";
}
tmpData.read(buffer, length); tmpData.read(buffer, length);
tmpData.close(); tmpData.close();
buffer[length] = '\0'; buffer[length] = '\0';