@@ -393,7 +393,7 @@ RBIMPL_ATTR_NORETURN()
393
393
#endif
394
394
static void raise_parse_error (const char * format , JSON_ParserState * state )
395
395
{
396
- unsigned char buffer [PARSE_ERROR_FRAGMENT_LEN + 1 ];
396
+ unsigned char buffer [PARSE_ERROR_FRAGMENT_LEN + 3 ];
397
397
398
398
const char * cursor = state -> cursor ;
399
399
long column = 0 ;
@@ -412,22 +412,34 @@ static void raise_parse_error(const char *format, JSON_ParserState *state)
412
412
}
413
413
}
414
414
415
- const char * ptr = state -> cursor ;
416
- size_t len = ptr ? strnlen (ptr , PARSE_ERROR_FRAGMENT_LEN ) : 0 ;
415
+ const char * ptr = "EOF" ;
416
+ if (state -> cursor && state -> cursor < state -> end ) {
417
+ ptr = state -> cursor ;
418
+ size_t len = 0 ;
419
+ while (len < PARSE_ERROR_FRAGMENT_LEN ) {
420
+ char ch = ptr [len ];
421
+ if (!ch || ch == '\n' || ch == ' ' || ch == '\t' || ch == '\r' ) {
422
+ break ;
423
+ }
424
+ len ++ ;
425
+ }
417
426
418
- if (len == PARSE_ERROR_FRAGMENT_LEN ) {
419
- MEMCPY (buffer , ptr , char , PARSE_ERROR_FRAGMENT_LEN );
427
+ if (len ) {
428
+ buffer [0 ] = '\'' ;
429
+ MEMCPY (buffer + 1 , ptr , char , len );
420
430
421
- while (buffer [len - 1 ] >= 0x80 && buffer [len - 1 ] < 0xC0 ) { // Is continuation byte
422
- len -- ;
423
- }
431
+ while (buffer [len ] >= 0x80 && buffer [len ] < 0xC0 ) { // Is continuation byte
432
+ len -- ;
433
+ }
424
434
425
- if (buffer [len - 1 ] >= 0xC0 ) { // multibyte character start
426
- len -- ;
427
- }
435
+ if (buffer [len ] >= 0xC0 ) { // multibyte character start
436
+ len -- ;
437
+ }
428
438
429
- buffer [len ] = '\0' ;
430
- ptr = (const char * )buffer ;
439
+ buffer [len + 1 ] = '\'' ;
440
+ buffer [len + 2 ] = '\0' ;
441
+ ptr = (const char * )buffer ;
442
+ }
431
443
}
432
444
433
445
VALUE msg = rb_sprintf (format , ptr );
@@ -473,16 +485,16 @@ static uint32_t unescape_unicode(JSON_ParserState *state, const unsigned char *p
473
485
signed char b ;
474
486
uint32_t result = 0 ;
475
487
b = digit_values [p [0 ]];
476
- if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at '%s' " , state , (char * )p - 2 );
488
+ if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at %s " , state , (char * )p - 2 );
477
489
result = (result << 4 ) | (unsigned char )b ;
478
490
b = digit_values [p [1 ]];
479
- if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at '%s' " , state , (char * )p - 2 );
491
+ if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at %s " , state , (char * )p - 2 );
480
492
result = (result << 4 ) | (unsigned char )b ;
481
493
b = digit_values [p [2 ]];
482
- if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at '%s' " , state , (char * )p - 2 );
494
+ if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at %s " , state , (char * )p - 2 );
483
495
result = (result << 4 ) | (unsigned char )b ;
484
496
b = digit_values [p [3 ]];
485
- if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at '%s' " , state , (char * )p - 2 );
497
+ if (b < 0 ) raise_parse_error_at ("incomplete unicode character escape sequence at %s " , state , (char * )p - 2 );
486
498
result = (result << 4 ) | (unsigned char )b ;
487
499
return result ;
488
500
}
@@ -532,11 +544,11 @@ json_eat_comments(JSON_ParserState *state)
532
544
break ;
533
545
}
534
546
default :
535
- raise_parse_error ("unexpected token '%s' " , state );
547
+ raise_parse_error ("unexpected token %s " , state );
536
548
break ;
537
549
}
538
550
} else {
539
- raise_parse_error ("unexpected token '%s' " , state );
551
+ raise_parse_error ("unexpected token %s " , state );
540
552
}
541
553
}
542
554
@@ -655,7 +667,7 @@ static VALUE json_string_unescape(JSON_ParserState *state, const char *string, c
655
667
break ;
656
668
case 'u' :
657
669
if (pe > stringEnd - 5 ) {
658
- raise_parse_error_at ("incomplete unicode character escape sequence at '%s' " , state , p );
670
+ raise_parse_error_at ("incomplete unicode character escape sequence at %s " , state , p );
659
671
} else {
660
672
uint32_t ch = unescape_unicode (state , (unsigned char * ) ++ pe );
661
673
pe += 3 ;
@@ -672,7 +684,7 @@ static VALUE json_string_unescape(JSON_ParserState *state, const char *string, c
672
684
if ((ch & 0xFC00 ) == 0xD800 ) {
673
685
pe ++ ;
674
686
if (pe > stringEnd - 6 ) {
675
- raise_parse_error_at ("incomplete surrogate pair at '%s' " , state , p );
687
+ raise_parse_error_at ("incomplete surrogate pair at %s " , state , p );
676
688
}
677
689
if (pe [0 ] == '\\' && pe [1 ] == 'u' ) {
678
690
uint32_t sur = unescape_unicode (state , (unsigned char * ) pe + 2 );
@@ -894,15 +906,15 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
894
906
return json_push_value (state , config , Qnil );
895
907
}
896
908
897
- raise_parse_error ("unexpected token '%s' " , state );
909
+ raise_parse_error ("unexpected token %s " , state );
898
910
break ;
899
911
case 't' :
900
912
if ((state -> end - state -> cursor >= 4 ) && (memcmp (state -> cursor , "true" , 4 ) == 0 )) {
901
913
state -> cursor += 4 ;
902
914
return json_push_value (state , config , Qtrue );
903
915
}
904
916
905
- raise_parse_error ("unexpected token '%s' " , state );
917
+ raise_parse_error ("unexpected token %s " , state );
906
918
break ;
907
919
case 'f' :
908
920
// Note: memcmp with a small power of two compile to an integer comparison
@@ -911,7 +923,7 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
911
923
return json_push_value (state , config , Qfalse );
912
924
}
913
925
914
- raise_parse_error ("unexpected token '%s' " , state );
926
+ raise_parse_error ("unexpected token %s " , state );
915
927
break ;
916
928
case 'N' :
917
929
// Note: memcmp with a small power of two compile to an integer comparison
@@ -920,15 +932,15 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
920
932
return json_push_value (state , config , CNaN );
921
933
}
922
934
923
- raise_parse_error ("unexpected token '%s' " , state );
935
+ raise_parse_error ("unexpected token %s " , state );
924
936
break ;
925
937
case 'I' :
926
938
if (config -> allow_nan && (state -> end - state -> cursor >= 8 ) && (memcmp (state -> cursor , "Infinity" , 8 ) == 0 )) {
927
939
state -> cursor += 8 ;
928
940
return json_push_value (state , config , CInfinity );
929
941
}
930
942
931
- raise_parse_error ("unexpected token '%s' " , state );
943
+ raise_parse_error ("unexpected token %s " , state );
932
944
break ;
933
945
case '-' :
934
946
// Note: memcmp with a small power of two compile to an integer comparison
@@ -937,7 +949,7 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
937
949
state -> cursor += 9 ;
938
950
return json_push_value (state , config , CMinusInfinity );
939
951
} else {
940
- raise_parse_error ("unexpected token '%s' " , state );
952
+ raise_parse_error ("unexpected token %s " , state );
941
953
}
942
954
}
943
955
// Fallthrough
@@ -1062,7 +1074,7 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
1062
1074
}
1063
1075
1064
1076
if (* state -> cursor != '"' ) {
1065
- raise_parse_error ("expected object key, got '%s' " , state );
1077
+ raise_parse_error ("expected object key, got %s " , state );
1066
1078
}
1067
1079
json_parse_string (state , config , true);
1068
1080
@@ -1097,13 +1109,13 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
1097
1109
}
1098
1110
1099
1111
if (* state -> cursor != '"' ) {
1100
- raise_parse_error ("expected object key, got: '%s' " , state );
1112
+ raise_parse_error ("expected object key, got: %s " , state );
1101
1113
}
1102
1114
json_parse_string (state , config , true);
1103
1115
1104
1116
json_eat_whitespace (state );
1105
1117
if ((state -> cursor >= state -> end ) || (* state -> cursor != ':' )) {
1106
- raise_parse_error ("expected ':' after object key, got: '%s' " , state );
1118
+ raise_parse_error ("expected ':' after object key, got: %s " , state );
1107
1119
}
1108
1120
state -> cursor ++ ;
1109
1121
@@ -1113,24 +1125,24 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
1113
1125
}
1114
1126
}
1115
1127
1116
- raise_parse_error ("expected ',' or '}' after object value, got: '%s' " , state );
1128
+ raise_parse_error ("expected ',' or '}' after object value, got: %s " , state );
1117
1129
}
1118
1130
break ;
1119
1131
}
1120
1132
1121
1133
default :
1122
- raise_parse_error ("unexpected character: '%s' " , state );
1134
+ raise_parse_error ("unexpected character: %s " , state );
1123
1135
break ;
1124
1136
}
1125
1137
1126
- raise_parse_error ("unreacheable: '%s' " , state );
1138
+ raise_parse_error ("unreacheable: %s " , state );
1127
1139
}
1128
1140
1129
1141
static void json_ensure_eof (JSON_ParserState * state )
1130
1142
{
1131
1143
json_eat_whitespace (state );
1132
1144
if (state -> cursor != state -> end ) {
1133
- raise_parse_error ("unexpected token at end of stream '%s' " , state );
1145
+ raise_parse_error ("unexpected token at end of stream %s " , state );
1134
1146
}
1135
1147
}
1136
1148
0 commit comments