From c1b344acbd99343ee09a654e99a2967884dbe6fa Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Sun, 16 Apr 2023 13:41:56 +0200 Subject: [PATCH] [1.x] catch the DataBuffer allocation failure 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 16f06258157c9df4b521a6af49db45b1ea93cec0) --- matroska/KaxBlock.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/matroska/KaxBlock.h b/matroska/KaxBlock.h index 6418ba4f..3dc4fb8e 100644 --- a/matroska/KaxBlock.h +++ b/matroska/KaxBlock.h @@ -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;