Skip to content

Commit

Permalink
[1.x] catch the DataBuffer allocation failure
Browse files Browse the repository at this point in the history
Using std::nothrow in a header risks breaking when included from a project
that defines `new` to a different signature.

For example projects using a special version of new on Windows to detect
memory leaks: https://learn.microsoft.com/en-us/cpp/c-runtime-library/find-memory-leaks-using-the-crt-library

(cherry picked from 16f0625)
  • Loading branch information
robUx4 committed Apr 16, 2023
1 parent 3cbad06 commit c1b344a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions matroska/KaxBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ class MATROSKA_DLL_API DataBuffer {
{
if (bInternalBuffer)
{
myBuffer = new (std::nothrow) binary[mySize];
if (myBuffer == nullptr)
bValidValue = false;
else
try {
myBuffer = new binary[mySize];
memcpy(myBuffer, aBuffer, mySize);
} catch (const std::bad_alloc &) {
bValidValue = false;
}
}
else
myBuffer = aBuffer;
Expand Down

0 comments on commit c1b344a

Please sign in to comment.