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

Re: [PATCH 1/4] csum-file: introduce checksum_valid()

From
Jeff King <peff@peff.net>
Date
Jun 24, 2021, 19:42 UTC
Message-ID
<YNTgCMN3ImaLmzKT@coredump.intra.peff.net>
In-Reply-To
<3f1fcf348473d7320d46e3df6fe97c482b9888f2.1624473543.git.me@ttaylorr.com>
On Wed, Jun 23, 2021 at 02:39:07PM -0400, Taylor Blau wrote:
Show 15 quoted lines
> +int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
> +{
> +	unsigned char got[GIT_MAX_RAWSZ];
> +	git_hash_ctx ctx;
> +	size_t data_len = total_len - the_hash_algo->rawsz;
> +
> +	if (total_len < the_hash_algo->rawsz)
> +		return 0; /* say "too short"? */
> +
> +	the_hash_algo->init_fn(&ctx);
> +	the_hash_algo->update_fn(&ctx, data, data_len);
> +	the_hash_algo->final_fn(got, &ctx);
> +
> +	return hasheq(got, data + data_len);
> +}

This "too short" comment is a little ugly, but I don't think it's easy to do better. This function does not otherwise print anything to stderr, so calling error() here would probably be bad. And having multiple return types would complicate the callers.

I _suspect_ it's hard to trigger anyway, because the callers would generally have complained about a too-short file in the first place. So possibly it could be a BUG(). But I think leaving it as-is is the more conservative choice, as we don't know what checks callers will have done.

In the long run, we might want to teach this function to actually write to stderr (or an error buffer), because it may be useful to say:

  hash mismatch: trailer checksum claims 1234abcd, but we computed 5678cdef

or similar. In which case we could then say "too short" in the same way. I'm content to leave that for later if somebody cares. In every case of file corruption I've seen, things are sufficiently complicated and confusing that I turn to a hex dump of the file anyway. ;)

-Peff
Previous: Taylor BlauNext: Taylor Blau
Message 3 of 16 in “midx: verify MIDX checksum before reusing”
  1. 0/4 midx: verify MIDX checksum before reusingTaylor Blau, Jun 23, 2021
  2. 1/4 csum-file: introduce checksum_valid()Taylor Blau, Jun 23, 2021
  3. Jeff KingJun 24, 2021
  4. 2/4 commit-graph: rewrite to use checksum_valid()Taylor Blau, Jun 23, 2021
  5. Jeff KingJun 24, 2021
  6. 3/4 midx: don't reuse corrupt MIDXs when writingTaylor Blau, Jun 23, 2021
  7. Jeff KingJun 24, 2021
  8. 4/4 midx: report checksum mismatches during 'verify'Taylor Blau, Jun 23, 2021
  9. Bagas SanjayaJun 24, 2021
  10. Jeff KingJun 24, 2021
  11. SZEDER GáborNov 10, 2021
  12. Jeff KingNov 11, 2021
  13. Taylor BlauNov 16, 2021
  14. t5319: corrupt more bytes of the midx checksumJeff King, Nov 16, 2021
  15. Taylor BlauNov 16, 2021
  16. Derrick StoleeNov 16, 2021

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.