From 6ba4bd5858753c1aff9d11cfa7c478b9616c109a Mon Sep 17 00:00:00 2001 From: drbrain Date: Tue, 10 Apr 2012 22:33:58 +0000 Subject: [PATCH] * pack.c (pack_pack): Warn when an invalid character is found in the format string when $VERBOSE is true. [ruby-trunk - Feature #5219] * pack.c (pack_unpack): ditto * test/ruby/test_pack.rb (class TestPack): Test for warnings on invalid format characters. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35292 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ pack.c | 4 ++++ test/ruby/test_pack.rb | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/ChangeLog b/ChangeLog index 6ad9c40bd8..73c86bef12 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Apr 11 07:33:13 2012 Eric Hodel + + * pack.c (pack_pack): Warn when an invalid character is found in the + format string when $VERBOSE is true. [ruby-trunk - Feature #5219] + * pack.c (pack_unpack): ditto + * test/ruby/test_pack.rb (class TestPack): Test for warnings on + invalid format characters. + Wed Apr 11 06:11:10 2012 Eric Hodel * string.c (rb_str_tr): Documented use of \ to escape characters. diff --git a/pack.c b/pack.c index 78789889de..4a2d246174 100644 --- a/pack.c +++ b/pack.c @@ -1026,6 +1026,8 @@ pack_pack(VALUE ary, VALUE fmt) break; default: + rb_warning("unknown pack directive '%c' in '%s'", + type, RSTRING_PTR(fmt)); break; } } @@ -2143,6 +2145,8 @@ pack_unpack(VALUE str, VALUE fmt) break; default: + rb_warning("unknown unpack directive '%c' in '%s'", + type, RSTRING_PTR(fmt)); break; } } diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index c862215cb4..e623a69cbf 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -651,4 +651,39 @@ class TestPack < Test::Unit::TestCase assert_nil("".unpack("i") {|x| result = x}, bug4059) assert_equal(:ok, result) end + + def test_pack_garbage + assert_silent do + assert_equal "\000", [0].pack("*U") + end + + verbose = $VERBOSE + $VERBOSE = true + + _, err = capture_io do + assert_equal "\000", [0].pack("*U") + end + + assert_match %r%unknown pack directive '\*' in '\*U'$%, err + ensure + $VERBOSE = verbose + end + + def test_unpack_garbage + assert_silent do + assert_equal [0], "\000".unpack("*U") + end + + verbose = $VERBOSE + $VERBOSE = true + + _, err = capture_io do + assert_equal [0], "\000".unpack("*U") + end + + assert_match %r%unknown unpack directive '\*' in '\*U'$%, err + ensure + $VERBOSE = verbose + end + end