diff src/modules/filesystem/physfs/File.cpp @ 1166:86908814f2d8

Apply patch that fixes File:read from reading too much (on EOF) (bug #530)
author Bart van Strien <bart.bes+projects@gmail.com>
date Sat, 15 Dec 2012 13:58:34 +0100
parents d8c70acb666e
children b7f097053d74
line wrap: on
line diff
--- a/src/modules/filesystem/physfs/File.cpp	Sat Dec 01 14:00:05 2012 +0100
+++ b/src/modules/filesystem/physfs/File.cpp	Sat Dec 15 13:58:34 2012 +0100
@@ -118,13 +118,34 @@
 	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();