git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] t: avoid perl's pack/unpack "Q" specifier

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 4, 2023, 01:47 UTC
Message-ID
<xmqq5y2ii91p.fsf@gitster.g>
In-Reply-To
<20231103162019.GB1470570@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 12 quoted lines
> +# Some platforms' perl builds don't support 64-bit integers, and hence do not
> +# allow packing/unpacking quadwords with "Q". The chunk format uses 64-bit file
> +# offsets to support files of any size, but in practice our test suite will
> +# only use small files. So we can fake it by asking for two 32-bit values and
> +# discarding the first (most significant) one, which is equivalent as long as
> +# it's just zero.
> +sub unpack_quad {
> +	my $bytes = shift;
> +	my ($n1, $n2) = unpack("NN", $bytes);
> +	die "quad value exceeds 32 bits" if $n1;
> +	return $n2;
> +};
Is this an unnecessary ';' at the end?
Show 11 quoted lines
> +sub pack_quad {
> +	my $n = shift;
> +	my $ret = pack("NN", 0, $n);
> +	# double check that our original $n did not exceed the 32-bit limit.
> +	# This is presumably impossible on a 32-bit system (which would have
> +	# truncated much earlier), but would still alert us on a 64-bit build
> +	# of a new test that would fail on a 32-bit build (though we'd
> +	# presumably see the die() from unpack_quad() in such a case).
> +	die "quad round-trip failed" if unpack_quad($ret) != $n;
> +	return $ret;
> +}
Nice.  Both sub are done carefully.
Show 25 quoted lines
>  # read until we find table-of-contents entry for chunk;
>  # note that we cheat a bit by assuming 4-byte alignment and
>  # that no ToC entry will accidentally look like a header.
>  #
>  # If we don't find the entry, copy() will hit EOF and exit
>  # (which should cause the caller to fail the test).
>  while (copy(4) ne $chunk) { }
> -my $offset = unpack("Q>", copy(8));
> +my $offset = unpack_quad(copy(8));
>  
>  # In clear mode, our length will change. So figure out
>  # the length by comparing to the offset of the next chunk, and
> @@ -38,11 +62,11 @@ sub copy {
>  	my $id;
>  	do {
>  		$id = copy(4);
> -		my $next = unpack("Q>", get(8));
> +		my $next = unpack_quad(get(8));
>  		if (!defined $len) {
>  			$len = $next - $offset;
>  		}
> -		print pack("Q>", $next - $len + length($bytes));
> +		print pack_quad($next - $len + length($bytes));
>  	} while (unpack("N", $id));
>  }
Previous: Jeff KingNext: Jeff King
Message 6 of 10 in “[BUG] Git 2.43.0-rc0 - t4216 unpack(Q) invalid type”
  1. rsbecker@nexbridge.comNov 3, 2023
  2. rsbecker@nexbridge.comNov 3, 2023
  3. Jeff KingNov 3, 2023
  4. rsbecker@nexbridge.comNov 3, 2023
  5. t: avoid perl's pack/unpack "Q" specifierJeff King, Nov 3, 2023
  6. Junio C HamanoNov 4, 2023
  7. Jeff KingNov 4, 2023
  8. rsbecker@nexbridge.comNov 3, 2023
  9. Jeff KingNov 3, 2023
  10. rsbecker@nexbridge.comNov 3, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.