From ae9bb2580c3e0a79496e72f79185256670abeb95 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sat, 23 Dec 2023 09:33:04 +0100 Subject: [PATCH] EbmlString::ReadFully: use automatic memory management/fewer allocations --- src/EbmlString.cpp | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/EbmlString.cpp b/src/EbmlString.cpp index 2d8774b9..02d8f58d 100644 --- a/src/EbmlString.cpp +++ b/src/EbmlString.cpp @@ -109,24 +109,20 @@ filepos_t EbmlString::ReadData(IOCallback & input, ScopeMode ReadFully) return GetSize(); if (GetSize() == 0) { - Value = ""; - SetValueIsSet(); + Value.clear(); + } else { - auto Buffer = (GetSize() + 1 < std::numeric_limits::max()) ? new (std::nothrow) char[GetSize() + 1] : nullptr; - if (Buffer == nullptr) { - // unable to store the data, skip it - input.setFilePointer(GetSize(), seek_current); - } else { - input.readFully(Buffer, GetSize()); - if (Buffer[GetSize()-1] != '\0') { - Buffer[GetSize()] = '\0'; - } - Value = Buffer; - delete [] Buffer; - SetValueIsSet(); - } + Value.resize(GetSize()); + std::memset(&Value[0], 0, GetSize()); + input.readFully(&Value[0], GetSize()); + + auto PosNull = Value.find('\0'); + if (PosNull != std::string::npos) + Value.resize(PosNull); } + SetValueIsSet(); + return GetSize(); }