mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-16 09:12:22 +00:00
[idl_parser] Improve stack overflow protection (#6364)
* [idl_parser] Improve stack overflow protection Add stack overflow protection for Flexbuffer and nested Flatbuffer parsers. Replaces the `Recurse()` method by the new ParseDepthGuard RAII class. * Remove move operator from Parser. It was wrong decision to add move ctor and assignment into Parser class. These operators will make it extremely difficult to add constant or reference fields in the future. * Remove ';' from definition of FLATBUFFERS_DELETE_FUNC * Format code * Make this PR compatible with MSVC2010 (it doesn't support inherited ctor)
This commit is contained in:
@@ -35,7 +35,7 @@
|
||||
// Definition Language) / schema file.
|
||||
|
||||
// Limits maximum depth of nested objects.
|
||||
// Prevents stack overflow while parse flatbuffers or json.
|
||||
// Prevents stack overflow while parse scheme, or json, or flexbuffer.
|
||||
#if !defined(FLATBUFFERS_MAX_PARSING_DEPTH)
|
||||
# define FLATBUFFERS_MAX_PARSING_DEPTH 64
|
||||
#endif
|
||||
@@ -767,8 +767,8 @@ class Parser : public ParserState {
|
||||
opts(options),
|
||||
uses_flexbuffers_(false),
|
||||
source_(nullptr),
|
||||
anonymous_counter(0),
|
||||
recurse_protection_counter(0) {
|
||||
anonymous_counter_(0),
|
||||
parse_depth_counter_(0) {
|
||||
if (opts.force_defaults) { builder_.ForceDefaults(true); }
|
||||
// Start out with the empty namespace being current.
|
||||
empty_namespace_ = new Namespace();
|
||||
@@ -806,11 +806,6 @@ class Parser : public ParserState {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef FLATBUFFERS_DEFAULT_DECLARATION
|
||||
Parser(Parser&&) = default;
|
||||
Parser& operator=(Parser&&) = default;
|
||||
#endif
|
||||
|
||||
// Parse the string containing either schema or JSON data, which will
|
||||
// populate the SymbolTable's or the FlatBufferBuilder above.
|
||||
// include_paths is used to resolve any include statements, and typically
|
||||
@@ -872,6 +867,8 @@ class Parser : public ParserState {
|
||||
static bool SupportsOptionalScalars(const flatbuffers::IDLOptions &opts);
|
||||
|
||||
private:
|
||||
class ParseDepthGuard;
|
||||
|
||||
void Message(const std::string &msg);
|
||||
void Warning(const std::string &msg);
|
||||
FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t *val);
|
||||
@@ -1000,8 +997,8 @@ class Parser : public ParserState {
|
||||
|
||||
std::vector<std::pair<Value, FieldDef *>> field_stack_;
|
||||
|
||||
int anonymous_counter;
|
||||
int recurse_protection_counter;
|
||||
int anonymous_counter_;
|
||||
int parse_depth_counter_; // stack-overflow guard
|
||||
};
|
||||
|
||||
// Utility functions for multiple generators:
|
||||
|
||||
Reference in New Issue
Block a user