summaryrefslogtreecommitdiff
path: root/src/vector.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@microsoft.com>2015-02-09 23:41:13 -0500
committerEdward Thomson <ethomson@edwardthomson.com>2015-02-12 22:54:46 -0500
commit392702ee2c88d7d8aaff25f7a84acb73606f9094 (patch)
tree97a66fe6e488797c6a9c2680ccb31964f61fe340 /src/vector.c
parentd24a5312d8ab6d3cdb259e450ec9f1e2e6f3399d (diff)
downloadlibgit2-392702ee2c88d7d8aaff25f7a84acb73606f9094.tar.gz
allocations: test for overflow of requested size
Introduce some helper macros to test integer overflow from arithmetic and set error message appropriately.
Diffstat (limited to 'src/vector.c')
-rw-r--r--src/vector.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/vector.c b/src/vector.c
index c769b696a..b636032b1 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -29,12 +29,12 @@ GIT_INLINE(size_t) compute_new_size(git_vector *v)
GIT_INLINE(int) resize_vector(git_vector *v, size_t new_size)
{
- size_t new_bytes = new_size * sizeof(void *);
+ size_t new_bytes;
void *new_contents;
/* Check for overflow */
- if (new_bytes / sizeof(void *) != new_size)
- GITERR_CHECK_ALLOC(NULL);
+ GITERR_CHECK_ALLOC_MULTIPLY(new_size, sizeof(void *));
+ new_bytes = new_size * sizeof(void *);
new_contents = git__realloc(v->contents, new_bytes);
GITERR_CHECK_ALLOC(new_contents);
@@ -51,6 +51,7 @@ int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp)
assert(v && src);
+ GITERR_CHECK_ALLOC_MULTIPLY(src->length, sizeof(void *));
bytes = src->length * sizeof(void *);
v->_alloc_size = src->length;