mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
stringio.c: check character code
* ext/stringio/stringio.c (strio_ungetc): check if the character code is valid in the encoding. reported by Ahmad Sherif (ahmadsherif) at https://hackerone.com/reports/209593. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d3cd2e618c
commit
853ab8662f
2 changed files with 6 additions and 2 deletions
|
@ -767,12 +767,14 @@ strio_ungetc(VALUE self, VALUE c)
|
||||||
check_modifiable(ptr);
|
check_modifiable(ptr);
|
||||||
if (NIL_P(c)) return Qnil;
|
if (NIL_P(c)) return Qnil;
|
||||||
if (FIXNUM_P(c)) {
|
if (FIXNUM_P(c)) {
|
||||||
int cc = FIX2INT(c);
|
int len, cc = FIX2INT(c);
|
||||||
char buf[16];
|
char buf[16];
|
||||||
|
|
||||||
enc = rb_enc_get(ptr->string);
|
enc = rb_enc_get(ptr->string);
|
||||||
|
len = rb_enc_codelen(cc, enc);
|
||||||
|
if (len <= 0) rb_enc_uint_chr(cc, enc);
|
||||||
rb_enc_mbcput(cc, buf, enc);
|
rb_enc_mbcput(cc, buf, enc);
|
||||||
return strio_unget_bytes(ptr, buf, rb_enc_codelen(cc, enc));
|
return strio_unget_bytes(ptr, buf, len);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
SafeStringValue(c);
|
SafeStringValue(c);
|
||||||
|
|
|
@ -453,6 +453,8 @@ class TestStringIO < Test::Unit::TestCase
|
||||||
f.ungetc("y".ord)
|
f.ungetc("y".ord)
|
||||||
assert_equal("y", f.getc)
|
assert_equal("y", f.getc)
|
||||||
assert_equal("2", f.getc)
|
assert_equal("2", f.getc)
|
||||||
|
|
||||||
|
assert_raise(RangeError) {f.ungetc(0x1ffffff)}
|
||||||
ensure
|
ensure
|
||||||
f.close unless f.closed?
|
f.close unless f.closed?
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue