]> git.ipfire.org Git - thirdparty/git.git/commitdiff
xdiff: introduce XDL_ALLOC_ARRAY()
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Fri, 8 Jul 2022 16:25:16 +0000 (16:25 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 8 Jul 2022 16:34:30 +0000 (09:34 -0700)
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>
xdiff/xdiffi.c
xdiff/xmacros.h
xdiff/xpatience.c
xdiff/xprepare.c

index 758410c11ac286adc77c6f992e51822def202a40..53e803e6bcb249c38b0d09d7811fa207513ada81 100644 (file)
@@ -337,7 +337,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
         * One is to store the forward path and one to store the backward path.
         */
        ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
-       if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
+       if (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) {
 
                xdl_free_env(xe);
                return -1;
index ae4636c2477cc640eae84578805d9722d5e28d1b..9fd3c5da91a9caf0b59d507832127b403e9d9c80 100644 (file)
@@ -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) */
index 1a21c6a74b368cb094e20c708a43071c72558d7e..ce87b9084ca70d6afddb09801e676b19448e492f 100644 (file)
@@ -200,7 +200,7 @@ static int binary_search(struct entry **sequence, int longest,
  */
 static int find_longest_common_sequence(struct hashmap *map, struct entry **res)
 {
-       struct entry **sequence = xdl_malloc(map->nr * sizeof(struct entry *));
+       struct entry **sequence;
        int longest = 0, i;
        struct entry *entry;
 
@@ -211,7 +211,7 @@ static int find_longest_common_sequence(struct hashmap *map, struct entry **res)
         */
        int anchor_i = -1;
 
-       if (!sequence)
+       if (!XDL_ALLOC_ARRAY(sequence, map->nr))
                return -1;
 
        for (entry = map->first; entry; entry = entry->next) {
index 105752758f2f3870448f1ec1cf0070c3a29b36e6..25866a1667a54aa759b4434d3b8caa3281c92213 100644 (file)
@@ -86,7 +86,7 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
        memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
 
        cf->alloc = size;
-       if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) {
+       if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
 
                xdl_free(cf->rchash);
                xdl_cha_free(&cf->ncha);
@@ -178,7 +178,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
 
        if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
                goto abort;
-       if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
+       if (!XDL_ALLOC_ARRAY(recs, narec))
                goto abort;
 
        hbits = xdl_hashbits((unsigned int) narec);
@@ -215,9 +215,9 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
 
        if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
            (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
-               if (!(rindex = xdl_malloc((nrec + 1) * sizeof(*rindex))))
+               if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
                        goto abort;
-               if (!(ha = xdl_malloc((nrec + 1) * sizeof(*ha))))
+               if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
                        goto abort;
        }