8000 stringio.c: check character code · Fryguy/ruby@853ab86 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 853ab86

Browse files
committed
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
1 parent d3cd2e6 commit 853ab86

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

ext/stringio/stringio.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,14 @@ strio_ungetc(VALUE self, VALUE c)
767767
check_modifiable(ptr);
768768
if (NIL_P(c)) return Qnil;
769769
if (FIXNUM_P(c)) {
770-
int cc = FIX2INT(c);
770+
int len, cc = FIX2INT(c);
771771
char buf[16];
772772

773773
enc = rb_enc_get(ptr->string);
774+
len = rb_enc_codelen(cc, enc);
775+
if (len <= 0) rb_enc_uint_chr(cc, enc);
774776
rb_enc_mbcput(cc, buf, enc);
775-
return strio_unget_bytes(ptr, buf, rb_enc_codelen(cc, enc));
777+
return strio_unget_bytes(ptr, buf, len);
776778
}
777779
else {
778780
SafeStringValue(c);

test/stringio/test_stringio.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ def test_ungetc
453453
f.ungetc("y".ord)
454454
assert_equal("y", f.getc)
455455
assert_equal("2", f.getc)
456+
457+
assert_raise(RangeError) {f.ungetc(0x1ffffff)}
456458
ensure
457459
f.close unless f.closed?
458460
end

0 commit comments

Comments
 (0)
0