The `oidcmp()` and `oideq()` functions only compare the prefix length as specified by the given hash algorithm. This mandates that the object IDs have a valid hash algorithm set, or otherwise we wouldn't be able to figure out that prefix. As we do not have a hash algorithm in many cases, for example when handling null object IDs, this assumption cannot always be fulfilled. We thus have a fallback in place that instead uses `the_repository` to derive the hash function. This implicit dependency is hidden away from callers and can be quite surprising, especially in contexts where there may be no repository. In theory, we can adapt those functions to always memcmp(3P) the whole length of their hash arrays. But there exist a couple of sites where we populate `struct object_id`s such that only the prefix of its hash that is actually used by the hash algorithm is populated. The remaining bytes are left uninitialized. The fact that those bytes are uninitialized also leads to warnings under Valgrind in some places where we copy those bytes. Refactor callsites where we populate object IDs to always initialize all bytes. This also allows us to get rid of `oidcpy_with_padding()`, for one because the input is now fully initialized, and because `oidcpy()` will now always copy the whole hash array. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
45 lines
977 B
C
45 lines
977 B
C
#ifndef HASH_H
|
|
#define HASH_H
|
|
|
|
#include "hash-ll.h"
|
|
#include "repository.h"
|
|
|
|
#define the_hash_algo the_repository->hash_algo
|
|
|
|
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
|
|
{
|
|
const struct git_hash_algo *algop;
|
|
if (!oid1->algo)
|
|
algop = the_hash_algo;
|
|
else
|
|
algop = &hash_algos[oid1->algo];
|
|
return hashcmp(oid1->hash, oid2->hash, algop);
|
|
}
|
|
|
|
static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
|
|
{
|
|
const struct git_hash_algo *algop;
|
|
if (!oid1->algo)
|
|
algop = the_hash_algo;
|
|
else
|
|
algop = &hash_algos[oid1->algo];
|
|
return hasheq(oid1->hash, oid2->hash, algop);
|
|
}
|
|
|
|
static inline int is_null_oid(const struct object_id *oid)
|
|
{
|
|
return oideq(oid, null_oid());
|
|
}
|
|
|
|
static inline int is_empty_blob_oid(const struct object_id *oid)
|
|
{
|
|
return oideq(oid, the_hash_algo->empty_blob);
|
|
}
|
|
|
|
static inline int is_empty_tree_oid(const struct object_id *oid)
|
|
{
|
|
return oideq(oid, the_hash_algo->empty_tree);
|
|
}
|
|
|
|
#endif
|