ruby-changes:72982
From: Nobuyoshi <ko1@a...>
Date: Sat, 20 Aug 2022 03:57:36 +0900 (JST)
Subject: [ruby-changes:72982] ce384ef5a9 (master): [Bug #18955] Check length of argument for `%c` in proper encoding
https://git.ruby-lang.org/ruby.git/commit/?id=ce384ef5a9 From ce384ef5a95b809f248e089c1608e60753dabe45 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sat, 20 Aug 2022 00:16:43 +0900 Subject: [Bug #18955] Check length of argument for `%c` in proper encoding --- sprintf.c | 5 +++-- test/ruby/test_m17n.rb | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/sprintf.c b/sprintf.c index b16ab3f581..5f7227e619 100644 --- a/sprintf.c +++ b/sprintf.c @@ -441,10 +441,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L441 tmp = rb_check_string_type(val); if (!NIL_P(tmp)) { - if (rb_enc_strlen(RSTRING_PTR(tmp),RSTRING_END(tmp),enc) != 1) { + rb_encoding *valenc = rb_enc_get(tmp); + if (rb_enc_strlen(RSTRING_PTR(tmp), RSTRING_END(tmp), valenc) != 1) { rb_raise(rb_eArgError, "%%c requires a character"); } - c = rb_enc_codepoint_len(RSTRING_PTR(tmp), RSTRING_END(tmp), &n, enc); + c = rb_enc_codepoint_len(RSTRING_PTR(tmp), RSTRING_END(tmp), &n, valenc); RB_GC_GUARD(tmp); } else { diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index c00bf59e18..2c6fcee004 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -892,6 +892,8 @@ class TestM17N < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_m17n.rb#L892 assert_raise(Encoding::CompatibilityError) { "%s%s" % [s("\xc2\xa1"), e("\xc2\xa1")] } + + "%c" % "\u3042".encode('Windows-31J') end def test_sprintf_p -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/