diff --git a/sprintf.c b/sprintf.c index f7ebe82e2c..22323265b3 100644 --- a/sprintf.c +++ b/sprintf.c @@ -250,6 +250,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) --argv; StringValue(fmt); enc = rb_enc_get(fmt); + rb_must_asciicompat(fmt); orig = fmt; fmt = rb_str_tmp_frozen_acquire(fmt); p = RSTRING_PTR(fmt); diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index a50507a528..da04ae7fa7 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -898,6 +898,16 @@ class TestM17N < Test::Unit::TestCase def test_sprintf_p Encoding.list.each do |e| + unless e.ascii_compatible? + format = e.dummy? ? "%p".force_encoding(e) : "%p".encode(e) + assert_raise(Encoding::CompatibilityError) do + sprintf(format, nil) + end + assert_raise(Encoding::CompatibilityError) do + format % nil + end + next + end format = "%p".force_encoding(e) ['', 'a', "\xC2\xA1", "\x00"].each do |s| s.force_encoding(e)