Fix recursion counter check. Add control to override depth of nested … (#4953)

* Fix recursion counter check. Add control to override depth of nested objects.

* Change if-condition to `>=`
This commit is contained in:
Vladimir Glavnyy
2018-10-04 23:27:37 +07:00
committed by Wouter van Oortmerssen
parent c0698cc33f
commit 925c1d77fc
5 changed files with 38 additions and 5 deletions

View File

@@ -34,6 +34,12 @@
// This file defines the data types representing a parsed IDL (Interface
// Definition Language) / schema file.
// Limits maximum depth of nested objects.
// Prevents stack overflow while parse flatbuffers or json.
#if !defined(FLATBUFFERS_MAX_PARSING_DEPTH)
# define FLATBUFFERS_MAX_PARSING_DEPTH 64
#endif
namespace flatbuffers {
// The order of these matters for Is*() functions below.
@@ -745,10 +751,11 @@ class Parser : public ParserState {
bool SupportsVectorOfUnions() const;
Namespace *UniqueNamespace(Namespace *ns);
enum { kMaxParsingDepth = 64 };
FLATBUFFERS_CHECKED_ERROR RecurseError();
template<typename F> CheckedError Recurse(F f) {
if (++recurse_protection_counter >= kMaxParsingDepth) return RecurseError();
if (recurse_protection_counter >= (FLATBUFFERS_MAX_PARSING_DEPTH))
return RecurseError();
recurse_protection_counter++;
auto ce = f();
recurse_protection_counter--;
return ce;