diff options
Diffstat (limited to 'src/google/protobuf/util/internal/json_stream_parser.h')
-rw-r--r-- | src/google/protobuf/util/internal/json_stream_parser.h | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/google/protobuf/util/internal/json_stream_parser.h b/src/google/protobuf/util/internal/json_stream_parser.h index 31933b67..4a065d27 100644 --- a/src/google/protobuf/util/internal/json_stream_parser.h +++ b/src/google/protobuf/util/internal/json_stream_parser.h @@ -38,11 +38,11 @@ #include <google/protobuf/stubs/stringpiece.h> #include <google/protobuf/stubs/status.h> -namespace google { namespace util { class Status; } // namespace util +namespace google { namespace protobuf { namespace util { namespace converter { @@ -83,7 +83,18 @@ class LIBPROTOBUF_EXPORT JsonStreamParser { util::Status FinishParse(); + // Sets the max recursion depth of JSON message to be deserialized. JSON + // messages over this depth will fail to be deserialized. + // Default value is 100. + void set_max_recursion_depth(int max_depth) { + max_recursion_depth_ = max_depth; + } + private: + friend class JsonStreamParserTest; + // Return the current recursion depth. + const int recursion_depth() { return recursion_depth_; } + enum TokenType { BEGIN_STRING, // " or ' BEGIN_NUMBER, // - or digit @@ -195,6 +206,11 @@ class LIBPROTOBUF_EXPORT JsonStreamParser { // status to return in this case. util::Status ReportUnknown(StringPiece message); + // Helper function to check recursion depth and increment it. It will return + // Status::OK if the current depth is allowed. Otherwise an error is returned. + // key is used for error reporting. + util::Status IncrementRecursionDepth(StringPiece key) const; + // Advance p_ past all whitespace or until the end of the string. void SkipWhitespace(); @@ -261,12 +277,18 @@ class LIBPROTOBUF_EXPORT JsonStreamParser { // Whether allows out-of-range floating point numbers or reject them. bool loose_float_number_conversion_; + // Tracks current recursion depth. + mutable int recursion_depth_; + + // Maximum allowed recursion depth. + int max_recursion_depth_; + GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(JsonStreamParser); }; } // namespace converter } // namespace util } // namespace protobuf - } // namespace google + #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_JSON_STREAM_PARSER_H__ |