Skip to content

Commit abf04bd

Browse files
phillipwoodgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4a4b31 commit abf04bd

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

xdiff/xdiffi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
337337
* One is to store the forward path and one to store the backward path.
338338
*/
339339
ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
340-
if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
340+
if (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) {
341341

342342
xdl_free_env(xe);
343343
return -1;

xdiff/xmacros.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@ do { \
4949
((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \
5050
} while (0)
5151

52+
/* Allocate an array of nr elements, returns NULL on failure */
53+
#define XDL_ALLOC_ARRAY(p, nr) \
54+
((p) = SIZE_MAX / sizeof(*(p)) >= (size_t)(nr) \
55+
? xdl_malloc((nr) * sizeof(*(p))) \
56+
: NULL)
5257

5358
#endif /* #if !defined(XMACROS_H) */

xdiff/xpatience.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static int binary_search(struct entry **sequence, int longest,
200200
*/
201201
static int find_longest_common_sequence(struct hashmap *map, struct entry **res)
202202
{
203-
struct entry **sequence = xdl_malloc(map->nr * sizeof(struct entry *));
203+
struct entry **sequence;
204204
int longest = 0, i;
205205
struct entry *entry;
206206

@@ -211,7 +211,7 @@ static int find_longest_common_sequence(struct hashmap *map, struct entry **res)
211211
*/
212212
int anchor_i = -1;
213213

214-
if (!sequence)
214+
if (!XDL_ALLOC_ARRAY(sequence, map->nr))
215215
return -1;
216216

217217
for (entry = map->first; entry; entry = entry->next) {

xdiff/xprepare.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
8686
memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
8787

8888
cf->alloc = size;
89-
if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) {
89+
if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
9090

9191
xdl_free(cf->rchash);
9292
xdl_cha_free(&cf->ncha);
@@ -178,7 +178,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
178178

179179
if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
180180
goto abort;
181-
if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
181+
if (!XDL_ALLOC_ARRAY(recs, narec))
182182
goto abort;
183183

184184
hbits = xdl_hashbits((unsigned int) narec);
@@ -215,9 +215,9 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
215215

216216
if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
217217
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
218-
if (!(rindex = xdl_malloc((nrec + 1) * sizeof(*rindex))))
218+
if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
219219
goto abort;
220-
if (!(ha = xdl_malloc((nrec + 1) * sizeof(*ha))))
220+
if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
221221
goto abort;
222222
}
223223

0 commit comments

Comments
 (0)