Skip to content

Commit

Permalink
Fix segfault when Fixnum#chr doesn't return a string
Browse files Browse the repository at this point in the history
  • Loading branch information
bouk committed Nov 23, 2016
1 parent 669bbc7 commit 9bf1c0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions mrbgems/mruby-sprintf/src/sprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
else {
mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid character");
}
mrb_check_type(mrb, tmp, MRB_TT_STRING);
c = RSTRING_PTR(tmp);
n = RSTRING_LEN(tmp);
if (!(flags & FWIDTH)) {
Expand Down
23 changes: 23 additions & 0 deletions mrbgems/mruby-sprintf/test/sprintf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,26 @@
assert_equal "123 < 456", "%{num} < %<str>s" % { num: 123, str: "456" }
assert_equal 15, ("%b" % (1<<14)).size
end

assert("String#% with invalid chr") do
begin
class Fixnum
alias_method :chr_, :chr if method_defined?(:chr)

def chr
nil
end
end

assert_raise TypeError do
"%c" % 0
end
ensure
class Fixnum
if method_defined?(:chr_)
alias_method :chr, :chr_
remove_method :chr_
end
end
end
end

0 comments on commit 9bf1c0e

Please sign in to comment.