Skip to content

Commit a033874

Browse files
committed
contrib: ebml: fix leak of upper elements
This can happen in damaged files. Undamaged files are unaffected. Sent for review upstream in Matroska-Org/libebml#321 / Matroska-Org/libebml#322 (cherry picked from commit cace91a)
1 parent 8c62ca2 commit a033874

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
From 3660273b4a6b71f9678da744f86a2035fd98fcc2 Mon Sep 17 00:00:00 2001
2+
From: Steve Lhomme <robux4@ycbcr.xyz>
3+
Date: Fri, 25 Jul 2025 14:37:18 +0200
4+
Subject: [PATCH] EbmlMaster: fix leak when reading upper level elements
5+
6+
When an element from an upper level is found we go up the caller chain,
7+
passing the found element but it was not actually used (added to a list or freed).
8+
9+
This patch allows setting that element as the ElementLevelA found in the loop.
10+
We skip the call the inDataStream.FindNextElement() to find it.
11+
12+
The new MaxSizeToRead is the size to read in the next inDataStream.FindNextElement() call.
13+
14+
The old MaxSizeToRead <= 0 code seems bogus as it would exit the loop
15+
to find elements for that EbmlMaster even though there might still be elements to read.
16+
---
17+
src/EbmlMaster.cpp | 5 ++++-
18+
1 file changed, 4 insertions(+), 1 deletion(-)
19+
20+
diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp
21+
index 071eb5d..465560f 100644
22+
--- a/src/EbmlMaster.cpp
23+
+++ b/src/EbmlMaster.cpp
24+
@@ -455,9 +455,12 @@ void EbmlMaster::Read(EbmlStream & inDataStream, const EbmlSemanticContext & sCo
25+
26+
if (UpperEltFound > 0) {
27+
UpperEltFound--;
28+
- if (UpperEltFound > 0 || MaxSizeToRead <= 0)
29+
+ if (UpperEltFound > 0)
30+
goto processCrc;
31+
ElementLevelA = FoundElt;
32+
+ if (IsFiniteSize() && ElementLevelA->IsFiniteSize()) {
33+
+ MaxSizeToRead = GetEndPosition() - ElementLevelA->GetEndPosition(); // even if it's the default value
34+
+ }
35+
continue;
36+
}
37+
38+
--
39+
2.45.1.windows.1
40+

contrib/src/ebml/rules.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ebml: libebml-$(EBML_VERSION).tar.xz .sum-ebml
1616
$(UNPACK)
1717
$(APPLY) $(SRC)/ebml/0001-EbmlString-ReadFully-use-automatic-memory-management.patch
1818
$(APPLY) $(SRC)/ebml/0002-EbmlUnicodeString-use-std-string-when-reading-instea.patch
19+
$(APPLY) $(SRC)/ebml/0001-EbmlMaster-fix-leak-when-reading-upper-level-element.patch
1920
$(MOVE)
2021

2122
.ebml: ebml toolchain.cmake

0 commit comments

Comments
 (0)