@@ -7987,7 +7987,14 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
7987
7987
while (s < send ) {
7988
7988
int may_modify = 0 ;
7989
7989
7990
- c0 = c = rb_enc_codepoint_len ((char * )s , (char * )send , & clen , e1 );
7990
+ int r = rb_enc_precise_mbclen ((char * )s , (char * )send , e1 );
7991
+ if (!MBCLEN_CHARFOUND_P (r )) {
7992
+ xfree (buf );
7993
+ rb_raise (rb_eArgError , "invalid byte sequence in %s" , rb_enc_name (e1 ));
7994
+ }
7995
+ clen = MBCLEN_CHARFOUND_LEN (r );
7996
+ c0 = c = rb_enc_mbc_to_codepoint ((char * )s , (char * )send , e1 );
7997
+
7991
7998
tlen = enc == e1 ? clen : rb_enc_codelen (c , enc );
7992
7999
7993
8000
s += clen ;
@@ -8067,7 +8074,15 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
8067
8074
8068
8075
while (s < send ) {
8069
8076
int may_modify = 0 ;
8070
- c0 = c = rb_enc_codepoint_len ((char * )s , (char * )send , & clen , e1 );
8077
+
8078
+ int r = rb_enc_precise_mbclen ((char * )s , (char * )send , e1 );
8079
+ if (!MBCLEN_CHARFOUND_P (r )) {
8080
+ xfree (buf );
8081
+ rb_raise (rb_eArgError , "invalid byte sequence in %s" , rb_enc_name (e1 ));
8082
+ }
8083
+ clen = MBCLEN_CHARFOUND_LEN (r );
8084
+ c0 = c = rb_enc_mbc_to_codepoint ((char * )s , (char * )send , e1 );
8085
+
8071
8086
tlen = enc == e1 ? clen : rb_enc_codelen (c , enc );
8072
8087
8073
8088
if (c < 256 ) {
0 commit comments