Skip to content

Commit

Permalink
Make String#chomp! raise ArgumentError for 2+ arguments if string is …
Browse files Browse the repository at this point in the history
…empty

String#chomp! returned nil without checking the number of passed
arguments in this case.
  • Loading branch information
jeremyevans committed Dec 13, 2023
1 parent c42e4a3 commit 0d53dba
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion string.c
Expand Up @@ -9689,7 +9689,7 @@ rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
{
VALUE rs;
str_modifiable(str);
if (RSTRING_LEN(str) == 0) return Qnil;
if (RSTRING_LEN(str) == 0 && argc < 2) return Qnil;
rs = chomp_rs(argc, argv);
if (NIL_P(rs)) return Qnil;
return rb_str_chomp_string(str, rs);
Expand Down
2 changes: 2 additions & 0 deletions test/ruby/test_string.rb
Expand Up @@ -587,6 +587,8 @@ def o.to_str
assert_equal("foo", s.chomp!("\n"))
s = "foo\r"
assert_equal("foo", s.chomp!("\n"))

assert_raise(ArgumentError) {String.new.chomp!("", "")}
ensure
$/ = save
$VERBOSE = verbose
Expand Down

0 comments on commit 0d53dba

Please sign in to comment.