From: Steve Lhomme <slhomme@matroska.org>
Date: Mon, 27 Nov 2017 09:48:32 +0100
Subject: Check the max size to read before actually reading

The size check waas also missing from the length parsing
---
 src/EbmlElement.cpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/EbmlElement.cpp b/src/EbmlElement.cpp
index d25abe8..b92522e 100644
--- a/src/EbmlElement.cpp
+++ b/src/EbmlElement.cpp
@@ -398,12 +398,14 @@ EbmlElement * EbmlElement::FindNextElement(IOCallback & DataStream, const EbmlSe
         memmove(&PossibleIdNSize[0],&PossibleIdNSize[1], --ReadIndex);
       }
 
+      if (MaxDataSize <= ReadSize)
+        break;
       if (DataStream.read(&PossibleIdNSize[ReadIndex++], 1) == 0) {
         return NULL; // no more data ?
       }
       ReadSize++;
 
-    } while (!bFound && MaxDataSize > ReadSize);
+    } while (!bFound);
 
     SizeIdx = ReadIndex;
     ReadIndex -= PossibleID_Length;
@@ -422,6 +424,10 @@ EbmlElement * EbmlElement::FindNextElement(IOCallback & DataStream, const EbmlSe
         bFound = false;
         break;
       }
+      if (MaxDataSize <= ReadSize) {
+        bFound = false;
+        break;
+      }
       if( DataStream.read( &PossibleIdNSize[SizeIdx++], 1 ) == 0 ) {
         return NULL; // no more data ?
       }
