Skip to content

Commit b1dbba7

Browse files
committed
[GR-17457] Fix for unpack Z null terminator consumption.
PullRequest: truffleruby/3351
2 parents 6df3fd3 + aaa158e commit b1dbba7

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Bug fixes:
1313
* Fix `String#[Regexp, Integer]` when the capture group exists but is not matched (@eregon).
1414
* Fix `File.open` mode string parsing when binary option is the third character (@bjfish).
1515
* Fix `rb_scan_args_kw` macro to avoid shadowing variables (#2649, @aardvark179).
16+
* Fix `String#unpack("Z")` to not advance after the null byte, like CRuby (#2659, @aardvark179).
1617

1718
Compatibility:
1819

spec/ruby/core/string/unpack/z_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@
2020
["\x00a\x00 bc \x00", ["", "c"]]
2121
].should be_computed_by(:unpack, "Z5Z")
2222
end
23+
24+
it "does not advance past the null byte when given a 'Z' format specifier" do
25+
"a\x00\x0f".unpack('Zxc').should == ['a', 15]
26+
"a\x00\x0f".unpack('Zcc').should == ['a', 0, 15]
27+
end
2328
end

src/main/java/org/truffleruby/core/format/read/bytes/ReadBinaryStringNode.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ protected RubyString read(VirtualFrame frame, byte[] source,
8181
while (start + length < getSourceLength(frame) && length < count && source[start + length] != 0) {
8282
length++;
8383
}
84-
85-
if (start + length < getSourceLength(frame) && source[start + length] == 0) {
86-
length++;
87-
}
8884
} else {
8985
length = count;
9086

0 commit comments

Comments
 (0)