From 2694bc03aac6482c0347cfc040a2df0b350b6199 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 e653b386..a6417c8f 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -89,8 +89,9 @@ filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully) Data = (GetSize() < std::numeric_limits::max()) ? static_cast(malloc(GetSize())) : nullptr; if (Data == nullptr) throw std::runtime_error("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 c9a978f0a7db75b756932348a2e7156f07b77d2e 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 a6417c8f..2457d065 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -72,25 +72,26 @@ filepos_t EbmlBinary::UpdateSize(const ShouldWrite & writeFilter, bool /* bForce 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() < std::numeric_limits::max()) ? static_cast(malloc(GetSize())) : nullptr; + Data = (SizeToRead < std::numeric_limits::max()) ? static_cast(malloc(SizeToRead)) : nullptr; if (Data == nullptr) throw std::runtime_error("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 986964323230233b9f95cac1b2c77e4f81ab4aa2 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 2457d065..a274cee3 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -90,9 +90,9 @@ filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully) Data = (SizeToRead < std::numeric_limits::max()) ? static_cast(malloc(SizeToRead)) : nullptr; if (Data == nullptr) throw std::runtime_error("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