Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e89fff5

Browse files
committed
Add some asserts
1 parent a35392b commit e89fff5

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

zz.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ zz_resize(size_t size, zz_t *u)
149149
{
150150
if (u->alloc >= size) {
151151
u->size = (zz_size_t)size;
152+
if (!u->size) {
153+
u->negative = false;
154+
}
152155
return ZZ_OK;
153156
}
154157
if (size > INT32_MAX) {
@@ -854,6 +857,7 @@ _zz_addsub_i32(const zz_t *u, int32_t v, bool subtract, zz_t *w)
854857
zz_limb_t digit = ABS(v);
855858

856859
if (u_size < v_size) {
860+
assert(v_size == 1);
857861
if (zz_resize(v_size, w)) {
858862
return ZZ_MEM; /* LCOV_EXCL_LINE */
859863
}
@@ -953,6 +957,7 @@ zz_mul(const zz_t *u, const zz_t *v, zz_t *w)
953957
mpn_mul(w->digits, u->digits, u->size, v->digits, v->size);
954958
}
955959
w->size -= w->digits[w->size - 1] == 0;
960+
assert(w->size >= 1);
956961
return ZZ_OK;
957962
}
958963

@@ -1418,6 +1423,7 @@ zz_and(const zz_t *u, const zz_t *v, zz_t *w)
14181423
return ZZ_OK;
14191424
}
14201425
else if (u->negative) {
1426+
assert(v_size > 0);
14211427
if (zz_resize(v_size, w)) {
14221428
goto err; /* LCOV_EXCL_LINE */
14231429
}
@@ -1429,6 +1435,7 @@ zz_and(const zz_t *u, const zz_t *v, zz_t *w)
14291435
return ZZ_OK;
14301436
}
14311437
else {
1438+
assert(u_size > 0);
14321439
if (zz_resize(u_size, w)) {
14331440
goto err; /* LCOV_EXCL_LINE */
14341441
}
@@ -1517,6 +1524,7 @@ zz_or(const zz_t *u, const zz_t *v, zz_t *w)
15171524
return ZZ_OK;
15181525
}
15191526
else if (u->negative) {
1527+
assert(v_size > 0);
15201528
if (zz_resize(u_size + 1, w)) {
15211529
goto err; /* LCOV_EXCL_LINE */
15221530
}
@@ -1530,6 +1538,7 @@ zz_or(const zz_t *u, const zz_t *v, zz_t *w)
15301538
return ZZ_OK;
15311539
}
15321540
else {
1541+
assert(u_size > 0);
15331542
if (zz_resize(v_size + 1, w)) {
15341543
goto err; /* LCOV_EXCL_LINE */
15351544
}
@@ -1625,6 +1634,7 @@ zz_xor(const zz_t *u, const zz_t *v, zz_t *w)
16251634
return ZZ_OK;
16261635
}
16271636
else if (u->negative) {
1637+
assert(v_size > 0);
16281638
if (zz_resize(u_size + 1, w)) {
16291639
goto err; /* LCOV_EXCL_LINE */
16301640
}
@@ -1638,6 +1648,7 @@ zz_xor(const zz_t *u, const zz_t *v, zz_t *w)
16381648
return ZZ_OK;
16391649
}
16401650
else {
1651+
assert(u_size > 0);
16411652
if (zz_resize(u_size + 1, w)) {
16421653
goto err; /* LCOV_EXCL_LINE */
16431654
}

0 commit comments

Comments
 (0)