From 1f53145dd467ebdb5d25a234f69d774ba0086afe Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 6 Nov 2014 14:58:43 +0000 Subject: pack.c: escape and encoding * pack.c (pack_pack): escape unprintable characters and preserve the encoding of warning message. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48307 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ pack.c | 15 ++++++++++++--- test/ruby/test_pack.rb | 8 ++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 19c5db0273..df021606a7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Nov 6 23:58:40 2014 Nobuyoshi Nakada + + * pack.c (pack_pack): escape unprintable characters and preserve + the encoding of warning message. + Thu Nov 6 23:55:18 2014 Nobuyoshi Nakada * string.c (sym_printable): QUOTE() should not raise an exception diff --git a/pack.c b/pack.c index 937cbe28c1..728c1d1560 100644 --- a/pack.c +++ b/pack.c @@ -912,10 +912,19 @@ pack_pack(VALUE ary, VALUE fmt) } break; - default: - rb_warning("unknown pack directive '%c' in '%s'", - type, RSTRING_PTR(fmt)); + default: { + char unknown[5]; + if (ISPRINT(type)) { + unknown[0] = type; + unknown[1] = '\0'; + } + else { + snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff); + } + rb_warning("unknown pack directive '%s' in '% "PRIsVALUE"'", + unknown, fmt); break; + } } } diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index 59edc3eb4b..5d2b656058 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -718,5 +718,13 @@ EXPECTED assert_warning(/unknown pack directive ',' in ','/) { [].pack(",") } + assert_warning(/\A[ -~]+\Z/) { + [].pack("\x7f") + } + assert_warning(/\A(.* in '\u{3042}'\n)+\z/) { + EnvUtil.with_default_external(Encoding::UTF_8) { + [].pack("\u{3042}") + } + } end end -- cgit v1.2.3