comparison mercurial/bdiff.c @ 18551:d6fb7bbec16a

bdiff: simplify overflow checks Rather than check that each delta start, end, and length is within 32 bits, we simply check that the input strings are under 4GB.
author Matt Mackall <mpm@selenic.com>
date Sat, 02 Feb 2013 16:15:22 -0600
parents 5b1f869b5548
children 66b21ce60a19
comparison
equal deleted inserted replaced
18550:db3a3a65e0db 18551:d6fb7bbec16a
345 PyThreadState *_save; 345 PyThreadState *_save;
346 346
347 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) 347 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
348 return NULL; 348 return NULL;
349 349
350 if (la > UINT_MAX || lb > UINT_MAX) {
351 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
352 return NULL;
353 }
354
350 _save = PyEval_SaveThread(); 355 _save = PyEval_SaveThread();
351 an = splitlines(sa, la, &al); 356 an = splitlines(sa, la, &al);
352 bn = splitlines(sb, lb, &bl); 357 bn = splitlines(sb, lb, &bl);
353 if (!al || !bl) 358 if (!al || !bl)
354 goto nomem; 359 goto nomem;
379 la = lb = 0; 384 la = lb = 0;
380 385
381 for (h = l.next; h; h = h->next) { 386 for (h = l.next; h; h = h->next) {
382 if (h->a1 != la || h->b1 != lb) { 387 if (h->a1 != la || h->b1 != lb) {
383 len = bl[h->b1].l - bl[lb].l; 388 len = bl[h->b1].l - bl[lb].l;
384 389 putbe32((uint32_t)(al[la].l - al->l), rb);
385 #define checkputbe32(__x, __c) \ 390 putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
386 if (__x > UINT_MAX) { \ 391 putbe32((uint32_t)len, rb + 8);
387 PyErr_SetString(PyExc_ValueError, \
388 "bdiff: value too large for putbe32"); \
389 goto nomem; \
390 } \
391 putbe32((uint32_t)(__x), __c);
392
393 checkputbe32(al[la].l - al->l, rb);
394 checkputbe32(al[h->a1].l - al->l, rb + 4);
395 checkputbe32(len, rb + 8);
396 memcpy(rb + 12, bl[lb].l, len); 392 memcpy(rb + 12, bl[lb].l, len);
397 rb += 12 + len; 393 rb += 12 + len;
398 } 394 }
399 la = h->a2; 395 la = h->a2;
400 lb = h->b2; 396 lb = h->b2;