mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Raise ArgumentError if sprintf format string ends with %
* Add tests and specs. See ruby/spec#401. Patch by Yuta Iwama and Shintaro Morikawa. [ruby-core:80153] [Bug #13315] [Fix GH-1560] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e779eee98b
commit
fd8df3ab3d
3 changed files with 26 additions and 4 deletions
|
@ -14,10 +14,19 @@ describe "String#%" do
|
||||||
("%d%% %s" % [10, "of chickens!"]).should == "10% of chickens!"
|
("%d%% %s" % [10, "of chickens!"]).should == "10% of chickens!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ruby_version_is ""..."2.5" do
|
||||||
it "formats single % character at the end as literal %" do
|
it "formats single % character at the end as literal %" do
|
||||||
("%" % []).should == "%"
|
("%" % []).should == "%"
|
||||||
("foo%" % []).should == "foo%"
|
("foo%" % []).should == "foo%"
|
||||||
end
|
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
|
it "formats single % character before a newline as literal %" do
|
||||||
("%\n" % []).should == "%\n"
|
("%\n" % []).should == "%\n"
|
||||||
|
|
|
@ -520,7 +520,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
||||||
VALUE sym = Qnil;
|
VALUE sym = Qnil;
|
||||||
|
|
||||||
for (t = p; t < end && *t != '%'; t++) ;
|
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);
|
PUSH(p, t - p);
|
||||||
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
|
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
|
||||||
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
|
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
|
||||||
|
|
|
@ -424,6 +424,16 @@ class TestSprintf < Test::Unit::TestCase
|
||||||
assert_equal("%" * BSIZ, sprintf("%%" * BSIZ))
|
assert_equal("%" * BSIZ, sprintf("%%" * BSIZ))
|
||||||
end
|
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
|
def test_rb_sprintf
|
||||||
assert_match(/^#<TestSprintf::T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:0x[0-9a-f]+>$/,
|
assert_match(/^#<TestSprintf::T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:0x[0-9a-f]+>$/,
|
||||||
T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.new.inspect)
|
T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.new.inspect)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue