Index: test/testPackUnpack.rb =================================================================== --- test/testPackUnpack.rb (revision 4841) +++ test/testPackUnpack.rb (working copy) @@ -52,3 +52,6 @@ test_exception(TypeError){ [-1, 0, 1, 128].pack("m") } test_equal(["testingT"], "testing=54".unpack("M*")) test_equal(["12345"], "12345".unpack("M*")) + +test_exception(ArgumentError){ "\205".unpack("U") } +test_equal([228, 228], "\303\244\303\244\303\244".unpack("U2")) Index: src/org/jruby/util/Pack.java =================================================================== --- src/org/jruby/util/Pack.java (revision 4841) +++ src/org/jruby/util/Pack.java (working copy) @@ -905,7 +905,8 @@ occurrences = encode.remaining(); } //get the correct substring - byte[] toUnpack = new byte[occurrences]; + final int nBytes = Math.min(occurrences * 4, encode.remaining()); + byte[] toUnpack = new byte[nBytes]; encode.get(toUnpack); CharBuffer lUtf8 = null; try {