]> Kevux Git Server - rit/commit
hash: provide generic wrappers to update hash contexts
authorPatrick Steinhardt <ps@pks.im>
Fri, 31 Jan 2025 12:55:30 +0000 (13:55 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 31 Jan 2025 18:06:11 +0000 (10:06 -0800)
commitb2755c15e2359c5436de062bf33a155a99c72c03
treeac603c885ead5c99365aa36725d525b333f9bb36
parent7346e340f147131ca32089f61f7d0f502f80d19d
hash: provide generic wrappers to update hash contexts

The hash context is supposed to be updated via the `git_hash_algo`
structure, which contains a list of function pointers to update, clone
or finalize a hashing context. This requires the callers to track which
algorithm was used to initialize the context and continue to use the
exact same algorithm. If they fail to do that correctly, it can happen
that we start to access context state of one hash algorithm with
functions of a different hash algorithm. The result would typically be a
segfault, as could be seen e.g. in the patches part of 98422943f0 (Merge
branch 'ps/weak-sha1-for-tail-sum-fix', 2025-01-01).

The situation was significantly improved starting with 04292c3796
(hash.h: drop unsafe_ function variants, 2025-01-23) and its parent
commits. These refactorings ensure that it is not possible to mix up
safe and unsafe variants of the same hash algorithm anymore. But in
theory, it is still possible to mix up different hash algorithms with
each other, even though this is a lot less likely to happen.

But still, we can do better: instead of asking the caller to remember
the hash algorithm used to initialize a context, we can instead make the
context itself remember which algorithm it has been initialized with. If
we do so, callers can use a set of generic helpers to update the context
and don't need to be aware of the hash algorithm at all anymore.

Adapt the context initialization functions to store the hash algorithm
in the hashing context and introduce these generic helpers. Callers will
be adapted in the subsequent commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
hash.h
object-file.c