Skip to content

Commit

Permalink
MemIOCallback: fix buffer overflow when writing too much data
Browse files Browse the repository at this point in the history
If the addition of 2 positive values is smaller than one of the values then we
have an overflowing addition.

In this case we will not be able to allocate that much, just return a size
written as 0.

(cherry picked from commit 2d5c11c)
Signed-off-by: Steve Lhomme <slhomme@matroska.org>
  • Loading branch information
robUx4 committed Nov 4, 2023
1 parent 4c0d757 commit f3249a7
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/MemIOCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,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 @@ -110,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

0 comments on commit f3249a7

Please sign in to comment.