@@ -130,11 +130,13 @@ static VALUE encoding_spec_rb_enc_mbc_to_codepoint(VALUE self, VALUE str, VALUE
130
130
static VALUE encoding_spec_rb_enc_mbcput (VALUE self , VALUE code , VALUE encoding ) {
131
131
unsigned int c = FIX2UINT (code );
132
132
rb_encoding * enc = rb_to_encoding (encoding );
133
- char * buf = (char * ) malloc (10 );
133
+ char buf [ONIGENC_CODE_TO_MBC_MAXLEN ];
134
+ memset (buf , '\1' , sizeof (buf ));
134
135
int len = rb_enc_mbcput (c , buf , enc );
135
- VALUE str = rb_enc_str_new (buf , len , enc );
136
- free (buf );
137
- return str ;
136
+ if (buf [len ] != '\1' ) {
137
+ rb_raise (rb_eRuntimeError , "should not change bytes after len" );
138
+ }
139
+ return rb_enc_str_new (buf , len , enc );
138
140
}
139
141
140
142
static VALUE encoding_spec_rb_enc_from_encoding (VALUE self , VALUE name ) {
@@ -281,8 +283,12 @@ static VALUE encoding_spec_ONIGENC_MBC_CASE_FOLD(VALUE self, VALUE str) {
281
283
char * beg_initial = beg ;
282
284
char * end = beg + 2 ;
283
285
OnigUChar fold [ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM ];
286
+ memset (fold , '\1' , sizeof (fold ));
284
287
rb_encoding * enc = rb_enc_get (str );
285
288
int r = ONIGENC_MBC_CASE_FOLD (enc , ONIGENC_CASE_FOLD , & beg , (const OnigUChar * )end , fold );
289
+ if (r > 0 && fold [r ] != '\1' ) {
290
+ rb_raise (rb_eRuntimeError , "should not change bytes after len" );
291
+ }
286
292
VALUE str_result = r <= 0 ? Qnil : rb_enc_str_new ((char * )fold , r , enc );
287
293
long bytes_used = beg - beg_initial ;
288
294
return rb_ary_new3 (2 , str_result , INT2FIX (bytes_used ));
0 commit comments