Skip to content

Commit 854bc8e

Browse files
committed
Improve rb_enc_mbcput() and ONIGENC_MBC_CASE_FOLD() specs
1 parent c708cc3 commit 854bc8e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

spec/ruby/optional/capi/encoding_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@
145145
@s.rb_enc_mbcput(0xA2, Encoding::UTF_8).should == "¢"
146146
@s.rb_enc_mbcput(0x20AC, Encoding::UTF_8).should == "€"
147147
@s.rb_enc_mbcput(0x24B62, Encoding::UTF_8).should == "𤭢"
148+
149+
@s.rb_enc_mbcput(0x24, Encoding::UTF_16BE).bytes.should == [0, 0x24]
150+
@s.rb_enc_mbcput(0x24B62, Encoding::UTF_16LE).bytes.should == [82, 216, 98, 223]
148151
end
149152
end
150153

@@ -639,6 +642,10 @@
639642
@s.ONIGENC_MBC_CASE_FOLD("lower".force_encoding("binary")).should == ["l", 1]
640643
@s.ONIGENC_MBC_CASE_FOLD("Upper".force_encoding("binary")).should == ["u", 1]
641644
@s.ONIGENC_MBC_CASE_FOLD("É").should == ["é", 2]
645+
646+
str, length = @s.ONIGENC_MBC_CASE_FOLD('$'.encode(Encoding::UTF_16BE))
647+
length.should == 2
648+
str.bytes.should == [0, 0x24]
642649
end
643650
end
644651
end

spec/ruby/optional/capi/ext/encoding_spec.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ static VALUE encoding_spec_rb_enc_mbc_to_codepoint(VALUE self, VALUE str, VALUE
130130
static VALUE encoding_spec_rb_enc_mbcput(VALUE self, VALUE code, VALUE encoding) {
131131
unsigned int c = FIX2UINT(code);
132132
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));
134135
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);
138140
}
139141

140142
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) {
281283
char *beg_initial = beg;
282284
char *end = beg + 2;
283285
OnigUChar fold[ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM];
286+
memset(fold, '\1', sizeof(fold));
284287
rb_encoding *enc = rb_enc_get(str);
285288
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+
}
286292
VALUE str_result = r <= 0 ? Qnil : rb_enc_str_new((char *)fold, r, enc);
287293
long bytes_used = beg - beg_initial;
288294
return rb_ary_new3(2, str_result, INT2FIX(bytes_used));

0 commit comments

Comments
 (0)