Skip to content

Commit

Permalink
Merge branch 'jk/xdiff-hardening' into HEAD
Browse files Browse the repository at this point in the history
* jk/xdiff-hardening:
  xdiff: drop bdiffparam_t
  xdiff: convert size variables to size_t
  xdiff: use st_* helper functions for allocations
  • Loading branch information
peff committed Dec 11, 2024
2 parents 4e5e663 + 7aac024 commit 66367b4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 48 deletions.
2 changes: 1 addition & 1 deletion diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -3361,7 +3361,7 @@ static void emit_binary_diff_body(struct diff_options *o,
data = delta;
data_size = delta_size;
} else {
char *s = xstrfmt("%lu", two->size);
char *s = xstrfmt("%"PRIuMAX, (uintmax_t)two->size);
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
s, strlen(s), 0);
free(s);
Expand Down
1 change: 1 addition & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ static inline size_t st_add(size_t a, size_t b)
}
#define st_add3(a,b,c) st_add(st_add((a),(b)),(c))
#define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d))
#define st_add5(a,b,c,d,e) st_add(st_add4((a),(b),(c),(d)),(e))

static inline size_t st_mult(size_t a, size_t b)
{
Expand Down
9 changes: 2 additions & 7 deletions xdiff/xdiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ extern "C" {

typedef struct s_mmfile {
char *ptr;
long size;
size_t size;
} mmfile_t;

typedef struct s_mmbuffer {
char *ptr;
long size;
size_t size;
} mmbuffer_t;

typedef struct s_xpparam {
Expand Down Expand Up @@ -114,11 +114,6 @@ typedef struct s_xdemitconf {
xdl_emit_hunk_consume_func_t hunk_func;
} xdemitconf_t;

typedef struct s_bdiffparam {
long bsize;
} bdiffparam_t;


#define xdl_malloc(x) xmalloc(x)
#define xdl_calloc(n, sz) xcalloc(n, sz)
#define xdl_free(ptr) free(ptr)
Expand Down
6 changes: 3 additions & 3 deletions xdiff/xdiffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,

int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
xdfenv_t *xe) {
long ndiags;
size_t ndiags;
long *kvd, *kvdf, *kvdb;
xdalgoenv_t xenv;
diffdata_t dd1, dd2;
Expand All @@ -336,8 +336,8 @@ 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 (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) {
ndiags = st_add3(xe->xdf1.nreff, xe->xdf2.nreff, 3);
if (!XDL_ALLOC_ARRAY(kvd, st_mult(2 ,st_add(ndiags, 2)))) {

xdl_free_env(xe);
return -1;
Expand Down
72 changes: 41 additions & 31 deletions xdiff/xmerge.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2,
return 0;
}

static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
static size_t xdl_recs_copy_0(int use_orig, xdfenv_t *xe, size_t i, int count, int needs_cr, int add_nl, char *dest)
{
xrecord_t **recs;
int size = 0;
size_t size = 0;

recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i;

if (count < 1)
return 0;

for (i = 0; i < count; size += recs[i++]->size)
for (i = 0; i < count; size = st_add(size, recs[i++]->size))
if (dest)
memcpy(dest + size, recs[i]->ptr, recs[i]->size);
if (add_nl) {
Expand All @@ -133,18 +133,18 @@ static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int nee

if (dest)
dest[size] = '\n';
size++;
size = st_add(size, 1);
}
}
return size;
}

static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
static size_t xdl_recs_copy(xdfenv_t *xe, size_t i, int count, int needs_cr, int add_nl, char *dest)
{
return xdl_recs_copy_0(0, xe, i, count, needs_cr, add_nl, dest);
}

static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
static size_t xdl_orig_copy(xdfenv_t *xe, size_t i, int count, int needs_cr, int add_nl, char *dest)
{
return xdl_recs_copy_0(1, xe, i, count, needs_cr, add_nl, dest);
}
Expand Down Expand Up @@ -193,10 +193,10 @@ static int is_cr_needed(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m)
return needs_cr < 0 ? 0 : needs_cr;
}

static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
static size_t fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
xdfenv_t *xe2, const char *name2,
const char *name3,
int size, int i, int style,
size_t size, int i, int style,
xdmerge_t *m, char *dest, int marker_size)
{
int marker1_size = (name1 ? strlen(name1) + 1 : 0);
Expand All @@ -208,11 +208,12 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;

/* Before conflicting part */
size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
dest ? dest + size : NULL);
size = st_add(size,
xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
dest ? dest + size : NULL));

if (!dest) {
size += marker_size + 1 + needs_cr + marker1_size;
size = st_add5(size, marker_size, 1, needs_cr, marker1_size);
} else {
memset(dest + size, '<', marker_size);
size += marker_size;
Expand All @@ -227,13 +228,14 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
}

/* Postimage from side #1 */
size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1,
dest ? dest + size : NULL);
size = st_add(size,
xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1,
dest ? dest + size : NULL));

