Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Fix MemIOCallback buffer overflows #149

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/MemIOCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ uint32 MemIOCallback::read(void *Buffer, size_t Size)
if (Buffer == nullptr || Size < 1)
return 0;
//If the size is larger than than the amount left in the buffer
if (Size + dataBufferPos > dataBufferTotalSize) {
if (Size + dataBufferPos < Size || // overflow, reading too much
Size + dataBufferPos > dataBufferTotalSize) {
//We will only return the remaining data
memcpy(Buffer, dataBuffer + dataBufferPos, dataBufferTotalSize - dataBufferPos);
uint64 oldDataPos = dataBufferPos;
Expand All @@ -95,6 +96,8 @@ void MemIOCallback::setFilePointer(int64 Offset, seek_mode Mode)

size_t MemIOCallback::write(const void *Buffer, size_t Size)
{
if (dataBufferPos + Size < Size) // overflow, we can't hold that much
return 0;
if (dataBufferMemorySize < dataBufferPos + Size) {
//We need more memory!
dataBuffer = static_cast<binary *>(realloc(static_cast<void *>(dataBuffer), dataBufferPos + Size));
Expand All @@ -109,6 +112,8 @@ size_t MemIOCallback::write(const void *Buffer, size_t Size)

uint32 MemIOCallback::write(IOCallback & IOToRead, size_t Size)
{
if (dataBufferPos + Size < Size) // overflow, we can't hold that much
return 0;
if (dataBufferMemorySize < dataBufferPos + Size) {
//We need more memory!
dataBuffer = static_cast<binary *>(realloc(static_cast<void *>(dataBuffer), dataBufferPos + Size));
Expand Down