From 8e146383aab27f8ce4ad5b6c06f955f5195bf309 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 28 Feb 2025 07:52:17 +0100 Subject: [PATCH 1/3] EbmlBinary: only set the Set flag when the whole data are read Otherwise the binary data are not fully usable. --- src/EbmlBinary.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/EbmlBinary.cpp b/src/EbmlBinary.cpp index a465c2fa..b537d502 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -100,8 +100,9 @@ filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully) Data = (GetSize() < SIZE_MAX) ? static_cast(malloc(GetSize())) : nullptr; if (Data == nullptr) throw CRTError(std::string("Error allocating data")); - SetValueIsSet(); - return input.read(Data, GetSize()); + filepos_t read = input.read(Data, GetSize()); + SetValueIsSet(read == GetSize()); + return read; } bool EbmlBinary::operator==(const EbmlBinary & ElementToCompare) const From dc768dbb0f05369c8fcd464e613a44b7aa59685d Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 28 Feb 2025 07:55:26 +0100 Subject: [PATCH 2/3] EbmlBinary: cache the size we want to read --- src/EbmlBinary.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/EbmlBinary.cpp b/src/EbmlBinary.cpp index b537d502..1899928a 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -83,25 +83,26 @@ uint64 EbmlBinary::UpdateSize(bool /* bWithDefault */, bool /* bForceRender */) filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully) { + const auto SizeToRead = GetSize(); if (Data != nullptr) { free(Data); Data = nullptr; } if (ReadFully == SCOPE_NO_DATA) { - return GetSize(); + return SizeToRead; } - if (!GetSize()) { + if (!SizeToRead) { SetValueIsSet(); return 0; } - Data = (GetSize() < SIZE_MAX) ? static_cast(malloc(GetSize())) : nullptr; + Data = (SizeToRead < SIZE_MAX) ? static_cast(malloc(SizeToRead)) : nullptr; if (Data == nullptr) throw CRTError(std::string("Error allocating data")); - filepos_t read = input.read(Data, GetSize()); - SetValueIsSet(read == GetSize()); + filepos_t read = input.read(Data, SizeToRead); + SetValueIsSet(read == SizeToRead); return read; } From b0bf26908543acec94fe53b28d8d44262fc5744e Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 28 Feb 2025 08:07:44 +0100 Subject: [PATCH 3/3] EbmlBinary: use readFully() to make sure we read the whole data read() may only provide partial data. We don't need to check for error, readFully() will throw if the data are not fully read. --- src/EbmlBinary.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/EbmlBinary.cpp b/src/EbmlBinary.cpp index 1899928a..8e436c4c 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -101,9 +101,9 @@ filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully) Data = (SizeToRead < SIZE_MAX) ? static_cast(malloc(SizeToRead)) : nullptr; if (Data == nullptr) throw CRTError(std::string("Error allocating data")); - filepos_t read = input.read(Data, SizeToRead); - SetValueIsSet(read == SizeToRead); - return read; + input.readFully(Data, SizeToRead); + SetValueIsSet(); + return SizeToRead; } bool EbmlBinary::operator==(const EbmlBinary & ElementToCompare) const