Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ef01e7

Browse files
author
Edward Thomson
committed
git_object__is_valid: use odb_read_header
This allows lighter weight validation in `git_object__is_valid` that does not require reading the entire object.
1 parent 6ddf533 commit 3ef01e7

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/object.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,27 @@ int git_object_short_id(git_buf *out, const git_object *obj)
467467
return error;
468468
}
469469

470+
bool git_object__is_valid(
471+
git_repository *repo, const git_oid *id, git_otype expected_type)
472+
{
473+
git_odb *odb;
474+
git_otype actual_type;
475+
size_t len;
476+
int error;
477+
478+
if (!git_object__strict_input_validation)
479+
return true;
480+
481+
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
482+
(error = git_odb_read_header(&len, &actual_type, odb, id)) < 0)
483+
return false;
484+
485+
if (expected_type != GIT_OBJ_ANY && expected_type != actual_type) {
486+
giterr_set(GITERR_INVALID,
487+
"the requested type does not match the type in the ODB");
488+
return false;
489+
}
490+
491+
return true;
492+
}
493+

src/object.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef INCLUDE_object_h__
88
#define INCLUDE_object_h__
99

10+
#include "repository.h"
11+
1012
extern bool git_object__strict_input_validation;
1113

1214
/** Base git object for inheritance */
@@ -30,21 +32,8 @@ int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end
3032

3133
void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid);
3234

33-
GIT_INLINE(bool) git_object__is_valid(
34-
git_repository *repo, const git_oid *id, git_otype type)
35-
{
36-
git_object *obj = NULL;
37-
bool valid = true;
38-
39-
if (git_object__strict_input_validation) {
40-
if (git_object_lookup(&obj, repo, id, type) < 0)
41-
valid = false;
42-
43-
git_object_free(obj);
44-
}
45-
46-
return valid;
47-
}
35+
bool git_object__is_valid(
36+
git_repository *repo, const git_oid *id, git_otype expected_type);
4837

4938
GIT_INLINE(git_otype) git_object__type_from_filemode(git_filemode_t mode)
5039
{

0 commit comments

Comments
 (0)