summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Evans <[email protected]>2023-12-12 16:58:20 -0800
committerJeremy Evans <[email protected]>2023-12-13 07:05:21 -0800
commit0d53dba7ce900387397610c060cfa24758fc806a (patch)
tree194209543de6a4a1212a21a9db225f96bab20fe9
parentc42e4a38e9839fe3380566727304f3fd75a6506a (diff)
Make String#chomp! raise ArgumentError for 2+ arguments if string is empty
String#chomp! returned nil without checking the number of passed arguments in this case.
-rw-r--r--string.c2
-rw-r--r--test/ruby/test_string.rb2
2 files changed, 3 insertions, 1 deletions
diff --git a/string.c b/string.c
index 9da5708964..cfad52d2e3 100644
--- a/string.c
+++ b/string.c
@@ -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);
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 42553cba82..1c7e085630 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -587,6 +587,8 @@ CODE
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