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

Re: [PATCH v2 1/2] t/helper/test-sha1: prepare for an unsafe mode

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Nov 7, 2024, 01:49 UTC
Message-ID
<Zywcr2lMM_Ij8suu@tapette.crustytoothpaste.net>
In-Reply-To
<20241107013915.GA961214@coredump.intra.peff.net>
On 2024-11-07 at 01:39:15, Jeff King wrote:
Show 39 quoted lines
> So I think you wouldn't want to allocate an enum or a slot in the
> hash_algos array, because it is not really an independent algorithm.
> But I think it _could_ work as a separate struct that the caller derives
> from the main hash algorithm. For example, imagine that the
> git_hash_algo struct didn't have unsafe_init_fn, etc, but instead had:
> 
>   struct git_hash_algo *unsafe_implementation;
> 
> with a matching accessor like:
> 
>   struct git_hash_algo *unsafe_hash_algo(struct git_hash_algo *algo)
>   {
> 	/* if we have a faster "unsafe" implementation, use that */
> 	if (algo->unsafe_implementation)
> 		return algo->unsafe_implementation;
> 	/* otherwise just use the default one */
> 	return algo;
>   }
> 
> And then csum-file.c, rather than calling:
> 
>   the_hash_algo->unsafe_init_fn(...);
>   ...
>   the_hash_algo->unsafe_final_fn(...);
> 
> would do this:
> 
>   struct git_hash_algo *algo = unsafe_hash_algo(the_hash_algo);
>   algo->init_fn(...);
>   ...
>   algo->final_fn(...);
> 
> And likewise this test helper would have a single conditional at the
> start:
> 
>   if (unsafe)
> 	algo = unsafe_hash_algo(algo);
> 
> and the rest of the code would just use that.

Ah, yes, I think that approach would be simpler and nicer to work with than a separate entry in the `hash_algos` array. We do, however, have some places that assume that a `struct git_hash_algo *` points into the `hash_algos` array (notably `hash_algo_by_ptr`), so we'd have to adjust for that, move the function pointers out into their own struct which we'd use for `unsafe_hash_algo`, or be careful never to call the relevant functions on our special `git_hash_algo` struct.

Show 6 quoted lines
> All that said, I do not think it buys us anything for "real" code. There
> the decision between safe/unsafe is in the context of how we are using
> it, and not based on some conditional. So while the code in this test
> helper is ugly, I don't think we'd ever write anything like that for
> real. So it may not be worth the effort to refactor into a more virtual
> object-oriented way.

Yeah, I don't have a strong opinion one way or the other. I think, with the limitation I mentioned above, it would probably require a decent amount of refactoring if we took a different approach, and I'm fine with going with Taylor's current approach unless he wants to do that refactoring (in which case, great).

-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA
Previous: Jeff KingNext: Jeff King
Message 6 of 19 in “t/helper/test-tool: implement 'sha1-unsafe' helper”
  1. 0/2 t/helper/test-tool: implement 'sha1-unsafe' helperTaylor Blau, Nov 5, 2024
  2. 1/2 t/helper/test-sha1: prepare for an unsafe modeTaylor Blau, Nov 5, 2024
  3. Junio C HamanoNov 6, 2024
  4. brian m. carlsonNov 7, 2024
  5. Jeff KingNov 7, 2024
  6. brian m. carlsonNov 7, 2024
  7. Jeff KingNov 7, 2024
  8. Junio C HamanoNov 7, 2024
  9. Taylor BlauNov 7, 2024
  10. Taylor BlauNov 7, 2024
  11. Jeff KingNov 8, 2024
  12. 2/2 t/helper/test-tool: implement sha1-unsafe helperTaylor Blau, Nov 5, 2024
  13. Jeff KingNov 7, 2024
  14. brian m. carlsonNov 7, 2024
  15. Taylor BlauNov 7, 2024
  16. Jeff KingNov 8, 2024
  17. Jeff KingNov 8, 2024
  18. Taylor BlauNov 7, 2024
  19. Jeff KingNov 8, 2024

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.