Skip to content

Commit 4815c3c

Browse files
peffgitster
authored andcommitted
t: avoid perl's pack/unpack "Q" specifier
The perl script introduced by 86b008e (t: add library for munging chunk-format files, 2023-10-09) uses pack("Q") and unpack("Q") to read and write 64-bit values ("quadwords" in perl parlance) from the on-disk chunk files. However, some builds of perl may not support 64-bit integers at all, and throw an exception here. While some 32-bit platforms may still support 64-bit integers in perl (such as our linux32 CI environment), others reportedly don't (the NonStop 32-bit builds). We can work around this by treating the 64-bit values as two 32-bit values. We can't ever combine them into a single 64-bit value, but in practice this is OK. These are representing file offsets, and our files are much smaller than 4GB. So the upper half of the 64-bit value will always be 0. We can just introduce a few helper functions which perform the translation and double-check our assumptions. Reported-by: Randall S. Becker <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7538f9d commit 4815c3c

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

t/lib-chunk/corrupt-chunk-file.pl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,38 @@ sub copy {
2121
return $buf;
2222
}
2323

24+
# Some platforms' perl builds don't support 64-bit integers, and hence do not
25+
# allow packing/unpacking quadwords with "Q". The chunk format uses 64-bit file
26+
# offsets to support files of any size, but in practice our test suite will
27+
# only use small files. So we can fake it by asking for two 32-bit values and
28+
# discarding the first (most significant) one, which is equivalent as long as
29+
# it's just zero.
30+
sub unpack_quad {
31+
my $bytes = shift;
32+
my ($n1, $n2) = unpack("NN", $bytes);
33+
die "quad value exceeds 32 bits" if $n1;
34+
return $n2;
35+
}
36+
sub pack_quad {
37+
my $n = shift;
38+
my $ret = pack("NN", 0, $n);
39+
# double check that our original $n did not exceed the 32-bit limit.
40+
# This is presumably impossible on a 32-bit system (which would have
41+
# truncated much earlier), but would still alert us on a 64-bit build
42+
# of a new test that would fail on a 32-bit build (though we'd
43+
# presumably see the die() from unpack_quad() in such a case).
44+
die "quad round-trip failed" if unpack_quad($ret) != $n;
45+
return $ret;
46+
}
47+
2448
# read until we find table-of-contents entry for chunk;
2549
# note that we cheat a bit by assuming 4-byte alignment and
2650
# that no ToC entry will accidentally look like a header.
2751
#
2852
# If we don't find the entry, copy() will hit EOF and exit
2953
# (which should cause the caller to fail the test).
3054
while (copy(4) ne $chunk) { }
31-
my $offset = unpack("Q>", copy(8));
55+
my $offset = unpack_quad(copy(8));
3256

3357
# In clear mode, our length will change. So figure out
3458
# the length by comparing to the offset of the next chunk, and
@@ -38,11 +62,11 @@ sub copy {
3862
my $id;
3963
do {
4064
$id = copy(4);
41-
my $next = unpack("Q>", get(8));
65+
my $next = unpack_quad(get(8));
4266
if (!defined $len) {
4367
$len = $next - $offset;
4468
}
45-
print pack("Q>", $next - $len + length($bytes));
69+
print pack_quad($next - $len + length($bytes));
4670
} while (unpack("N", $id));
4771
}
4872

0 commit comments

Comments
 (0)