summaryrefslogtreecommitdiff
path: root/src/tree.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-12-04 20:00:41 -0500
committerGitHub <noreply@github.com>2021-12-04 20:00:41 -0500
commit12b53eb0318b0529514ad7e318de70b8325d43f2 (patch)
treedcec3efee630f9c38e15433302e5c6faec65e517 /src/tree.c
parent6fdb1b2f55da9593576b096ee2eecce61995fb51 (diff)
parent9f03ebd14b6beb00a9bed52c0568a13f8d5ebb08 (diff)
downloadlibgit2-12b53eb0318b0529514ad7e318de70b8325d43f2.tar.gz
Merge pull request #6128 from libgit2/ethomson/object_validation
Introduce `git_object_rawcontent_is_valid`
Diffstat (limited to 'src/tree.c')
-rw-r--r--src/tree.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/tree.c b/src/tree.c
index b8e82d485..7290e154d 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -351,15 +351,26 @@ size_t git_treebuilder_entrycount(git_treebuilder *bld)
return git_strmap_size(bld->map);
}
-static int tree_error(const char *str, const char *path)
+GIT_INLINE(void) set_error(const char *str, const char *path)
{
if (path)
git_error_set(GIT_ERROR_TREE, "%s - %s", str, path);
else
git_error_set(GIT_ERROR_TREE, "%s", str);
+}
+
+static int tree_error(const char *str, const char *path)
+{
+ set_error(str, path);
return -1;
}
+static int tree_parse_error(const char *str, const char *path)
+{
+ set_error(str, path);
+ return GIT_EINVALID;
+}
+
static int parse_mode(uint16_t *mode_out, const char *buffer, size_t buffer_len, const char **buffer_out)
{
int32_t mode;
@@ -399,19 +410,19 @@ int git_tree__parse_raw(void *_tree, const char *data, size_t size)
uint16_t attr;
if (parse_mode(&attr, buffer, buffer_end - buffer, &buffer) < 0 || !buffer)
- return tree_error("failed to parse tree: can't parse filemode", NULL);
+ return tree_parse_error("failed to parse tree: can't parse filemode", NULL);
if (buffer >= buffer_end || (*buffer++) != ' ')
- return tree_error("failed to parse tree: missing space after filemode", NULL);
+ return tree_parse_error("failed to parse tree: missing space after filemode", NULL);
if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL)
- return tree_error("failed to parse tree: object is corrupted", NULL);
+ return tree_parse_error("failed to parse tree: object is corrupted", NULL);
if ((filename_len = nul - buffer) == 0 || filename_len > UINT16_MAX)
- return tree_error("failed to parse tree: can't parse filename", NULL);
+ return tree_parse_error("failed to parse tree: can't parse filename", NULL);
if ((buffer_end - (nul + 1)) < GIT_OID_RAWSZ)
- return tree_error("failed to parse tree: can't parse OID", NULL);
+ return tree_parse_error("failed to parse tree: can't parse OID", NULL);
/* Allocate the entry */
{
@@ -434,16 +445,15 @@ int git_tree__parse_raw(void *_tree, const char *data, size_t size)
int git_tree__parse(void *_tree, git_odb_object *odb_obj)
{
git_tree *tree = _tree;
+ const char *data = git_odb_object_data(odb_obj);
+ size_t size = git_odb_object_size(odb_obj);
+ int error;
- if ((git_tree__parse_raw(tree,
- git_odb_object_data(odb_obj),
- git_odb_object_size(odb_obj))) < 0)
- return -1;
-
- if (git_odb_object_dup(&tree->odb_obj, odb_obj) < 0)
- return -1;
+ if ((error = git_tree__parse_raw(tree, data, size)) < 0 ||
+ (error = git_odb_object_dup(&tree->odb_obj, odb_obj)) < 0)
+ return error;
- return 0;
+ return error;
}
static size_t find_next_dir(const char *dirname, git_index *index, size_t start)