Commits

Bart van Strien  committed 8690881

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

  • Participants
  • Parent commits 16bc7c0

Comments (0)

Files changed (1)

File src/modules/filesystem/physfs/File.cpp

 	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();