diff --git a/src/openrct2/core/FileStream.cpp b/src/openrct2/core/FileStream.cpp index d3abb0e07a..63dea436dd 100644 --- a/src/openrct2/core/FileStream.cpp +++ b/src/openrct2/core/FileStream.cpp @@ -13,6 +13,7 @@ #include "Path.hpp" #include "String.hpp" +#include #include #ifndef _WIN32 @@ -207,11 +208,17 @@ namespace OpenRCT2 } throw IOException("Attempted to read past end of file."); } + uint64_t position = GetPosition(); if (fread(buffer, 1, static_cast(length), _file) == length) { return; } - throw IOException("Attempted to read past end of file."); + char msg[256]; + std::snprintf( + msg, sizeof(msg), + "Unable to read %" PRIu64 " bytes from file. Position: %" PRIu64 ", FileSize: %" PRIu64 ", feof = %d, ferror = %d", + length, position, _fileSize, feof(_file), ferror(_file)); + throw IOException(msg); } void FileStream::Write(const void* buffer, uint64_t length) @@ -224,15 +231,19 @@ namespace OpenRCT2 { return; } + uint64_t position = GetPosition(); if (auto count = fwrite(buffer, static_cast(length), 1, _file); count != 1) { - std::string error = "Unable to write " + std::to_string(length) + " bytes to file. Count = " + std::to_string(count) - + ", errno = " + std::to_string(errno); - throw IOException(error); + char msg[256]; + std::snprintf( + msg, sizeof(msg), + "Unable to write %" PRIu64 " bytes to file. Count = %zu, Position = %" PRIu64 ", FileSize = %" PRIu64 + ", feof = %d, ferror = %d", + length, count, position, _fileSize, feof(_file), ferror(_file)); + throw IOException(msg); } - uint64_t position = GetPosition(); - _fileSize = std::max(_fileSize, position); + _fileSize = std::max(_fileSize, position + length); } uint64_t FileStream::TryRead(void* buffer, uint64_t length) diff --git a/src/openrct2/core/MemoryStream.cpp b/src/openrct2/core/MemoryStream.cpp index de3d357470..fe99700d7c 100644 --- a/src/openrct2/core/MemoryStream.cpp +++ b/src/openrct2/core/MemoryStream.cpp @@ -12,6 +12,7 @@ #include "Guard.hpp" #include "Memory.hpp" +#include #include namespace OpenRCT2 @@ -100,7 +101,12 @@ namespace OpenRCT2 void MemoryStream::SetPosition(uint64_t position) { if (position > _dataSize) - throw IOException("New position out of bounds."); + { + char msg[256]; + std::snprintf( + msg, sizeof(msg), "Attempted to set position to %" PRIu64 ", stream length %zu.", position, _dataSize); + throw IOException(msg); + } _position = static_cast(position); } @@ -124,7 +130,13 @@ namespace OpenRCT2 void MemoryStream::Read(void* buffer, uint64_t length) { if (_position + length > _dataSize) - throw IOException("Attempted to read past end of stream."); + { + char msg[256]; + std::snprintf( + msg, sizeof(msg), "Attempted to read past end of stream. Position: %zu, Length: %" PRIu64 ", DataSize: %zu.", + _position, length, _dataSize); + throw IOException(msg); + } std::memcpy(buffer, _data + _position, length); _position += static_cast(length); @@ -155,7 +167,13 @@ namespace OpenRCT2 const void* MemoryStream::ReadDirect(size_t length) { if (_position + length > _dataSize) - throw IOException("Attempted to read past end of stream."); + { + char msg[256]; + std::snprintf( + msg, sizeof(msg), "Attempted to read past end of stream. Position: %zu, Length: %zu, DataSize: %zu.", _position, + length, _dataSize); + throw IOException(msg); + } const void* readPosition = _data + _position; _position += length; diff --git a/src/openrct2/core/MemoryStream.h b/src/openrct2/core/MemoryStream.h index ad0e7fc387..af0eed524a 100644 --- a/src/openrct2/core/MemoryStream.h +++ b/src/openrct2/core/MemoryStream.h @@ -13,6 +13,7 @@ #include "IStream.hpp" #include +#include #include #include @@ -90,7 +91,13 @@ namespace OpenRCT2 void Read(void* buffer) { if (_position + N > _dataSize) - throw IOException("Attempted to read past end of stream."); + { + char message[256]; + std::snprintf( + message, sizeof(message), "Attempted to read past end of stream. Position: %zu, Length: %zu, DataSize: %zu", + _position, N, _dataSize); + throw IOException(message); + } std::memcpy(buffer, _data + _position, N); _position += static_cast(N);