summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@github.com>2016-02-28 14:37:37 -0500
committerEdward Thomson <ethomson@github.com>2016-02-28 18:54:39 -0500
commit3ef01e772729f44c2871b590577cc64e4a58a381 (patch)
treec13ad5bbf6671e80dca5826f12604807daf1d96e
parent6ddf533afca5e25b7c532b4c164ae66e06f9c1c2 (diff)
downloadlibgit2-3ef01e772729f44c2871b590577cc64e4a58a381.tar.gz
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.
-rw-r--r--src/object.c24
-rw-r--r--src/object.h19
2 files changed, 28 insertions, 15 deletions
diff --git a/src/object.c b/src/object.c
index 7f7de9fee..e7c1fef09 100644
--- a/src/object.c
+++ b/src/object.c
@@ -467,3 +467,27 @@ int git_object_short_id(git_buf *out, const git_object *obj)
return error;
}
+bool git_object__is_valid(
+ git_repository *repo, const git_oid *id, git_otype expected_type)
+{
+ git_odb *odb;
+ git_otype actual_type;
+ size_t len;
+ int error;
+
+ if (!git_object__strict_input_validation)
+ return true;
+
+ if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
+ (error = git_odb_read_header(&len, &actual_type, odb, id)) < 0)
+ return false;
+
+ if (expected_type != GIT_OBJ_ANY && expected_type != actual_type) {
+ giterr_set(GITERR_INVALID,
+ "the requested type does not match the type in the ODB");
+ return false;
+ }
+
+ return true;
+}
+
diff --git a/src/object.h b/src/object.h
index 13edf3118..dd227d16d 100644
--- a/src/object.h
+++ b/src/object.h
@@ -7,6 +7,8 @@
#ifndef INCLUDE_object_h__
#define INCLUDE_object_h__
+#include "repository.h"
+
extern bool git_object__strict_input_validation;
/** Base git object for inheritance */
@@ -30,21 +32,8 @@ int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end
void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid);
-GIT_INLINE(bool) git_object__is_valid(
- git_repository *repo, const git_oid *id, git_otype type)
-{
- git_object *obj = NULL;
- bool valid = true;
-
- if (git_object__strict_input_validation) {
- if (git_object_lookup(&obj, repo, id, type) < 0)
- valid = false;
-
- git_object_free(obj);
- }
-
- return valid;
-}
+bool git_object__is_valid(
+ git_repository *repo, const git_oid *id, git_otype expected_type);
GIT_INLINE(git_otype) git_object__type_from_filemode(git_filemode_t mode)
{