Skip to content

Commit

Permalink
Fix a bad assert in hsRAMStream
Browse files Browse the repository at this point in the history
This was added because UBSan was warning that we were calling memcpy
with a null destination, and while that is technically true, it was also
a no-op because it was copying 0 bytes. A null buffer gets properly resized
with the data in the next step, so this is actually expected behaviour
that is broken by asserting too early.

Instead, detect the cases where we're calling memcpy with actual data,
and assert in those cases that the RAMStream buffer is non-null.
  • Loading branch information
dpogue committed Jun 2, 2024
1 parent 71edac8 commit 0fd697b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Sources/Plasma/CoreLib/hsStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,15 @@ uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer)

uint32_t hsRAMStream::Write(uint32_t byteCount, const void* buffer)
{
hsAssert(fVector.data(), "Trying to write to a null RAM buffer");

size_t spaceUntilEof = fVector.size() - fPosition;
if (byteCount <= spaceUntilEof) {
hsAssert(fVector.data(), "Trying to write to a null RAM buffer");
memcpy(fVector.data() + fPosition, buffer, byteCount);
} else {
memcpy(fVector.data() + fPosition, buffer, spaceUntilEof);
if (spaceUntilEof) {
hsAssert(fVector.data(), "Trying to write to a null RAM buffer");
memcpy(fVector.data() + fPosition, buffer, spaceUntilEof);
}
auto buf = static_cast<const uint8_t*>(buffer);
fVector.insert(fVector.end(), buf + spaceUntilEof, buf + byteCount);
}
Expand Down

0 comments on commit 0fd697b

Please sign in to comment.