summaryrefslogtreecommitdiff
path: root/xdiff/xmacros.h
diff options
context:
space:
mode:
authorPhillip Wood <phillip.wood@dunelm.org.uk>2022-07-08 16:25:16 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-07-08 16:34:30 (GMT)
commitabf04bdaa845a6650d1f44b9f5914df442559d75 (patch)
treec221dbd6f3fcac8d81ed84c96a3e6bc74ade434a /xdiff/xmacros.h
parente4a4b31577c7419497ac30cebe30d755b97752c5 (diff)
downloadgit-abf04bdaa845a6650d1f44b9f5914df442559d75.zip
git-abf04bdaa845a6650d1f44b9f5914df442559d75.tar.gz
git-abf04bdaa845a6650d1f44b9f5914df442559d75.tar.bz2
xdiff: introduce XDL_ALLOC_ARRAY()
Add a helper to allocate an array that automatically calculates the allocation size. This is analogous to ALLOC_ARRAY() in the rest of the codebase but returns NULL if the allocation fails to accommodate other users of libxdiff such as libgit2. The helper will also return NULL if the multiplication in the allocation calculation overflows. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'xdiff/xmacros.h')
-rw-r--r--xdiff/xmacros.h5
1 files changed, 5 insertions, 0 deletions
diff --git a/xdiff/xmacros.h b/xdiff/xmacros.h
index ae4636c..9fd3c5d 100644
--- a/xdiff/xmacros.h
+++ b/xdiff/xmacros.h
@@ -49,5 +49,10 @@ do { \
((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \
} while (0)
+/* Allocate an array of nr elements, returns NULL on failure */
+#define XDL_ALLOC_ARRAY(p, nr) \
+ ((p) = SIZE_MAX / sizeof(*(p)) >= (size_t)(nr) \
+ ? xdl_malloc((nr) * sizeof(*(p))) \
+ : NULL)
#endif /* #if !defined(XMACROS_H) */