Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise ArgumentError on unknown directive for Array pack / String unpack #3727

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Compatibility:
* Add `IO#{pread, pwrite}` methods (#3718, @andrykonchin).
* Add `rb_io_closed_p()` (#3681, @andrykonchin).
* Add `rb_io_open_descriptor()` (#3681, @andrykonchin).
* `Array#pack` now raises `ArgumentError` for unknown directives (#3681, @Th3-M4jor).
* `String#unpack` now raises `ArgumentError` for unknown directives (#3681, @Th3-M4jor).

Performance:

Expand Down
6 changes: 3 additions & 3 deletions spec/ruby/core/array/pack/shared/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
# NOTE: Added this case just to not forget about the decision in the ticket
it "raise ArgumentError when a directive is unknown" do
# additional directive ('a') is required for the X directive
-> { [@obj, @obj].pack("a R" + pack_format) }.should raise_error(ArgumentError)
-> { [@obj, @obj].pack("a 0" + pack_format) }.should raise_error(ArgumentError)
-> { [@obj, @obj].pack("a :" + pack_format) }.should raise_error(ArgumentError)
-> { [@obj, @obj].pack("a R" + pack_format) }.should raise_error(ArgumentError, /unknown pack directive 'R'/)
-> { [@obj, @obj].pack("a 0" + pack_format) }.should raise_error(ArgumentError, /unknown pack directive '0'/)
-> { [@obj, @obj].pack("a :" + pack_format) }.should raise_error(ArgumentError, /unknown pack directive ':'/)
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/ruby/core/string/unpack/shared/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
d.should_receive(:to_str).and_return("a"+unpack_format)
"abc".unpack(d).should be_an_instance_of(Array)
end

ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19150
it 'raise ArgumentError when a directive is unknown' do
-> { "abcdefgh".unpack("a R" + unpack_format) }.should raise_error(ArgumentError, /unknown unpack directive 'R'/)
-> { "abcdefgh".unpack("a 0" + unpack_format) }.should raise_error(ArgumentError, /unknown unpack directive '0'/)
-> { "abcdefgh".unpack("a :" + unpack_format) }.should raise_error(ArgumentError, /unknown unpack directive ':'/)
end
end
end

describe :string_unpack_no_platform, shared: true do
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/a_tags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
fails:Array#pack with format 'A' warns that a directive is unknown
fails:Array#pack with format 'a' warns that a directive is unknown
fails:Array#pack with format 'A' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'a' raise ArgumentError when a directive is unknown
1 change: 0 additions & 1 deletion spec/tags/core/array/pack/at_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fails:Array#pack with format '@' warns that a directive is unknown
fails:Array#pack with format '@' raise ArgumentError when a directive is unknown
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/b_tags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
fails:Array#pack with format 'B' warns that a directive is unknown
fails:Array#pack with format 'b' warns that a directive is unknown
fails:Array#pack with format 'B' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'b' raise ArgumentError when a directive is unknown
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/c_tags.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
fails:Array#pack with format 'C' warns that a directive is unknown
fails:Array#pack with format 'c' warns that a directive is unknown
fails:Array#pack with format 'C' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'C' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'c' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'c' raise ArgumentError for NULL bytes between directives
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/h_tags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
fails:Array#pack with format 'H' warns that a directive is unknown
fails:Array#pack with format 'h' warns that a directive is unknown
fails:Array#pack with format 'H' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'h' raise ArgumentError when a directive is unknown
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/i_tags.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
fails:Array#pack with format 'I' warns that a directive is unknown
fails:Array#pack with format 'i' warns that a directive is unknown
fails:Array#pack with format 'I' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'i' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'I' with modifier '<' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'I' with modifier '<' and '_' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'I' with modifier '<' and '!' raise ArgumentError for NULL bytes between directives
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/j_tags.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
fails:Array#pack with format 'J' warns that a directive is unknown
fails:Array#pack with format 'j' warns that a directive is unknown
fails:Array#pack with format 'J' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'j' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'J' with modifier '_' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'J' with modifier '!' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'j' with modifier '_' raise ArgumentError for NULL bytes between directives
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/l_tags.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
fails:Array#pack with format 'L' warns that a directive is unknown
fails:Array#pack with format 'l' warns that a directive is unknown
fails:Array#pack with format 'L' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'l' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'L' with modifier '<' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'L' with modifier '>' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'L' with modifier '<' and '_' raise ArgumentError for NULL bytes between directives
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/m_tags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
fails:Array#pack with format 'M' warns that a directive is unknown
fails:Array#pack with format 'm' warns that a directive is unknown
fails:Array#pack with format 'M' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'm' raise ArgumentError when a directive is unknown
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/n_tags.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
fails:Array#pack with format 'N' warns that a directive is unknown
fails:Array#pack with format 'n' warns that a directive is unknown
fails:Array#pack with format 'N' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'N' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'n' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'n' raise ArgumentError for NULL bytes between directives
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/p_tags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
fails:Array#pack with format 'P' warns that a directive is unknown
fails:Array#pack with format 'p' warns that a directive is unknown
fails:Array#pack with format 'P' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'p' raise ArgumentError when a directive is unknown
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/q_tags.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
fails:Array#pack with format 'Q' warns that a directive is unknown
fails:Array#pack with format 'q' warns that a directive is unknown
fails:Array#pack with format 'Q' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'q' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'Q' with modifier '<' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'Q' with modifier '>' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'q' with modifier '<' raise ArgumentError for NULL bytes between directives
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/s_tags.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
fails:Array#pack with format 'S' warns that a directive is unknown
fails:Array#pack with format 's' warns that a directive is unknown
fails:Array#pack with format 'S' raise ArgumentError when a directive is unknown
fails:Array#pack with format 's' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'S' with modifier '<' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'S' with modifier '<' and '_' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'S' with modifier '<' and '!' raise ArgumentError for NULL bytes between directives
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/u_tags.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
fails:Array#pack with format 'U' warns that a directive is unknown
fails:Array#pack with format 'u' warns that a directive is unknown
fails:Array#pack with format 'U' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'U' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'u' raise ArgumentError when a directive is unknown
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/v_tags.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
fails:Array#pack with format 'V' warns that a directive is unknown
fails:Array#pack with format 'v' warns that a directive is unknown
fails:Array#pack with format 'V' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'V' raise ArgumentError for NULL bytes between directives
fails:Array#pack with format 'v' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'v' raise ArgumentError for NULL bytes between directives
1 change: 0 additions & 1 deletion spec/tags/core/array/pack/w_tags.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
fails:Array#pack with format 'w' warns that a directive is unknown
fails:Array#pack with format 'w' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'w' raise ArgumentError for NULL bytes between directives
2 changes: 0 additions & 2 deletions spec/tags/core/array/pack/x_tags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
fails:Array#pack with format 'x' warns that a directive is unknown
fails:Array#pack with format 'X' warns that a directive is unknown
fails:Array#pack with format 'x' raise ArgumentError when a directive is unknown
fails:Array#pack with format 'X' raise ArgumentError when a directive is unknown
1 change: 0 additions & 1 deletion spec/tags/core/array/pack/z_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fails:Array#pack with format 'Z' warns that a directive is unknown
fails:Array#pack with format 'Z' raise ArgumentError when a directive is unknown
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ public interface SimplePackListener {

void warn(String message);

String packListenerMode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,7 @@ public void parse() throws DeferredRaiseException {

default:
n++;
listener.warn(String.format("unknown pack directive '%c'", (char) b));
break;
listener.error(String.format("unknown %s directive '%c'", listener.packListenerMode(), (char) b));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ public void warn(String message) {
}
}

@Override
public String packListenerMode() {
return "pack";
}

public FormatNode getNode() {
return sequenceStack.peek().get(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ public void warn(String message) {
throw CompilerDirectives.shouldNotReachHere();
}

@Override
public String packListenerMode() {
return "unpack";
}

public FormatNode getNode() {
return sequenceStack.peek().get(0);
}
Expand Down
Loading