if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) {
/* Shared preimage */
if (!dest) {
size += marker_size + 1 + needs_cr + marker3_size;
size = st_add5(size, marker_size, 1, needs_cr, marker3_size);
} else {
memset(dest + size, '|', marker_size);
size += marker_size;
Expand All @@ -246,12 +248,13 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
dest[size++] = '\r';
dest[size++] = '\n';
}
size += xdl_orig_copy(xe1, m->i0, m->chg0, needs_cr, 1,
dest ? dest + size : NULL);
size = st_add(size,
xdl_orig_copy(xe1, m->i0, m->chg0, needs_cr, 1,
dest ? dest + size : NULL));
}

if (!dest) {
size += marker_size + 1 + needs_cr;
size = st_add4(size, marker_size, 1, needs_cr);
} else {
memset(dest + size, '=', marker_size);
size += marker_size;
Expand All @@ -261,10 +264,11 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
}

/* Postimage from side #2 */
size += xdl_recs_copy(xe2, m->i2, m->chg2, needs_cr, 1,
dest ? dest + size : NULL);
size = st_add(size,
xdl_recs_copy(xe2, m->i2, m->chg2, needs_cr, 1,
dest ? dest + size : NULL));
if (!dest) {
size += marker_size + 1 + needs_cr + marker2_size;
size = st_add5(size, marker_size, 1, needs_cr, marker2_size);
} else {
memset(dest + size, '>', marker_size);
size += marker_size;
Expand All @@ -280,14 +284,15 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
return size;
}

static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
static size_t xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
xdfenv_t *xe2, const char *name2,
const char *ancestor_name,
int favor,
xdmerge_t *m, char *dest, int style,
int marker_size)
{
int size, i;
size_t size;
int i;

for (size = i = 0; m; m = m->next) {
if (favor && !m->mode)
Expand All @@ -300,25 +305,30 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
marker_size);
else if (m->mode & 3) {
/* Before conflicting part */
size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
dest ? dest + size : NULL);
size = st_add(size,
xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
dest ? dest + size : NULL));
/* Postimage from side #1 */
if (m->mode & 1) {
int needs_cr = is_cr_needed(xe1, xe2, m);

size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, (m->mode & 2),
dest ? dest + size : NULL);
size = st_add(size,
xdl_recs_copy(xe1, m->i1,
m->chg1, needs_cr, (m->mode & 2),
dest ? dest + size : NULL));
}
/* Postimage from side #2 */
if (m->mode & 2)
size += xdl_recs_copy(xe2, m->i2, m->chg2, 0, 0,
dest ? dest + size : NULL);
size = st_add(size,
xdl_recs_copy(xe2, m->i2, m->chg2, 0, 0,
dest ? dest + size : NULL));
} else
continue;
i = m->i1 + m->chg1;
}
size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, 0,
dest ? dest + size : NULL);
size = st_add(size,
xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, 0,
dest ? dest + size : NULL));
return size;
}

Expand Down Expand Up @@ -664,7 +674,7 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1,
/* output */
if (result) {
int marker_size = xmp->marker_size;
int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
size_t size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
ancestor_name,
favor, changes, NULL, style,
marker_size);
Expand Down
10 changes: 5 additions & 5 deletions xdiff/xprepare.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
for (top = blk + bsize; cur < top; ) {
prev = cur;
hav = xdl_hash_record(&cur, top, xpp->flags);
if (XDL_ALLOC_GROW(recs, nrec + 1, narec))
if (XDL_ALLOC_GROW(recs, st_add(nrec, 1), narec))
goto abort;
if (!(crec = xdl_cha_alloc(&xdf->rcha)))
goto abort;
Expand All @@ -196,14 +196,14 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
}
}

if (!XDL_CALLOC_ARRAY(rchg, nrec + 2))
if (!XDL_CALLOC_ARRAY(rchg, st_add(nrec, 2)))
goto abort;

if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
if (!XDL_ALLOC_ARRAY(rindex, st_add(nrec, 1)))
goto abort;
if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
if (!XDL_ALLOC_ARRAY(ha, st_add(nrec, 1)))
goto abort;
}

Expand Down Expand Up @@ -369,7 +369,7 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
xdlclass_t *rcrec;
char *dis, *dis1, *dis2;

if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2))
if (!XDL_CALLOC_ARRAY(dis, st_add3(xdf1->nrec, xdf2->nrec, 2)))
return -1;
dis1 = dis;
dis2 = dis1 + xdf1->nrec + 1;
Expand Down
2 changes: 1 addition & 1 deletion xdiff/xutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void *xdl_cha_alloc(chastore_t *cha) {
void *data;

if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {
if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {
if (!(ancur = (chanode_t *) xdl_malloc(st_add(sizeof(chanode_t), cha->nsize)))) {

return NULL;
}
Expand Down

0 comments on commit 66367b4

Please sign in to comment.