Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

sprintf() will not throw exception when passed the format of single %…

… character before a '\n' or '\0'.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_equal("%", sprintf("%", ""))
assert_equal("foo%", sprintf("foo%", ""))
assert_equal("%\n", sprintf("%\n", ""))
assert_equal("%\n.3f", sprintf("%\n.3f", 1.2))
assert_equal("%\x00.3f", sprintf("%\0.3f", 1.2))
assert_raise(ArgumentError){ sprintf("% ", "") }

puts :ok
}}}

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@4928 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
commit 421ab552da16fd5a3e18ca46b052eccc9c69321d 1 parent e3ee11d
@Watson1978 Watson1978 authored
Showing with 17 additions and 2 deletions.
  1. +17 −2 sprintf.c
View
19 sprintf.c
@@ -716,8 +716,23 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
break;
default:
- rb_raise(rb_eArgError, "malformed format string - %%%c",
- format_str[i]);
+ if (format_str[i - 1] == '%' &&
+ (format_str[i] == '\0' || format_str[i] == '\n')) {
+ if (format_str[i] == '\n') {
+ arg = rb_str_new("%\n", 2);
+ }
+ else if (format_len > i) {
+ arg = rb_str_new("%\0", 2);
+ }
+ else {
+ arg = rb_str_new("%", 1);
+ }
+ complete = true;
+ }
+ else {
+ rb_raise(rb_eArgError, "malformed format string - %%%c",
+ format_str[i]);
+ }
}
if (!complete) {
continue;
Please sign in to comment.
Something went wrong with that request. Please try again.