@@ -145,10 +145,17 @@ static VALUE parse_type_try(VALUE a) {
145145 return rbs_struct_to_ruby_value (ctx , type );
146146}
147147
148- static rbs_lexer_t * alloc_lexer_from_buffer ( rbs_allocator_t * allocator , VALUE string , rb_encoding * encoding , int start_pos , int end_pos ) {
148+ static void validate_position_range ( int start_pos , int end_pos ) {
149149 if (start_pos < 0 || end_pos < 0 ) {
150150 rb_raise (rb_eArgError , "negative position range: %d...%d" , start_pos , end_pos );
151151 }
152+ if (start_pos > end_pos ) {
153+ rb_raise (rb_eArgError , "invalid position range: %d...%d" , start_pos , end_pos );
154+ }
155+ }
156+
157+ static rbs_lexer_t * alloc_lexer_from_buffer (rbs_allocator_t * allocator , VALUE string , rb_encoding * encoding , int start_pos , int end_pos ) {
158+ validate_position_range (start_pos , end_pos );
152159
153160 const char * encoding_name = rb_enc_name (encoding );
154161
@@ -162,9 +169,7 @@ static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE st
162169}
163170
164171static rbs_parser_t * alloc_parser_from_buffer (VALUE buffer , int start_pos , int end_pos ) {
165- if (start_pos < 0 || end_pos < 0 ) {
166- rb_raise (rb_eArgError , "negative position range: %d...%d" , start_pos , end_pos );
167- }
172+ validate_position_range (start_pos , end_pos );
168173
169174 VALUE string = rb_funcall (buffer , rb_intern ("content" ), 0 );
170175 StringValue (string );
0 commit comments