add NULL pre firewall to ZVALUE code

The z*.c functions that take pointers that cannot be NULL are checked
for NULL pointers at the beginning of the function.

While calc is not known to pass bogus NULL pointers to ZVALUE related
code, libcalc could be called by external code that might do so by
mistake.  If that happens, math_error() is called with the name of
the function and the name of the arg that was NULL.
This commit is contained in:
Landon Curt Noll
2023-08-23 15:46:46 -07:00
parent 0bb66cff74
commit 61206172f1
8 changed files with 549 additions and 27 deletions

108
zfunc.c
View File

@@ -194,6 +194,12 @@ zfact(ZVALUE z, ZVALUE *dest)
long mul; /* collected value to multiply by */ long mul; /* collected value to multiply by */
ZVALUE res, temp; ZVALUE res, temp;
/* firewall */
if (dest == NULL) {
math_error("%s: dest NULL", __func__);
not_reached();
}
if (zisneg(z)) { if (zisneg(z)) {
math_error("Negative argument for factorial"); math_error("Negative argument for factorial");
not_reached(); not_reached();
@@ -247,6 +253,12 @@ zperm(ZVALUE z1, ZVALUE z2, ZVALUE *res)
SFULL count; SFULL count;
ZVALUE cur, tmp, ans; ZVALUE cur, tmp, ans;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (zisneg(z1) || zisneg(z2)) { if (zisneg(z1) || zisneg(z2)) {
math_error("Negative argument for permutation"); math_error("Negative argument for permutation");
not_reached(); not_reached();
@@ -289,6 +301,12 @@ docomb(ZVALUE z1, ZVALUE z2, ZVALUE *res)
HALF dh[1]; HALF dh[1];
#endif #endif
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (zrel(z2, z1) > 0) if (zrel(z2, z1) > 0)
return 0; return 0;
zsub(z1, z2, &temp); zsub(z1, z2, &temp);
@@ -347,6 +365,12 @@ zcomb(ZVALUE z1, ZVALUE z2, ZVALUE *res)
ZVALUE z3, z4; ZVALUE z3, z4;
int r; int r;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (z2.sign || (!z1.sign && zrel(z2, z1) > 0)) if (z2.sign || (!z1.sign && zrel(z2, z1) > 0))
return 0; return 0;
if (zisone(z2)) if (zisone(z2))
@@ -466,6 +490,12 @@ zfib(ZVALUE z, ZVALUE *res)
ZVALUE t1, t2, t3; ZVALUE t1, t2, t3;
FULL i; FULL i;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (zge31b(z)) { if (zge31b(z)) {
math_error("Very large Fibonacci number"); math_error("Very large Fibonacci number");
not_reached(); not_reached();
@@ -530,6 +560,12 @@ zpowi(ZVALUE z1, ZVALUE z2, ZVALUE *res)
long twos; /* count of times 2 is in result */ long twos; /* count of times 2 is in result */
ZVALUE ans, temp; ZVALUE ans, temp;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
sign = (z1.sign && zisodd(z2)); sign = (z1.sign && zisodd(z2));
z1.sign = 0; z1.sign = 0;
z2.sign = 0; z2.sign = 0;
@@ -657,6 +693,12 @@ ztenpow(long power, ZVALUE *res)
ZVALUE ans; ZVALUE ans;
ZVALUE temp; ZVALUE temp;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (power <= 0) { if (power <= 0) {
*res = _one_; *res = _one_;
return; return;
@@ -696,6 +738,12 @@ zmodinv(ZVALUE u, ZVALUE v, ZVALUE *res)
FULL q1, q2, ui3, vi3, uh, vh, A, B, C, D, T; FULL q1, q2, ui3, vi3, uh, vh, A, B, C, D, T;
ZVALUE u2, u3, v2, v3, qz, tmp1, tmp2, tmp3; ZVALUE u2, u3, v2, v3, qz, tmp1, tmp2, tmp3;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
v.sign = 0; v.sign = 0;
if (zisneg(u) || (zrel(u, v) >= 0)) if (zisneg(u) || (zrel(u, v) >= 0))
zmod(u, v, &v3, 0); zmod(u, v, &v3, 0);
@@ -860,6 +908,12 @@ zgcd(ZVALUE z1, ZVALUE z2, ZVALUE *res)
ZVALUE gcd; ZVALUE gcd;
bool needw; bool needw;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (zisunit(z1) || zisunit(z2)) { if (zisunit(z1) || zisunit(z2)) {
*res = _one_; *res = _one_;
return; return;
@@ -1179,6 +1233,12 @@ zlcm(ZVALUE z1, ZVALUE z2, ZVALUE *res)
{ {
ZVALUE temp1, temp2; ZVALUE temp1, temp2;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
zgcd(z1, z2, &temp1); zgcd(z1, z2, &temp1);
zequo(z1, temp1, &temp2); zequo(z1, temp1, &temp2);
zfree(temp1); zfree(temp1);
@@ -1335,6 +1395,8 @@ zlog10(ZVALUE z, bool *was_10_power)
FLAG rel; /* relationship */ FLAG rel; /* relationship */
int i; int i;
/* NOTE: It is OK if was_10_power == NULL */
if (ziszero(z)) { if (ziszero(z)) {
math_error("Zero argument argument for zlog10"); math_error("Zero argument argument for zlog10");
not_reached(); not_reached();
@@ -1493,6 +1555,12 @@ zfacrem(ZVALUE z1, ZVALUE z2, ZVALUE *rem)
ZVALUE temp1, temp2, temp3; /* temporaries */ ZVALUE temp1, temp2, temp3; /* temporaries */
ZVALUE squares[32]; /* table of squares of factor */ ZVALUE squares[32]; /* table of squares of factor */
/* firewall */
if (rem == NULL) {
math_error("%s: rem NULL", __func__);
not_reached();
}
z1.sign = 0; z1.sign = 0;
z2.sign = 0; z2.sign = 0;
/* /*
@@ -1623,6 +1691,12 @@ zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
long count, onecount; long count, onecount;
long sh; long sh;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (ziszero(z1) || ziszero(z2)) { if (ziszero(z1) || ziszero(z2)) {
math_error("Zero argument in call to zgcdrem!!!"); math_error("Zero argument in call to zgcdrem!!!");
not_reached(); not_reached();
@@ -1753,6 +1827,12 @@ zsqrt(ZVALUE z, ZVALUE *dest, long rnd)
bool up, onebit; bool up, onebit;
ZVALUE sqrt; ZVALUE sqrt;
/* firewall */
if (dest == NULL) {
math_error("%s: dest NULL", __func__);
not_reached();
}
if (z.sign) { if (z.sign) {
math_error("Square root of negative number"); math_error("Square root of negative number");
not_reached(); not_reached();
@@ -2023,6 +2103,12 @@ zroot(ZVALUE z1, ZVALUE z2, ZVALUE *dest)
LEN highbit, k; LEN highbit, k;
SIUNION sival; SIUNION sival;
/* firewall */
if (dest == NULL) {
math_error("%s: dest NULL", __func__);
not_reached();
}
sign = z1.sign; sign = z1.sign;
if (sign && ziseven(z2)) { if (sign && ziseven(z2)) {
math_error("Even root of negative number"); math_error("Even root of negative number");
@@ -2150,25 +2236,3 @@ zissquare(ZVALUE z)
zfree(tmp); zfree(tmp);
return (n ? true : false); return (n ? true : false);
} }
#if 0 /* XXX - to be added later */
/*
* test if a number is a power of 2
*
* given:
* z value to check if it is a power of 2
* zlog2 set to log base 2 of z if z is a power of 2, 0 otherwise
*/
bool
zispowerof2(ZVALUE z, ZVALUE *zlog2)
{
/* zero and negative values are never powers of 2 */
if (ziszero(z) || zisneg(z)) {
return false;
}
/* XXX - add code here - XXX */
}
#endif /* XXX */

34
zio.c
View File

@@ -125,6 +125,12 @@ math_str(char *str)
char *cp; char *cp;
size_t len; size_t len;
/* firewall */
if (str == NULL) {
math_error("%s: str NULL", __func__);
not_reached();
}
if (!outputisstring) { if (!outputisstring) {
fputs(str, outfp); fputs(str, outfp);
return; return;
@@ -155,6 +161,12 @@ math_str(char *str)
void void
math_fill(char *str, long width) math_fill(char *str, long width)
{ {
/* firewall */
if (str == NULL) {
math_error("%s: str NULL", __func__);
not_reached();
}
if (width > 0) { if (width > 0) {
width -= (long)strlen(str); width -= (long)strlen(str);
while (width-- > 0) while (width-- > 0)
@@ -179,6 +191,12 @@ math_fmt(char *fmt, ...)
va_list ap; va_list ap;
char buf[BUFSIZ+1]; char buf[BUFSIZ+1];
/* firewall */
if (fmt == NULL) {
math_error("%s: fmt NULL", __func__);
not_reached();
}
va_start(ap, fmt); va_start(ap, fmt);
vsnprintf(buf, BUFSIZ, fmt, ap); vsnprintf(buf, BUFSIZ, fmt, ap);
va_end(ap); va_end(ap);
@@ -295,6 +313,12 @@ math_cleardiversions(void)
void void
math_setfp(FILE *newfp) math_setfp(FILE *newfp)
{ {
/* firewall */
if (newfp == NULL) {
math_error("%s: newfp NULL", __func__);
not_reached();
}
outfp = newfp; outfp = newfp;
outputisstring = (oldiostates && (newfp == stdout)); outputisstring = (oldiostates && (newfp == stdout));
} }
@@ -714,6 +738,16 @@ str2z(char *s, ZVALUE *res)
bool minus; bool minus;
long shift; long shift;
/* firewall */
if (s == NULL) {
math_error("%s: s NULL", __func__);
not_reached();
}
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
minus = false; minus = false;
shift = 0; shift = 0;
if (*s == '+') if (*s == '+')

181
zmath.c
View File

@@ -255,17 +255,23 @@ alloc(LEN len)
* is_const - determine if a HALF array is an pre-allocated array * is_const - determine if a HALF array is an pre-allocated array
* *
* given: * given:
* h pointer to the beginning of the HALF array * h pointer to the beginning of the HALF array
* *
* returns: * returns:
* true - h is found in the half_tbl array * true - h is found in the half_tbl array
* false - is is not found in the half_tbl array * false - is is not found in the half_tbl array
*/ */
int int
is_const(HALF* h) is_const(HALF *h)
{ {
HALF **h_p; /* half_tbl array pointer */ HALF **h_p; /* half_tbl array pointer */
/* firewall */
if (h == NULL) {
math_error("%s: h NULL", __func__);
not_reached();
}
/* search the half_tbl for h */ /* search the half_tbl for h */
for (h_p = &half_tbl[0]; *h_p != NULL; ++h_p) { for (h_p = &half_tbl[0]; *h_p != NULL; ++h_p) {
if (h == *h_p) { if (h == *h_p) {
@@ -282,6 +288,13 @@ is_const(HALF* h)
void void
freeh(HALF *h) freeh(HALF *h)
{ {
/* firewall */
if (h == NULL) {
math_error("%s: h NULL", __func__);
not_reached();
}
/* free h is not a constant */
if (!is_const(h)) { if (!is_const(h)) {
free(h); free(h);
++nfree; ++nfree;
@@ -306,6 +319,12 @@ itoz(long i, ZVALUE *res)
{ {
long diddle, len; long diddle, len;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
res->len = 1; res->len = 1;
res->sign = 0; res->sign = 0;
diddle = 0; diddle = 0;
@@ -360,6 +379,12 @@ utoz(FULL i, ZVALUE *res)
{ {
long len; long len;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
res->len = 1; res->len = 1;
res->sign = 0; res->sign = 0;
if (i == 0) { if (i == 0) {
@@ -387,6 +412,12 @@ stoz(SFULL i, ZVALUE *res)
{ {
long diddle, len; long diddle, len;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
res->len = 1; res->len = 1;
res->sign = 0; res->sign = 0;
diddle = 0; diddle = 0;
@@ -471,6 +502,13 @@ ztos(ZVALUE z)
void void
zcopy(ZVALUE z, ZVALUE *res) zcopy(ZVALUE z, ZVALUE *res)
{ {
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* make copy */
res->sign = z.sign; res->sign = z.sign;
res->len = z.len; res->len = z.len;
if (zisabsleone(z)) { /* zero or plus or minus one are easy */ if (zisabsleone(z)) { /* zero or plus or minus one are easy */
@@ -494,6 +532,12 @@ zadd(ZVALUE z1, ZVALUE z2, ZVALUE *res)
FULL carry; FULL carry;
SIUNION sival; SIUNION sival;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (z1.sign && !z2.sign) { if (z1.sign && !z2.sign) {
z1.sign = 0; z1.sign = 0;
zsub(z2, z1, res); zsub(z2, z1, res);
@@ -547,6 +591,12 @@ zsub(ZVALUE z1, ZVALUE z2, ZVALUE *res)
SIUNION sival; SIUNION sival;
ZVALUE dest; ZVALUE dest;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (z1.sign != z2.sign) { if (z1.sign != z2.sign) {
z2.sign = z1.sign; z2.sign = z1.sign;
zadd(z1, z2, res); zadd(z1, z2, res);
@@ -617,6 +667,12 @@ zmuli(ZVALUE z, long n, ZVALUE *res)
SIUNION sival; SIUNION sival;
ZVALUE dest; ZVALUE dest;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if ((n == 0) || ziszero(z)) { if ((n == 0) || ziszero(z)) {
*res = _zero_; *res = _zero_;
return; return;
@@ -695,6 +751,16 @@ zreduce(ZVALUE z1, ZVALUE z2, ZVALUE *z1res, ZVALUE *z2res)
{ {
ZVALUE tmp; ZVALUE tmp;
/* firewall */
if (z1res == NULL) {
math_error("%s: z1res NULL", __func__);
not_reached();
}
if (z2res == NULL) {
math_error("%s: z2res NULL", __func__);
not_reached();
}
if (zisabsleone(z1) || zisabsleone(z2)) if (zisabsleone(z1) || zisabsleone(z2))
tmp = _one_; tmp = _one_;
else else
@@ -727,6 +793,16 @@ zdiv(ZVALUE z1, ZVALUE z2, ZVALUE *quo, ZVALUE *rem, long rnd)
LEN m, n, len, i, p, j1, j2, k; LEN m, n, len, i, p, j1, j2, k;
long t, val; long t, val;
/* firewall */
if (quo == NULL) {
math_error("%s: quo NULL", __func__);
not_reached();
}
if (rem == NULL) {
math_error("%s: rem NULL", __func__);
not_reached();
}
if (ziszero(z2)) { if (ziszero(z2)) {
math_error("Division by zero in zdiv"); math_error("Division by zero in zdiv");
not_reached(); not_reached();
@@ -959,6 +1035,12 @@ zquo(ZVALUE z1, ZVALUE z2, ZVALUE *res, long rnd)
ZVALUE tmp; ZVALUE tmp;
long val; long val;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
val = zdiv(z1, z2, res, &tmp, rnd); val = zdiv(z1, z2, res, &tmp, rnd);
if (z2.sign) if (z2.sign)
val = -val; val = -val;
@@ -978,6 +1060,12 @@ zmod(ZVALUE z1, ZVALUE z2, ZVALUE *res, long rnd)
ZVALUE tmp; ZVALUE tmp;
long val; long val;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
val = zdiv(z1, z2, &tmp, res, rnd); val = zdiv(z1, z2, &tmp, res, rnd);
zfree(tmp); zfree(tmp);
return val; return val;
@@ -996,6 +1084,12 @@ zequo(ZVALUE z1, ZVALUE z2, ZVALUE *res)
HALF *a, *a0, *A, *b, *B, u, v, w, x; HALF *a, *a0, *A, *b, *B, u, v, w, x;
FULL f, g; FULL f, g;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (ziszero(z1)) { if (ziszero(z1)) {
*res = _zero_; *res = _zero_;
return; return;
@@ -1126,6 +1220,12 @@ zdivi(ZVALUE z, long n, ZVALUE *res)
ZVALUE dest; ZVALUE dest;
LEN len; LEN len;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (n == 0) { if (n == 0) {
math_error("Division by zero"); math_error("Division by zero");
not_reached(); not_reached();
@@ -1370,6 +1470,12 @@ zor(ZVALUE z1, ZVALUE z2, ZVALUE *res)
long len; long len;
ZVALUE bz, lz, dest; ZVALUE bz, lz, dest;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (z1.len >= z2.len) { if (z1.len >= z2.len) {
bz = z1; bz = z1;
lz = z2; lz = z2;
@@ -1400,6 +1506,12 @@ zand(ZVALUE z1, ZVALUE z2, ZVALUE *res)
LEN len; LEN len;
ZVALUE dest; ZVALUE dest;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
len = ((z1.len <= z2.len) ? z1.len : z2.len); len = ((z1.len <= z2.len) ? z1.len : z2.len);
h1 = &z1.v[len-1]; h1 = &z1.v[len-1];
h2 = &z2.v[len-1]; h2 = &z2.v[len-1];
@@ -1430,6 +1542,12 @@ zxor(ZVALUE z1, ZVALUE z2, ZVALUE *res)
LEN len, j, k; LEN len, j, k;
ZVALUE dest; ZVALUE dest;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
h1 = z1.v; h1 = z1.v;
h2 = z2.v; h2 = z2.v;
len = z1.len; len = z1.len;
@@ -1467,6 +1585,12 @@ zandnot(ZVALUE z1, ZVALUE z2, ZVALUE *res)
LEN len, j, k; LEN len, j, k;
ZVALUE dest; ZVALUE dest;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
len = z1.len; len = z1.len;
if (z2.len >= len) { if (z2.len >= len) {
while (len > 1 && (z1.v[len-1] & ~z2.v[len-1]) == 0) while (len > 1 && (z1.v[len-1] & ~z2.v[len-1]) == 0)
@@ -1502,6 +1626,12 @@ zshift(ZVALUE z, long n, ZVALUE *res)
ZVALUE ans; ZVALUE ans;
LEN hc; /* number of halfwords shift is by */ LEN hc; /* number of halfwords shift is by */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (ziszero(z)) { if (ziszero(z)) {
*res = _zero_; *res = _zero_;
return; return;
@@ -1705,6 +1835,12 @@ zbitvalue(long n, ZVALUE *res)
{ {
ZVALUE z; ZVALUE z;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (n < 0) n = 0; if (n < 0) n = 0;
z.sign = 0; z.sign = 0;
z.len = (LEN)((n / BASEB) + 1); z.len = (LEN)((n / BASEB) + 1);
@@ -1866,6 +2002,12 @@ ztrim(ZVALUE *z)
HALF *h; HALF *h;
LEN len; LEN len;
/* firewall */
if (z == NULL) {
math_error("%s: z NULL", __func__);
not_reached();
}
h = z->v + z->len - 1; h = z->v + z->len - 1;
len = z->len; len = z->len;
while (*h == 0 && len > 1) { while (*h == 0 && len > 1) {
@@ -2021,3 +2163,34 @@ zpopcnt(ZVALUE z, int bitval)
*/ */
return cnt; return cnt;
} }
#if 0 /* XXX - to be added later */
/*
* test if a number is a power of 2
*
* given:
* z value to check if it is a power of 2
* zlog2 set to log base 2 of z if z is a power of 2, 0 otherwise
*
* returns:
* true z is a power of 2
* false z is not a power of 2
*/
bool
zispowerof2(ZVALUE z, ZVALUE *zlog2)
{
/* firewall */
if (zlog2 == NULL) {
math_error("%s: zlog2 NULL", __func__);
not_reached();
}
/* zero and negative values are never powers of 2 */
if (ziszero(z) || zisneg(z)) {
return false;
}
/* XXX - add code here - XXX */
}
#endif /* XXX */

98
zmod.c
View File

@@ -76,6 +76,12 @@ zsquaremod(ZVALUE z1, ZVALUE z2, ZVALUE *res)
FULL prod; FULL prod;
FULL digit; FULL digit;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (ziszero(z2) || zisneg(z2)) { if (ziszero(z2) || zisneg(z2)) {
math_error("Mod of non-positive integer"); math_error("Mod of non-positive integer");
not_reached(); not_reached();
@@ -138,6 +144,12 @@ zminmod(ZVALUE z1, ZVALUE z2, ZVALUE *res)
int sign; int sign;
int cv; int cv;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (ziszero(z2) || zisneg(z2)) { if (ziszero(z2) || zisneg(z2)) {
math_error("Mod of non-positive integer"); math_error("Mod of non-positive integer");
not_reached(); not_reached();
@@ -353,6 +365,12 @@ zmod5(ZVALUE *zp)
FULL f; FULL f;
HALF u; HALF u;
/* firewall */
if (zp == NULL) {
math_error("%s: zp NULL", __func__);
not_reached();
}
int subcount = 0; int subcount = 0;
if (zrel(*zp, *lastmod) < 0) if (zrel(*zp, *lastmod) < 0)
@@ -431,6 +449,12 @@ zmod6(ZVALUE z1, ZVALUE *res)
int sign; int sign;
ZVALUE zp0, ztmp; ZVALUE zp0, ztmp;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (ziszero(z1) || zisone(*lastmod)) { if (ziszero(z1) || zisone(*lastmod)) {
*res = _zero_; *res = _zero_;
return; return;
@@ -497,6 +521,12 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
bool free_z1; /* true => need to free z1 */ bool free_z1; /* true => need to free z1 */
int i; int i;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (zisneg(z3) || ziszero(z3)) { if (zisneg(z3) || ziszero(z3)) {
math_error("Non-positive modulus in zpowermod"); math_error("Non-positive modulus in zpowermod");
not_reached(); not_reached();
@@ -824,6 +854,12 @@ zredcmodinv(ZVALUE z, ZVALUE *res)
FULL f; FULL f;
LEN N, i, j, len; LEN N, i, j, len;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
N = z.len; N = z.len;
tmp.sign = 0; tmp.sign = 0;
tmp.len = N; tmp.len = N;
@@ -923,6 +959,12 @@ zredcalloc(ZVALUE z1)
void void
zredcfree(REDC *rp) zredcfree(REDC *rp)
{ {
/* firewall */
if (rp == NULL) {
math_error("%s: rp NULL", __func__);
not_reached();
}
zfree(rp->mod); zfree(rp->mod);
zfree(rp->inv); zfree(rp->inv);
zfree(rp->one); zfree(rp->one);
@@ -949,6 +991,16 @@ zredcencode(REDC *rp, ZVALUE z1, ZVALUE *res)
{ {
ZVALUE tmp1; ZVALUE tmp1;
/* firewall */
if (rp == NULL) {
math_error("%s: rp NULL", __func__);
not_reached();
}
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* Confirm or initialize lastmod information when modulus is a * Confirm or initialize lastmod information when modulus is a
* big number. * big number.
@@ -1037,6 +1089,16 @@ zredcdecode(REDC *rp, ZVALUE z1, ZVALUE *res)
LEN len; LEN len;
FULL f; FULL f;
int sign; int sign;
/* firewall */
if (rp == NULL) {
math_error("%s: rp NULL", __func__);
not_reached();
}
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
int i, j; int i, j;
/* /*
@@ -1213,6 +1275,16 @@ zredcmul(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
ZVALUE z1tmp, z2tmp; ZVALUE z1tmp, z2tmp;
int sign; int sign;
/* firewall */
if (rp == NULL) {
math_error("%s: rp NULL", __func__);
not_reached();
}
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
sign = z1.sign ^ z2.sign; sign = z1.sign ^ z2.sign;
z1.sign = 0; z1.sign = 0;
z2.sign = 0; z2.sign = 0;
@@ -1511,6 +1583,16 @@ zredcsquare(REDC *rp, ZVALUE z1, ZVALUE *res)
FULL f; FULL f;
int i, j; int i, j;
/* firewall */
if (rp == NULL) {
math_error("%s: rp NULL", __func__);
not_reached();
}
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
ztmp.len = 0; ztmp.len = 0;
z1.sign = 0; z1.sign = 0;
if (zrel(z1, rp->mod) >= 0) { if (zrel(z1, rp->mod) >= 0) {
@@ -1707,6 +1789,16 @@ zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
int sign; int sign;
int i; int i;
/* firewall */
if (rp == NULL) {
math_error("%s: rp NULL", __func__);
not_reached();
}
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (zisneg(z2)) { if (zisneg(z2)) {
math_error("Negative power in zredcpower"); math_error("Negative power in zredcpower");
not_reached(); not_reached();
@@ -1987,6 +2079,12 @@ zhnrmod(ZVALUE v, ZVALUE zh, ZVALUE zn, ZVALUE zr, ZVALUE *res)
int zrelval; /* return value of zrel() */ int zrelval; /* return value of zrel() */
int hisone; /* 1 => h == 1, 0 => h != 1 */ int hisone; /* 1 => h == 1, 0 => h != 1 */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* firewall * firewall
*/ */

26
zmul.c
View File

@@ -148,6 +148,16 @@ domul(HALF *v1, LEN size1, HALF *v2, LEN size2, HALF *ans)
register HALF *hd, *h1=NULL, *h2=NULL; /* for inner loops */ register HALF *hd, *h1=NULL, *h2=NULL; /* for inner loops */
SIUNION sival; /* for addition of digits */ SIUNION sival; /* for addition of digits */
/* firewall */
if (v1 == NULL) {
math_error("%s: v1 NULL", __func__);
not_reached();
}
if (ans == NULL) {
math_error("%s: ans NULL", __func__);
not_reached();
}
/* /*
* Trim the numbers of leading zeroes and initialize the * Trim the numbers of leading zeroes and initialize the
* estimated size of the result. * estimated size of the result.
@@ -651,6 +661,12 @@ zsquare(ZVALUE z, ZVALUE *res)
{ {
LEN len; LEN len;
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
if (ziszero(z)) { if (ziszero(z)) {
*res = _zero_; *res = _zero_;
return; return;
@@ -735,6 +751,16 @@ dosquare(HALF *vp, LEN size, HALF *ans)
register HALF *hd, *h1, *h2, *h3; /* for inner loops */ register HALF *hd, *h1, *h2, *h3; /* for inner loops */
SIUNION sival; /* for addition of digits */ SIUNION sival; /* for addition of digits */
/* firewall */
if (vp == NULL) {
math_error("%s: vp NULL", __func__);
not_reached();
}
if (ans == NULL) {
math_error("%s: ans NULL", __func__);
not_reached();
}
/* /*
* First trim the number of leading zeroes. * First trim the number of leading zeroes.
*/ */

View File

@@ -547,6 +547,12 @@ zfactor(ZVALUE n, ZVALUE zlimit, ZVALUE *res)
{ {
FULL f; /* factor found, or 0 */ FULL f; /* factor found, or 0 */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* determine the limit * determine the limit
*/ */
@@ -828,6 +834,12 @@ zpfact(ZVALUE z, ZVALUE *dest)
CONST unsigned char *j; /* current jump increment */ CONST unsigned char *j; /* current jump increment */
ZVALUE res, temp; ZVALUE res, temp;
/* firewall */
if (dest == NULL) {
math_error("%s: dest NULL", __func__);
not_reached();
}
/* firewall */ /* firewall */
if (zisneg(z)) { if (zisneg(z)) {
math_error("Negative argument for factorial"); math_error("Negative argument for factorial");
@@ -1212,6 +1224,12 @@ znextcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
ZVALUE tmp1; ZVALUE tmp1;
ZVALUE tmp2; ZVALUE tmp2;
/* firewall */
if (cand == NULL) {
math_error("%s: cand NULL", __func__);
not_reached();
}
z.sign = 0; z.sign = 0;
mod.sign = 0; mod.sign = 0;
if (ziszero(mod)) { if (ziszero(mod)) {
@@ -1292,6 +1310,12 @@ zprevcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
ZVALUE tmp1; ZVALUE tmp1;
ZVALUE tmp2; ZVALUE tmp2;
/* firewall */
if (cand == NULL) {
math_error("%s: cand NULL", __func__);
not_reached();
}
z.sign = 0; z.sign = 0;
mod.sign = 0; mod.sign = 0;
if (ziszero(mod)) { if (ziszero(mod)) {
@@ -1533,6 +1557,12 @@ zlcmfact(ZVALUE z, ZVALUE *dest)
CONST unsigned short *pr; /* pointer to a small prime */ CONST unsigned short *pr; /* pointer to a small prime */
ZVALUE res, temp; ZVALUE res, temp;
/* firewall */
if (dest == NULL) {
math_error("%s: dest NULL", __func__);
not_reached();
}
if (zisneg(z) || ziszero(z)) { if (zisneg(z) || ziszero(z)) {
math_error("Non-positive argument for lcmfact"); math_error("Non-positive argument for lcmfact");
not_reached(); not_reached();

62
zrand.c
View File

@@ -284,7 +284,7 @@
* of the digitization of chaotic system that consisted of a noisy * of the digitization of chaotic system that consisted of a noisy
* digital camera and 6 Lava Lite(R) lamps. * digital camera and 6 Lava Lite(R) lamps.
* *
* BTW: Lava Lite(R) is a trademark of Haggerty Enterprises, Inc. * BTW: Lava Lite(R) is a trademark of Haggerty Enterprises, Inc.
* *
* The first 100 groups of 64 bit bits were used to initialize init_s100.slot. * The first 100 groups of 64 bit bits were used to initialize init_s100.slot.
* *
@@ -1044,6 +1044,12 @@ randreseed64(ZVALUE seed, ZVALUE *res)
HALF *v64; /* 64 bit array of HALFs */ HALF *v64; /* 64 bit array of HALFs */
long chunknum; /* 64 bit chunk number */ long chunknum; /* 64 bit chunk number */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* quickly return 0 if seed is 0 * quickly return 0 if seed is 0
*/ */
@@ -1172,6 +1178,9 @@ zsrand(CONST ZVALUE *pseed, CONST MATRIX *pmat100)
long indx; /* index to shuffle slots for seeding */ long indx; /* index to shuffle slots for seeding */
int i; int i;
/* NOTE: It is OK for pseed == NULL */
/* NOTE: It is OK for pmat100 == NULL */
/* /*
* firewall * firewall
*/ */
@@ -1394,6 +1403,12 @@ zsetrand(CONST RAND *state)
{ {
RAND *ret; /* previous s100 state */ RAND *ret; /* previous s100 state */
/* firewall */
if (state == NULL) {
math_error("%s: state NULL", __func__);
not_reached();
}
/* /*
* initialize state if first call * initialize state if first call
*/ */
@@ -1449,6 +1464,12 @@ slotcp(BITSTR *bitstr, FULL *src, int count)
int need; /* number of bits we need to transfer */ int need; /* number of bits we need to transfer */
int ret; /* bits transferred */ int ret; /* bits transferred */
/* firewall */
if (src == NULL) {
math_error("%s: src NULL", __func__);
not_reached();
}
/* /*
* determine how many bits we actually need to transfer * determine how many bits we actually need to transfer
*/ */
@@ -1622,6 +1643,16 @@ slotcp64(BITSTR *bitstr, FULL *src)
HALF *dh = bitstr->loc; /* most significant HALF in dest */ HALF *dh = bitstr->loc; /* most significant HALF in dest */
int dnxtbit = bitstr->bit+1; /* next dh bit beyond most significant */ int dnxtbit = bitstr->bit+1; /* next dh bit beyond most significant */
/* firewall */
if (bitstr == NULL) {
math_error("%s: bitstr NULL", __func__);
not_reached();
}
if (src == NULL) {
math_error("%s: src NULL", __func__);
not_reached();
}
/* /*
* prepare for the return * prepare for the return
* *
@@ -1918,6 +1949,12 @@ zrand(long cnt, ZVALUE *res)
int trans; /* bits transferred */ int trans; /* bits transferred */
int indx; /* shuffle entry index */ int indx; /* shuffle entry index */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* firewall * firewall
*/ */
@@ -2187,6 +2224,12 @@ zrandrange(CONST ZVALUE low, CONST ZVALUE beyond, ZVALUE *res)
ZVALUE rangem1; /* range - 1 */ ZVALUE rangem1; /* range - 1 */
long bitlen; /* smallest power of 2 >= diff */ long bitlen; /* smallest power of 2 >= diff */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* firewall * firewall
*/ */
@@ -2279,6 +2322,12 @@ randcopy(CONST RAND *state)
{ {
RAND *ret; /* return copy of state */ RAND *ret; /* return copy of state */
/* firewall */
if (state == NULL) {
math_error("%s: state NULL", __func__);
not_reached();
}
/* /*
* malloc state * malloc state
*/ */
@@ -2323,6 +2372,16 @@ randfree(RAND *state)
bool bool
randcmp(CONST RAND *s1, CONST RAND *s2) randcmp(CONST RAND *s1, CONST RAND *s2)
{ {
/* firewall */
if (s1 == NULL) {
math_error("%s: s1 NULL", __func__);
not_reached();
}
if (s2 == NULL) {
math_error("%s: s2 NULL", __func__);
not_reached();
}
/* /*
* assume uninitialized state == the default seeded state * assume uninitialized state == the default seeded state
*/ */
@@ -2355,5 +2414,6 @@ randcmp(CONST RAND *s1, CONST RAND *s2)
void void
randprint(CONST RAND *UNUSED(state), int UNUSED(flags)) randprint(CONST RAND *UNUSED(state), int UNUSED(flags))
{ {
/* NOTE: It is OK for state == NULL */
math_str("RAND state"); math_str("RAND state");
} }

View File

@@ -2635,6 +2635,8 @@ zsetrandom(CONST RANDOM *state)
RANDOM *ret; /* previous Blum state */ RANDOM *ret; /* previous Blum state */
RANDOM *p_blum; /* malloced RANDOM by randomcopy() */ RANDOM *p_blum; /* malloced RANDOM by randomcopy() */
/* NOTE: It is OK for state == NULL */
/* /*
* initialize state if first call * initialize state if first call
*/ */
@@ -2768,6 +2770,12 @@ zrandom(long cnt, ZVALUE *res)
RANDOM *p_blum; /* malloced RANDOM by randomcopy() */ RANDOM *p_blum; /* malloced RANDOM by randomcopy() */
int t; /* temp shift value */ int t; /* temp shift value */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* firewall * firewall
*/ */
@@ -2951,6 +2959,12 @@ zrandomrange(CONST ZVALUE low, CONST ZVALUE beyond, ZVALUE *res)
ZVALUE rangem1; /* range - 1 */ ZVALUE rangem1; /* range - 1 */
long bitlen; /* smallest power of 2 >= diff */ long bitlen; /* smallest power of 2 >= diff */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/* /*
* firewall * firewall
*/ */
@@ -3043,6 +3057,12 @@ randomcopy(CONST RANDOM *state)
{ {
RANDOM *ret; /* return copy of state */ RANDOM *ret; /* return copy of state */
/* firewall */
if (state == NULL) {
math_error("%s: state NULL", __func__);
not_reached();
}
/* /*
* malloc state * malloc state
*/ */
@@ -3094,6 +3114,12 @@ randomcopy(CONST RANDOM *state)
void void
randomfree(RANDOM *state) randomfree(RANDOM *state)
{ {
/* firewall */
if (state == NULL) {
math_error("%s: state NULL", __func__);
not_reached();
}
/* free the values */ /* free the values */
zfree_random(state->n); zfree_random(state->n);
zfree_random(state->r); zfree_random(state->r);
@@ -3123,6 +3149,16 @@ randomfree(RANDOM *state)
bool bool
randomcmp(CONST RANDOM *s1, CONST RANDOM *s2) randomcmp(CONST RANDOM *s1, CONST RANDOM *s2)
{ {
/* firewall */
if (s1 == NULL) {
math_error("%s: s1 NULL", __func__);
not_reached();
}
if (s2 == NULL) {
math_error("%s: s2 NULL", __func__);
not_reached();
}
/* /*
* assume uninitialized state == the default seeded state * assume uninitialized state == the default seeded state
*/ */
@@ -3171,6 +3207,7 @@ randomcmp(CONST RANDOM *s1, CONST RANDOM *s2)
void void
randomprint(CONST RANDOM *UNUSED(state), int UNUSED(flags)) randomprint(CONST RANDOM *UNUSED(state), int UNUSED(flags))
{ {
/* NOTE: It is OK for state == NULL */
math_str("RANDOM state"); math_str("RANDOM state");
} }