Skip to content

Commit

Permalink
sprintf() will not throw exception when passed the format of single %…
Browse files Browse the repository at this point in the history
… 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
Watson1978 committed Nov 22, 2010
1 parent e3ee11d commit 421ab55
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions sprintf.c
Expand Up @@ -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;
Expand Down

0 comments on commit 421ab55

Please sign in to comment.