Skip to content

Commit

Permalink
[Bug #19108] Check for the encoding of pack/unpack format
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 1, 2022
1 parent a1d341e commit 9869bd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pack_pack(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer)
int integer_size, bigendian_p;

StringValue(fmt);
rb_must_asciicompat(fmt);
p = RSTRING_PTR(fmt);
pend = p + RSTRING_LEN(fmt);

Expand Down Expand Up @@ -959,6 +960,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)

StringValue(str);
StringValue(fmt);
rb_must_asciicompat(fmt);

if (offset < 0) rb_raise(rb_eArgError, "offset can't be negative");
len = RSTRING_LEN(str);
Expand Down
14 changes: 14 additions & 0 deletions test/ruby/test_pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ def test_pack
assert_equal(x, x.pack("l").unpack("l"))
end

def test_ascii_incompatible
assert_raise(Encoding::CompatibilityError) do
["foo"].pack("u".encode("UTF-32BE"))
end

assert_raise(Encoding::CompatibilityError) do
"foo".unpack("C".encode("UTF-32BE"))
end

assert_raise(Encoding::CompatibilityError) do
"foo".unpack1("C".encode("UTF-32BE"))
end
end

def test_pack_n
assert_equal "\000\000", [0].pack('n')
assert_equal "\000\001", [1].pack('n')
Expand Down

0 comments on commit 9869bd1

Please sign in to comment.