summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2016-10-28 09:01:18 -0700
committerJunio C Hamano <gitster@pobox.com>2016-10-28 09:01:18 -0700
commit63cf124c2436722d70c28fc12f9337e9938091ef (patch)
tree4fdfa0d8d525408af57570ff22824373905db5a2
parent39000e849970a554a257577dcb2fb844a523a1d1 (diff)
parent0ac52a38e8008fa5bb243e150031681420c639fa (diff)
downloadgit-63cf124c2436722d70c28fc12f9337e9938091ef.tar.gz
Merge branch 'jk/tighten-alloc' into maint
Protect our code from over-eager compilers. * jk/tighten-alloc: inline xalloc_flex() into FLEXPTR_ALLOC_MEM avoid pointer arithmetic involving NULL in FLEX_ALLOC_MEM
-rw-r--r--git-compat-util.h17
1 files changed, 6 insertions, 11 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index aec8cc9890..b4d9c2aa58 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -848,11 +848,14 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
* times, and it must be assignable as an lvalue.
*/
#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
- (x) = NULL; /* silence -Wuninitialized for offset calculation */ \
- (x) = xalloc_flex(sizeof(*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
} while (0)
#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
- (x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((x) + 1, (buf), flex_array_len_); \
(x)->ptrname = (void *)((x)+1); \
} while(0)
#define FLEX_ALLOC_STR(x, flexname, str) \
@@ -860,14 +863,6 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
-static inline void *xalloc_flex(size_t base_len, size_t offset,
- const void *src, size_t src_len)
-{
- unsigned char *ret = xcalloc(1, st_add3(base_len, src_len, 1));
- memcpy(ret + offset, src, src_len);
- return ret;
-}
-
static inline char *xstrdup_or_null(const char *str)
{
return str ? xstrdup(str) : NULL;