summaryrefslogtreecommitdiff
path: root/src/tree.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@microsoft.com>2015-02-12 12:19:37 -0500
committerEdward Thomson <ethomson@microsoft.com>2015-02-13 09:27:33 -0500
commitf1453c59b2afb9dab43281bfe9f1ba34cf6e0d02 (patch)
treecb189e211547042080f35227b7e4d3f9b0c8ac2a /src/tree.c
parent650e45f69124bd8b53ecefddeb214a82538ab2c1 (diff)
downloadlibgit2-f1453c59b2afb9dab43281bfe9f1ba34cf6e0d02.tar.gz
Make our overflow check look more like gcc/clang's
Make our overflow checking look more like gcc and clang's, so that we can substitute it out with the compiler instrinsics on platforms that support it. This means dropping the ability to pass `NULL` as an out parameter. As a result, the macros also get updated to reflect this as well.
Diffstat (limited to 'src/tree.c')
-rw-r--r--src/tree.c18
1 files changed, 7 insertions, 11 deletions
diff --git a/src/tree.c b/src/tree.c
index 573e56447..9fd4e0a07 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -84,12 +84,11 @@ int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
static git_tree_entry *alloc_entry(const char *filename)
{
git_tree_entry *entry = NULL;
- size_t filename_len = strlen(filename),
- tree_len = sizeof(git_tree_entry);
+ size_t filename_len = strlen(filename), tree_len;
- if (GIT_ALLOC_OVERFLOW_ADD(tree_len, filename_len) ||
- GIT_ALLOC_OVERFLOW_ADD(tree_len + filename_len, 1) ||
- !(entry = git__malloc(tree_len + filename_len + 1)))
+ if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
+ GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1) ||
+ !(entry = git__malloc(tree_len)))
return NULL;
memset(entry, 0x0, sizeof(git_tree_entry));
@@ -207,16 +206,13 @@ void git_tree_entry_free(git_tree_entry *entry)
int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source)
{
- size_t total_size = sizeof(git_tree_entry);
+ size_t total_size;
git_tree_entry *copy;
assert(source);
- GITERR_CHECK_ALLOC_ADD(total_size, source->filename_len);
- total_size += source->filename_len;
-
- GITERR_CHECK_ALLOC_ADD(total_size, 1);
- total_size++;
+ GITERR_CHECK_ALLOC_ADD(&total_size, sizeof(git_tree_entry), source->filename_len);
+ GITERR_CHECK_ALLOC_ADD(&total_size, total_size, 1);
copy = git__malloc(total_size);
GITERR_CHECK_ALLOC(copy);