@@ -123,7 +123,7 @@ enum {
123
123
#endif
124
124
};
125
125
126
- enum { INDENT_WIDTH = 2 };
126
+ enum { INDENT_WIDTH = 2 , DEFAULT_MAX_DEPTHS = 100 };
127
127
128
128
struct null {};
129
129
@@ -832,7 +832,7 @@ template <typename Context, typename Iter> inline bool _parse_object(Context &ct
832
832
return false ;
833
833
}
834
834
if (in.expect (' }' )) {
835
- return true ;
835
+ return ctx. parse_object_stop () ;
836
836
}
837
837
do {
838
838
std::string key;
@@ -843,7 +843,7 @@ template <typename Context, typename Iter> inline bool _parse_object(Context &ct
843
843
return false ;
844
844
}
845
845
} while (in.expect (' ,' ));
846
- return in.expect (' }' );
846
+ return in.expect (' }' ) && ctx. parse_object_stop () ;
847
847
}
848
848
849
849
template <typename Iter> inline std::string _parse_number (input<Iter> &in) {
@@ -959,9 +959,10 @@ class deny_parse_context {
959
959
class default_parse_context {
960
960
protected:
961
961
value *out_;
962
+ size_t depths_;
962
963
963
964
public:
964
- default_parse_context (value *out) : out_(out) {
965
+ default_parse_context (value *out, size_t depths = DEFAULT_MAX_DEPTHS ) : out_(out), depths_(depths ) {
965
966
}
966
967
bool set_null () {
967
968
*out_ = value ();
@@ -986,42 +987,55 @@ class default_parse_context {
986
987
return _parse_string (out_->get <std::string>(), in);
987
988
}
988
989
bool parse_array_start () {
990
+ if (depths_ == 0 )
991
+ return false ;
992
+ --depths_;
989
993
*out_ = value (array_type, false );
990
994
return true ;
991
995
}
992
996
template <typename Iter> bool parse_array_item (input<Iter> &in, size_t ) {
993
997
array &a = out_->get <array>();
994
998
a.push_back (value ());
995
- default_parse_context ctx (&a.back ());
999
+ default_parse_context ctx (&a.back (), depths_ );
996
1000
return _parse (ctx, in);
997
1001
}
998
1002
bool parse_array_stop (size_t ) {
1003
+ ++depths_;
999
1004
return true ;
1000
1005
}
1001
1006
bool parse_object_start () {
1007
+ if (depths_ == 0 )
1008
+ return false ;
1002
1009
*out_ = value (object_type, false );
1003
1010
return true ;
1004
1011
}
1005
1012
template <typename Iter> bool parse_object_item (input<Iter> &in, const std::string &key) {
1006
1013
object &o = out_->get <object>();
1007
- default_parse_context ctx (&o[key]);
1014
+ default_parse_context ctx (&o[key], depths_ );
1008
1015
return _parse (ctx, in);
1009
1016
}
1017
+ bool parse_object_stop () {
1018
+ ++depths_;
1019
+ return true ;
1020
+ }
1010
1021
1011
1022
private:
1012
1023
default_parse_context (const default_parse_context &);
1013
1024
default_parse_context &operator =(const default_parse_context &);
1014
1025
};
1015
1026
1016
1027
class null_parse_context {
1028
+ protected:
1029
+ size_t depths_;
1030
+
1017
1031
public:
1018
1032
struct dummy_str {
1019
1033
void push_back (int ) {
1020
1034
}
1021
1035
};
1022
1036
1023
1037
public:
1024
- null_parse_context () {
1038
+ null_parse_context (size_t depths = DEFAULT_MAX_DEPTHS) : depths_(depths ) {
1025
1039
}
1026
1040
bool set_null () {
1027
1041
return true ;
@@ -1042,20 +1056,31 @@ class null_parse_context {
1042
1056
return _parse_string (s, in);
1043
1057
}
1044
1058
bool parse_array_start () {
1059
+ if (depths_ == 0 )
1060
+ return false ;
1061
+ --depths_;
1045
1062
return true ;
1046
1063
}
1047
1064
template <typename Iter> bool parse_array_item (input<Iter> &in, size_t ) {
1048
1065
return _parse (*this , in);
1049
1066
}
1050
1067
bool parse_array_stop (size_t ) {
1068
+ ++depths_;
1051
1069
return true ;
1052
1070
}
1053
1071
bool parse_object_start () {
1072
+ if (depths_ == 0 )
1073
+ return false ;
1074
+ --depths_;
1054
1075
return true ;
1055
1076
}
1056
1077
template <typename Iter> bool parse_object_item (input<Iter> &in, const std::string &) {
1078
+ ++depths_;
1057
1079
return _parse (*this , in);
1058
1080
}
1081
+ bool parse_object_stop () {
1082
+ return true ;
1083
+ }
1059
1084
1060
1085
private:
1061
1086
null_parse_context (const null_parse_context &);
0 commit comments