summaryrefslogtreecommitdiff
path: root/src/pack.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/pack.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/pack.c')
-rw-r--r--src/pack.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/pack.c b/src/pack.c
index d475b28ee..26a6036c2 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -624,7 +624,7 @@ int git_packfile_unpack(
struct pack_chain_elem *elem = NULL, *stack;
git_pack_cache_entry *cached = NULL;
struct pack_chain_elem small_stack[SMALL_STACK_SIZE];
- size_t stack_size = 0, elem_pos;
+ size_t stack_size = 0, elem_pos, alloclen;
git_otype base_type;
/*
@@ -684,8 +684,8 @@ int git_packfile_unpack(
if (cached && stack_size == 1) {
void *data = obj->data;
- GITERR_CHECK_ALLOC_ADD(obj->len, 1);
- obj->data = git__malloc(obj->len + 1);
+ GITERR_CHECK_ALLOC_ADD(&alloclen, obj->len, 1);
+ obj->data = git__malloc(alloclen);
GITERR_CHECK_ALLOC(obj->data);
memcpy(obj->data, data, obj->len + 1);
@@ -840,17 +840,18 @@ int packfile_unpack_compressed(
size_t size,
git_otype type)
{
+ size_t buf_size;
int st;
z_stream stream;
unsigned char *buffer, *in;
- GITERR_CHECK_ALLOC_ADD(size, 1);
- buffer = git__calloc(1, size + 1);
+ GITERR_CHECK_ALLOC_ADD(&buf_size, size, 1);
+ buffer = git__calloc(1, buf_size);
GITERR_CHECK_ALLOC(buffer);
memset(&stream, 0, sizeof(stream));
stream.next_out = buffer;
- stream.avail_out = (uInt)size + 1;
+ stream.avail_out = (uInt)buf_size;
stream.zalloc = use_git_alloc;
stream.zfree = use_git_free;
@@ -1089,17 +1090,17 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
{
struct stat st;
struct git_pack_file *p;
- size_t path_len = path ? strlen(path) : 0;
+ size_t path_len = path ? strlen(path) : 0, alloc_len;
*pack_out = NULL;
if (path_len < strlen(".idx"))
return git_odb__error_notfound("invalid packfile path", NULL);
- GITERR_CHECK_ALLOC_ADD(sizeof(*p), path_len);
- GITERR_CHECK_ALLOC_ADD(sizeof(*p) + path_len, 2);
+ GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*p), path_len);
+ GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
- p = git__calloc(1, sizeof(*p) + path_len + 2);
+ p = git__calloc(1, alloc_len);
GITERR_CHECK_ALLOC(p);
memcpy(p->pack_name, path, path_len + 1);