summaryrefslogtreecommitdiff
path: root/git-compat-util.h
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2016-10-15 16:23:11 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-10-17 21:42:31 (GMT)
commite9451782cfbe9fc9105bf63228bca3e2265af8f8 (patch)
tree273fb9ec943e2d478f61011b8a92c93d01d3b504 /git-compat-util.h
parentddd0bfac7cfaabc7c0422ecee9604ede9c4841d1 (diff)
downloadgit-e9451782cfbe9fc9105bf63228bca3e2265af8f8.zip
git-e9451782cfbe9fc9105bf63228bca3e2265af8f8.tar.gz
git-e9451782cfbe9fc9105bf63228bca3e2265af8f8.tar.bz2
avoid pointer arithmetic involving NULL in FLEX_ALLOC_MEM
Calculating offsets involving a NULL pointer is undefined. It works in practice (for now?), but we should not rely on it. Allocate first and then simply refer to the flexible array member by its name instead of performing pointer arithmetic up front. The resulting code is slightly shorter, easier to read and doesn't rely on undefined behaviour. NB: The cast to a (non-const) void pointer is necessary to keep support for flexible array members declared as const. Signed-off-by: Rene Scharfe <l.s.r@web.de> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'git-compat-util.h')
-rw-r--r--git-compat-util.h5
1 files changed, 3 insertions, 2 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index 17918d0..237cef1 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -815,8 +815,9 @@ extern FILE *fopen_for_writing(const char *path);
* 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)); \