@@ -174,6 +174,16 @@ namespace tao
174174 }
175175 }
176176
177+ template < typename Input >
178+ static std::size_t read_size ( Input& in )
179+ {
180+ const auto s = read_unsigned ( in );
181+ if ( s > static_cast < std::uint64_t >( std::numeric_limits< std::size_t >::max () ) ) {
182+ throw json_pegtl::parse_error ( " size too large for 32-bit platform" , in );
183+ }
184+ return static_cast < std::size_t >( s );
185+ }
186+
177187 template < typename Input, typename Consumer >
178188 static bool match_unsigned ( Input& in, Consumer& consumer )
179189 {
@@ -198,8 +208,8 @@ namespace tao
198208 static Result read_string_1 ( Input& in )
199209 {
200210 using value_t = typename Result::value_type;
201- const auto size = read_unsigned ( in );
202- if ( static_cast < std:: uint64_t >( in.size ( size ) ) < size ) {
211+ const auto size = read_size ( in );
212+ if ( in.size ( size ) < size ) {
203213 throw json_pegtl::parse_error ( " unexpected end of input" , in );
204214 }
205215 const value_t * pointer = reinterpret_cast < const value_t * >( in.current () );
@@ -219,8 +229,8 @@ namespace tao
219229 if ( internal::peek_major ( in ) != m ) {
220230 throw json_pegtl::parse_error ( " non-matching fragment in indefinite length string" , in ); // "String" is text or byte string in RFC 7049 terminology.
221231 }
222- const auto size = read_unsigned ( in );
223- if ( static_cast < std:: uint64_t >( in.size ( size ) ) < size ) {
232+ const auto size = read_size ( in );
233+ if ( in.size ( size ) < size ) {
224234 throw json_pegtl::parse_error ( " unexpected end of input" , in );
225235 }
226236 const value_t * pointer = reinterpret_cast < const value_t * >( in.current () );
@@ -262,9 +272,9 @@ namespace tao
262272 template < typename Input, typename Consumer >
263273 static void match_array_1 ( Input& in, Consumer& consumer )
264274 {
265- const auto size = read_unsigned ( in );
275+ const auto size = read_size ( in );
266276 consumer.begin_array ( size );
267- for ( std::uint64_t i = 0 ; i < size; ++i ) {
277+ for ( std::size_t i = 0 ; i < size; ++i ) {
268278 internal::throw_on_empty ( in );
269279 match_impl ( in, consumer );
270280 consumer.element ();
@@ -300,9 +310,9 @@ namespace tao
300310 template < typename Input, typename Consumer >
301311 static void match_object_1 ( Input& in, Consumer& consumer )
302312 {
303- const auto size = read_unsigned ( in );
313+ const auto size = read_size ( in );
304314 consumer.begin_object ( size );
305- for ( std::uint64_t i = 0 ; i < size; ++i ) {
315+ for ( std::size_t i = 0 ; i < size; ++i ) {
306316 if ( internal::peek_major_safe ( in ) != major::STRING ) {
307317 throw json_pegtl::parse_error ( " non-string object key" , in );
308318 }
0 commit comments