Apply patch that fixes File:read from reading too much (on EOF) (bug #530)

This commit is contained in:
Bart van Strien
2012-12-15 13:58:34 +01:00
parent 62e2c752e5
commit b10ff8da42
+24 -3
View File
@@ -118,13 +118,34 @@ FileData *File::read(int64 size)
if (!isOpen && !open(READ))
throw love::Exception("Could not read file %s.", filename.c_str());
int64 max = (int64)PHYSFS_fileLength(file);
int64 max = getSize();
int64 cur = tell();
size = (size == ALL) ? max : size;
size = (size > max) ? max : size;
// Clamping because the file offset may be in a weird position.
if (cur < 0)
cur = 0;
else if (cur > max)
cur = max;
if (cur + size > max)
size = max - cur;
FileData *fileData = new FileData(size, getFilename());
int64 bytesRead = read(fileData->getData(), size);
read(fileData->getData(), size);
if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size))
{
delete fileData;
throw love::Exception("Could not read from file.");
}
if (bytesRead < size)
{
FileData *tmpFileData = new FileData(bytesRead, getFilename());
memcpy(tmpFileData->getData(), fileData->getData(), (size_t) bytesRead);
delete fileData;
fileData = tmpFileData;
}
if (!isOpen)
close();