Skip to content

Commit e10f084

Browse files
committed
[GR-18163] Fixed String#{chomp, chomp!} issue with invalid encoded strings
PullRequest: truffleruby/2122
2 parents fa7e760 + 026e983 commit e10f084

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Bug fixes:
88
* Fix error message when the method name is not a Symbol or String for `Kernel#respond_to?` (#2132, @ssnickolay)
99
* Fixed setting of special variables in enumerators and enumerables (#1484).
1010
* Fix status and output when SystemExit is subclassed and raised (#2128)
11+
* Fix `String#{chomp, chomp!}` issue with invalid encoded strings (#2133).
1112

1213
Compatibility:
1314

spec/ruby/core/string/chomp_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
$/ = "cdef"
5656
"abcdef".chomp.should == "ab"
5757
end
58+
59+
it "removes one trailing newline for string with invalid encoding" do
60+
"\xa0\xa1\n".chomp.should == "\xa0\xa1"
61+
end
5862
end
5963

6064
describe "when passed nil" do
@@ -108,6 +112,10 @@
108112
it "returns an empty String when self is empty" do
109113
"".chomp("").should == ""
110114
end
115+
116+
it "removes one trailing newline for string with invalid encoding" do
117+
"\xa0\xa1\n".chomp("").should == "\xa0\xa1"
118+
end
111119
end
112120

113121
describe "when passed '\\n'" do

src/main/ruby/truffleruby/core/string.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def chomp!(sep=undefined)
826826
if j = Primitive.string_previous_byte_index(self, bytes)
827827
chr = Primitive.string_chr_at(self, j)
828828

829-
if chr.ord == 13
829+
if !Primitive.nil?(chr) && chr.ord == 13
830830
bytes = j
831831
end
832832
end
@@ -839,13 +839,13 @@ def chomp!(sep=undefined)
839839

840840
while i = Primitive.string_previous_byte_index(self, bytes)
841841
chr = Primitive.string_chr_at(self, i)
842-
break unless chr.ord == 10
842+
break unless !Primitive.nil?(chr) && chr.ord == 10
843843

844844
bytes = i
845845

846846
if j = Primitive.string_previous_byte_index(self, i)
847847
chr = Primitive.string_chr_at(self, j)
848-
if chr.ord == 13
848+
if !Primitive.nil?(chr) && chr.ord == 13
849849
bytes = j
850850
end
851851
end

0 commit comments

Comments
 (0)