Skip to content

Commit

Permalink
Fix coderange of invalid_encoding_string.<<(ord)
Browse files Browse the repository at this point in the history
Appending valid encoding character can change coderange from invalid to valid.
Example: "\x95".force_encoding('sjis')<<0x5C will be a valid string "\x{955C}"
  • Loading branch information
tompng committed Jan 16, 2024
1 parent 9f02680 commit d5992f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion string.c
Original file line number Diff line number Diff line change
Expand Up @@ -3522,8 +3522,12 @@ rb_str_concat(VALUE str1, VALUE str2)
}
rb_str_resize(str1, pos+len);
memcpy(RSTRING_PTR(str1) + pos, buf, len);
if (cr == ENC_CODERANGE_7BIT && code > 127)
if (cr == ENC_CODERANGE_7BIT && code > 127) {
cr = ENC_CODERANGE_VALID;
}
else if (cr == ENC_CODERANGE_BROKEN) {
cr = ENC_CODERANGE_UNKNOWN;
}
ENC_CODERANGE_SET(str1, cr);
}
return str1;
Expand Down
3 changes: 3 additions & 0 deletions test/ruby/test_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ def test_LSHIFT # '<<'
assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -1}
assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << 0x81308130}
assert_nothing_raised {S("a".force_encoding(Encoding::GB18030)) << 0x81308130}

s = "\x95".force_encoding(Encoding::SJIS).tap(&:valid_encoding?)
assert_predicate(s << 0x5c, :valid_encoding?)
end

def test_MATCH # '=~'
Expand Down

0 comments on commit d5992f9

Please sign in to comment.