xdiff: convert size variables to size_t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
diff --git a/src/xdiff/xdiffi.c b/src/xdiff/xdiffi.c
index 0620e5f..f4d01b4 100644
--- a/src/xdiff/xdiffi.c
+++ b/src/xdiff/xdiffi.c
@@ -21,7 +21,8 @@
*/
#include "xinclude.h"
-
+#include "common.h"
+#include "integer.h"
#define XDL_MAX_COST_MIN 256
@@ -323,7 +324,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, allocsize;
long *kvd, *kvdf, *kvdb;
xdalgoenv_t xenv;
diffdata_t dd1, dd2;
@@ -343,9 +344,12 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
* Allocate and setup K vectors to be used by the differential algorithm.
* 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)))) {
+ GITERR_CHECK_ALLOC_ADD3(&ndiags, xe->xdf1.nreff, xe->xdf2.nreff, 3);
+ GITERR_CHECK_ALLOC_MULTIPLY(&allocsize, ndiags, 2);
+ GITERR_CHECK_ALLOC_ADD(&allocsize, allocsize, 2);
+ GITERR_CHECK_ALLOC_MULTIPLY(&allocsize, allocsize, sizeof(long));
+ if (!(kvd = (long *) xdl_malloc(allocsize))) {
xdl_free_env(xe);
return -1;
}
diff --git a/src/xdiff/xhistogram.c b/src/xdiff/xhistogram.c
index 500d811..0c2edb8 100644
--- a/src/xdiff/xhistogram.c
+++ b/src/xdiff/xhistogram.c
@@ -44,6 +44,7 @@
#include "xinclude.h"
#include "xtypes.h"
#include "xdiff.h"
+#include "common.h"
#define MAX_PTR UINT_MAX
#define MAX_CNT UINT_MAX
@@ -271,7 +272,7 @@ static int histogram_diff(
{
struct histindex index;
struct region lcs;
- unsigned int sz;
+ size_t sz;
int result = -1;
if (count1 <= 0 && count2 <= 0)
@@ -302,7 +303,8 @@ static int histogram_diff(
index.table_bits = xdl_hashbits(count1);
sz = index.records_size = 1 << index.table_bits;
- sz *= sizeof(struct record *);
+ GITERR_CHECK_ALLOC_MULTIPLY(&sz, sz, sizeof(struct record *));
+
if (!(index.records = (struct record **) xdl_malloc(sz)))
goto cleanup;
memset(index.records, 0, sz);
diff --git a/src/xdiff/xmerge.c b/src/xdiff/xmerge.c
index b11e598..7b7e0e2 100644
--- a/src/xdiff/xmerge.c
+++ b/src/xdiff/xmerge.c
@@ -21,6 +21,7 @@
*/
#include "xinclude.h"
+#include "common.h"
typedef struct s_xdmerge {
struct s_xdmerge *next;
@@ -109,59 +110,74 @@ 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 add_nl, char *dest)
+static int xdl_recs_copy_0(size_t *out, int use_orig, xdfenv_t *xe, int i, int count, int add_nl, char *dest)
{
xrecord_t **recs;
- int size = 0;
+ size_t size = 0;
+
+ *out = 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; ) {
if (dest)
memcpy(dest + size, recs[i]->ptr, recs[i]->size);
+
+ GITERR_CHECK_ALLOC_ADD(&size, size, recs[i++]->size);
+ }
+
if (add_nl) {
i = recs[count - 1]->size;
if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') {
if (dest)
dest[size] = '\n';
- size++;
+
+ GITERR_CHECK_ALLOC_ADD(&size, size, 1);
}
}
- return size;
+
+ *out = size;
+ return 0;
}
-static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
+static int xdl_recs_copy(size_t *out, xdfenv_t *xe, int i, int count, int add_nl, char *dest)
{
- return xdl_recs_copy_0(0, xe, i, count, add_nl, dest);
+ return xdl_recs_copy_0(out, 0, xe, i, count, add_nl, dest);
}
-static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
+static int xdl_orig_copy(size_t *out, xdfenv_t *xe, int i, int count, int add_nl, char *dest)
{
- return xdl_recs_copy_0(1, xe, i, count, add_nl, dest);
+ return xdl_recs_copy_0(out, 1, xe, i, count, add_nl, dest);
}
-static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
+static int fill_conflict_hunk(size_t *out, 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 ? (int)strlen(name1) + 1 : 0);
int marker2_size = (name2 ? (int)strlen(name2) + 1 : 0);
int marker3_size = (name3 ? (int)strlen(name3) + 1 : 0);
+ size_t copied;
+
+ *out = 0;
if (marker_size <= 0)
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
/* Before conflicting part */
- size += xdl_recs_copy(xe1, i, m->i1 - i, 0,
- dest ? dest + size : NULL);
+ if (xdl_recs_copy(&copied, xe1, i, m->i1 - i, 0,
+ dest ? dest + size : NULL) < 0)
+ return -1;
+
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
if (!dest) {
- size += marker_size + 1 + marker1_size;
+ GITERR_CHECK_ALLOC_ADD4(&size, size, marker_size, 1, marker1_size);
} else {
memset(dest + size, '<', marker_size);
size += marker_size;
@@ -174,13 +190,16 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
}
/* Postimage from side #1 */
- size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
- dest ? dest + size : NULL);
+ if (xdl_recs_copy(&copied, xe1, m->i1, m->chg1, 1,
+ dest ? dest + size : NULL) < 0)
+ return -1;
+
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
if (style == XDL_MERGE_DIFF3) {
/* Shared preimage */
if (!dest) {
- size += marker_size + 1 + marker3_size;
+ GITERR_CHECK_ALLOC_ADD4(&size, size, marker_size, 1, marker3_size);
} else {
memset(dest + size, '|', marker_size);
size += marker_size;
@@ -191,12 +210,15 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
}
dest[size++] = '\n';
}
- size += xdl_orig_copy(xe1, m->i0, m->chg0, 1,
- dest ? dest + size : NULL);
+
+ if (xdl_orig_copy(&copied, xe1, m->i0, m->chg0, 1,
+ dest ? dest + size : NULL) < 0)
+ return -1;
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
}
if (!dest) {
- size += marker_size + 1;
+ GITERR_CHECK_ALLOC_ADD3(&size, size, marker_size, 1);
} else {
memset(dest + size, '=', marker_size);
size += marker_size;
@@ -204,10 +226,14 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
}
/* Postimage from side #2 */
- size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
- dest ? dest + size : NULL);
+
+ if (xdl_recs_copy(&copied, xe2, m->i2, m->chg2, 1,
+ dest ? dest + size : NULL) < 0)
+ return -1;
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
+
if (!dest) {
- size += marker_size + 1 + marker2_size;
+ GITERR_CHECK_ALLOC_ADD4(&size, size, marker_size, 1, marker2_size);
} else {
memset(dest + size, '>', marker_size);
size += marker_size;
@@ -218,46 +244,69 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
}
dest[size++] = '\n';
}
- return size;
+
+ *out = size;
+ return 0;
}
-static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
+static int xdl_fill_merge_buffer(size_t *out,
+ 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, copied;
+ int i;
+
+ *out = 0;
for (size = i = 0; m; m = m->next) {
if (favor && !m->mode)
m->mode = favor;
- if (m->mode == 0)
- size = fill_conflict_hunk(xe1, name1, xe2, name2,
+ if (m->mode == 0) {
+ if (fill_conflict_hunk(&size, xe1, name1, xe2, name2,
ancestor_name,
size, i, style, m, dest,
- marker_size);
+ marker_size) < 0)
+ return -1;
+ }
else if (m->mode & 3) {
/* Before conflicting part */
- size += xdl_recs_copy(xe1, i, m->i1 - i, 0,
- dest ? dest + size : NULL);
+ if (xdl_recs_copy(&copied, xe1, i, m->i1 - i, 0,
+ dest ? dest + size : NULL) < 0)
+ return -1;
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
+
/* Postimage from side #1 */
- if (m->mode & 1)
- size += xdl_recs_copy(xe1, m->i1, m->chg1, (m->mode & 2),
- dest ? dest + size : NULL);
+ if (m->mode & 1) {
+ if (xdl_recs_copy(&copied, xe1, m->i1, m->chg1, (m->mode & 2),
+ dest ? dest + size : NULL) < 0)
+ return -1;
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
+ }
+
/* Postimage from side #2 */
- if (m->mode & 2)
- size += xdl_recs_copy(xe2, m->i2, m->chg2, 0,
- dest ? dest + size : NULL);
+ if (m->mode & 2) {
+ if (xdl_recs_copy(&copied, xe2, m->i2, m->chg2, 0,
+ dest ? dest + size : NULL) < 0)
+ return -1;
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
+ }
} else
continue;
i = m->i1 + m->chg1;
}
- size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0,
- dest ? dest + size : NULL);
- return size;
+
+ if (xdl_recs_copy(&copied, xe1, i, xe1->xdf2.nrec - i, 0,
+ dest ? dest + size : NULL) < 0)
+ return -1;
+ GITERR_CHECK_ALLOC_ADD(&size, size, copied);
+
+ *out = size;
+ return 0;
}
/*
@@ -551,19 +600,24 @@ 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;
+
+ if (xdl_fill_merge_buffer(&size, xe1, name1, xe2, name2,
ancestor_name,
favor, changes, NULL, style,
- marker_size);
+ marker_size) < 0)
+ return -1;
+
result->ptr = xdl_malloc(size);
if (!result->ptr) {
xdl_cleanup_merge(changes);
return -1;
}
result->size = size;
- xdl_fill_merge_buffer(xe1, name1, xe2, name2,
+ if (xdl_fill_merge_buffer(&size, xe1, name1, xe2, name2,
ancestor_name, favor, changes,
- result->ptr, style, marker_size);
+ result->ptr, style, marker_size) < 0)
+ return -1;
}
return xdl_cleanup_merge(changes);
}