[前][次][番号順一覧][スレッド一覧]

ruby-changes:46774

From: eregon <ko1@a...>
Date: Fri, 26 May 2017 00:33:31 +0900 (JST)
Subject: [ruby-changes:46774] eregon:r58890 (trunk): Raise ArgumentError if sprintf format string ends with %

eregon	2017-05-26 00:33:28 +0900 (Fri, 26 May 2017)

  New Revision: 58890

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58890

  Log:
    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]

  Modified files:
    trunk/spec/rubyspec/core/string/modulo_spec.rb
    trunk/sprintf.c
    trunk/test/ruby/test_sprintf.rb
Index: test/ruby/test_sprintf.rb
===================================================================
--- test/ruby/test_sprintf.rb	(revision 58889)
+++ test/ruby/test_sprintf.rb	(revision 58890)
@@ -424,6 +424,16 @@ class TestSprintf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_sprintf.rb#L424
     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)
Index: spec/rubyspec/core/string/modulo_spec.rb
===================================================================
--- spec/rubyspec/core/string/modulo_spec.rb	(revision 58889)
+++ spec/rubyspec/core/string/modulo_spec.rb	(revision 58890)
@@ -14,9 +14,18 @@ describe "String#%" do https://github.com/ruby/ruby/blob/trunk/spec/rubyspec/core/string/modulo_spec.rb#L14
     ("%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
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 58889)
+++ sprintf.c	(revision 58890)
@@ -520,7 +520,10 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L520
 	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);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]