Skip to content

Commit 8444b6d

Browse files
committed
odb_hash*: accept the oid type to hash into
The git_odb_hash helper functions should not assume SHA1, and instead should be given the oid type that they're producing.
1 parent c50b280 commit 8444b6d

File tree

16 files changed

+136
-79
lines changed

16 files changed

+136
-79
lines changed

fuzzers/packfile_fuzzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9090
if (git_indexer_append(indexer, data, size, &stats) < 0)
9191
goto cleanup;
9292
if (append_hash) {
93-
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
93+
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB, GIT_OID_SHA1) < 0) {
9494
fprintf(stderr, "Failed to compute the SHA1 hash\n");
9595
abort();
9696
}

include/git2/odb.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,24 @@ GIT_EXTERN(int) git_odb_write_multi_pack_index(
435435
git_odb *db);
436436

437437
/**
438-
* Determine the object-ID (sha1 hash) of a data buffer
438+
* Determine the object-ID (sha1 or sha256 hash) of a data buffer
439439
*
440-
* The resulting SHA-1 OID will be the identifier for the data
441-
* buffer as if the data buffer it were to written to the ODB.
440+
* The resulting OID will be the identifier for the data buffer as if
441+
* the data buffer it were to written to the ODB.
442442
*
443443
* @param out the resulting object-ID.
444444
* @param data data to hash
445445
* @param len size of the data
446-
* @param type of the data to hash
446+
* @param object_type of the data to hash
447+
* @param oid_type the oid type to hash to
447448
* @return 0 or an error code
448449
*/
449-
GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type);
450+
GIT_EXTERN(int) git_odb_hash(
451+
git_oid *out,
452+
const void *data,
453+
size_t len,
454+
git_object_t object_type,
455+
git_oid_t oid_type);
450456

451457
/**
452458
* Read a file from disk and fill a git_oid with the object id
@@ -458,10 +464,15 @@ GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_obj
458464
*
459465
* @param out oid structure the result is written into.
460466
* @param path file to read and determine object id for
461-
* @param type the type of the object that will be hashed
467+
* @param object_type of the data to hash
468+
* @param oid_type the oid type to hash to
462469
* @return 0 or an error code
463470
*/
464-
GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_object_t type);
471+
GIT_EXTERN(int) git_odb_hashfile(
472+
git_oid *out,
473+
const char *path,
474+
git_object_t object_type,
475+
git_oid_t oid_type);
465476

466477
/**
467478
* Create a copy of an odb_object

src/cli/cmd_hash_object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static int hash_buf(git_odb *odb, git_str *buf, git_object_t type)
6464
if (git_odb_write(&oid, odb, buf->ptr, buf->size, type) < 0)
6565
return cli_error_git();
6666
} else {
67-
if (git_odb_hash(&oid, buf->ptr, buf->size, type) < 0)
67+
if (git_odb_hash(&oid, buf->ptr, buf->size, type, GIT_OID_SHA1) < 0)
6868
return cli_error_git();
6969
}
7070

src/libgit2/diff_file.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int git_diff_file_content__init_from_src(
162162
fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
163163
} else {
164164
int error;
165-
if ((error = git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB)) < 0)
165+
if ((error = git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, GIT_OID_SHA1)) < 0)
166166
return error;
167167
fc->file->size = src->buflen;
168168
fc->file->id_abbrev = GIT_OID_SHA1_HEXSIZE;
@@ -412,7 +412,8 @@ static int diff_file_content_load_workdir(
412412
/* once data is loaded, update OID if we didn't have it previously */
413413
if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
414414
error = git_odb_hash(
415-
&fc->file->id, fc->map.data, fc->map.len, GIT_OBJECT_BLOB);
415+
&fc->file->id, fc->map.data, fc->map.len,
416+
GIT_OBJECT_BLOB, GIT_OID_SHA1);
416417
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
417418
}
418419

src/libgit2/diff_generate.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ int git_diff__oid_for_entry(
660660
git_error_clear();
661661
}
662662
} else if (S_ISLNK(mode)) {
663-
error = git_odb__hashlink(out, full_path.ptr);
663+
error = git_odb__hashlink(out, full_path.ptr, GIT_OID_SHA1);
664664
diff->base.perf.oid_calculations++;
665665
} else if (!git__is_sizet(entry.file_size)) {
666666
git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'",
@@ -675,7 +675,8 @@ int git_diff__oid_for_entry(
675675
error = fd;
676676
else {
677677
error = git_odb__hashfd_filtered(
678-
out, fd, (size_t)entry.file_size, GIT_OBJECT_BLOB, fl);
678+
out, fd, (size_t)entry.file_size,
679+
GIT_OBJECT_BLOB, GIT_OID_SHA1, fl);
679680
p_close(fd);
680681
diff->base.perf.oid_calculations++;
681682
}

src/libgit2/indexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start)
546546
entry = git__calloc(1, sizeof(*entry));
547547
GIT_ERROR_CHECK_ALLOC(entry);
548548

549-
if (git_odb__hashobj(&oid, obj) < 0) {
549+
if (git_odb__hashobj(&oid, obj, GIT_OID_SHA1) < 0) {
550550
git_error_set(GIT_ERROR_INDEXER, "failed to hash object");
551551
goto on_error;
552552
}

src/libgit2/iterator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ static int filesystem_iterator_entry_hash(
12811281

12821282
if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) &&
12831283
!(error = git_path_validate_str_length(iter->base.repo, &fullpath)))
1284-
error = git_odb_hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB);
1284+
error = git_odb_hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB, GIT_OID_SHA1);
12851285

12861286
git_str_dispose(&fullpath);
12871287
return error;

src/libgit2/object.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
bool git_object__strict_input_validation = true;
2323

24-
extern int git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type);
2524
size_t git_object__size(git_object_t type);
2625

2726
typedef struct {
@@ -87,7 +86,7 @@ int git_object__from_raw(
8786
GIT_ERROR_CHECK_ALLOC(object);
8887
object->cached.flags = GIT_CACHE_STORE_PARSED;
8988
object->cached.type = type;
90-
if ((error = git_odb_hash(&object->cached.oid, data, size, type)) < 0)
89+
if ((error = git_odb_hash(&object->cached.oid, data, size, type, GIT_OID_SHA1)) < 0)
9190
return error;
9291

9392
/* Parse raw object data */

0 commit comments

Comments
 (0)