summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spec/rubyspec/core/string/modulo_spec.rb15
-rw-r--r--sprintf.c5
-rw-r--r--test/ruby/test_sprintf.rb10
3 files changed, 26 insertions, 4 deletions
diff --git a/spec/rubyspec/core/string/modulo_spec.rb b/spec/rubyspec/core/string/modulo_spec.rb
index 249b15bf1e..4f26ac5033 100644
--- a/spec/rubyspec/core/string/modulo_spec.rb
+++ b/spec/rubyspec/core/string/modulo_spec.rb
@@ -14,9 +14,18 @@ describe "String#%" do
("%d%% %s" % [10, "of chickens!"]).should == "10% of chickens!"
end
- it "formats single % character at the end as literal %" do
- ("%" % []).should == "%"
- ("foo%" % []).should == "foo%"
+ ruby_version_is ""..."2.5" do
+ it "formats single % character at the end as literal %" do
+ ("%" % []).should == "%"
+ ("foo%" % []).should == "foo%"
+ end
+ end
+
+ ruby_version_is "2.5" do
+ it "raises an error if single % appears at the end" do
+ lambda { ("%" % []) }.should raise_error(ArgumentError)
+ lambda { ("foo%" % [])}.should raise_error(ArgumentError)
+ end
end
it "formats single % character before a newline as literal %" do
diff --git a/sprintf.c b/sprintf.c
index 89541344a9..8488d58cbd 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -520,7 +520,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
VALUE sym = Qnil;
for (t = p; t < end && *t != '%'; t++) ;
- if (t + 1 == end) ++t;
+ if (t + 1 == end) {
+ if (*t == '%') rb_raise(rb_eArgError, "incomplete format specifier");
+ ++t;
+ }
PUSH(p, t - p);
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb
index 1bf65f1eab..f58686d93d 100644
--- a/test/ruby/test_sprintf.rb
+++ b/test/ruby/test_sprintf.rb
@@ -424,6 +424,16 @@ class TestSprintf < Test::Unit::TestCase
assert_equal("%" * BSIZ, sprintf("%%" * BSIZ))
end
+ def test_percent_sign_at_end
+ assert_raise_with_message(ArgumentError, "incomplete format specifier") do
+ sprintf("%")
+ end
+
+ assert_raise_with_message(ArgumentError, "incomplete format specifier") do
+ sprintf("abc%")
+ end
+ end
+
def test_rb_sprintf
assert_match(/^#<TestSprintf::T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:0x[0-9a-f]+>$/,
T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.new.inspect)