Skip to content

Commit

Permalink
Raise an ArgumentError for unknown pack/unpack directive
Browse files Browse the repository at this point in the history
* [Bug #19150]
  • Loading branch information
eregon committed Jan 5, 2023
1 parent 358f325 commit 03dd377
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ Note that each entry is kept to a minimum, see links for details.

Note: We're only listing outstanding class updates.

* Array

* `Array#pack` now raises ArgumentError for unknown directives. [[Bug #19150]]

* String

* `String#unpack` now raises ArgumentError for unknown directives. [[Bug #19150]]

## Stdlib updates

* The following default gems are updated.
Expand Down Expand Up @@ -41,3 +49,5 @@ See GitHub releases like [GitHub Releases of Logger](https://github.com/ruby/log
## Implementation improvements

## JIT

[Bug #19150]: https://bugs.ruby-lang.org/issues/19150
2 changes: 1 addition & 1 deletion pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ unknown_directive(const char *mode, char type, VALUE fmt)
snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
}
fmt = rb_str_quote_unprintable(fmt);
rb_warn("unknown %s directive '%s' in '%"PRIsVALUE"'",
rb_raise(rb_eArgError, "unknown %s directive '%s' in '%"PRIsVALUE"'",
mode, unknown, fmt);
}

Expand Down
14 changes: 7 additions & 7 deletions test/ruby/test_pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -777,32 +777,32 @@ def test_short_with_block
end

def test_pack_garbage
assert_warn(%r%unknown pack directive '\*' in '\*U'$%) do
assert_raise(ArgumentError, %r%unknown pack directive '\*' in '\*U'$%) do
assert_equal "\000", [0].pack("*U")
end
end

def test_unpack_garbage
assert_warn(%r%unknown unpack directive '\*' in '\*U'$%) do
assert_raise(ArgumentError, %r%unknown unpack directive '\*' in '\*U'$%) do
assert_equal [0], "\000".unpack("*U")
end
end

def test_invalid_warning
assert_warning(/unknown pack directive ',' in ','/) {
assert_raise(ArgumentError, /unknown pack directive ',' in ','/) {
[].pack(",")
}
assert_warning(/\A[ -~]+\Z/) {
assert_raise(ArgumentError, /\A[ -~]+\Z/) {
[].pack("\x7f")
}
assert_warning(/\A(.* in '\u{3042}'\n)+\z/) {
assert_raise(ArgumentError, /\A(.* in '\u{3042}'\n)+\z/) {
[].pack("\u{3042}")
}

assert_warning(/\A.* in '.*U'\Z/) {
assert_raise(ArgumentError, /\A.* in '.*U'\Z/) {
assert_equal "\000", [0].pack("\0U")
}
assert_warning(/\A.* in '.*U'\Z/) {
assert_raise(ArgumentError, /\A.* in '.*U'\Z/) {
"\000".unpack("\0U")
}
end
Expand Down

0 comments on commit 03dd377

Please sign in to comment.