mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
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:
108
zfunc.c
108
zfunc.c
@@ -194,6 +194,12 @@ zfact(ZVALUE z, ZVALUE *dest)
|
||||
long mul; /* collected value to multiply by */
|
||||
ZVALUE res, temp;
|
||||
|
||||
/* firewall */
|
||||
if (dest == NULL) {
|
||||
math_error("%s: dest NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (zisneg(z)) {
|
||||
math_error("Negative argument for factorial");
|
||||
not_reached();
|
||||
@@ -247,6 +253,12 @@ zperm(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
SFULL count;
|
||||
ZVALUE cur, tmp, ans;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (zisneg(z1) || zisneg(z2)) {
|
||||
math_error("Negative argument for permutation");
|
||||
not_reached();
|
||||
@@ -289,6 +301,12 @@ docomb(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
HALF dh[1];
|
||||
#endif
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (zrel(z2, z1) > 0)
|
||||
return 0;
|
||||
zsub(z1, z2, &temp);
|
||||
@@ -347,6 +365,12 @@ zcomb(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
ZVALUE z3, z4;
|
||||
int r;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (z2.sign || (!z1.sign && zrel(z2, z1) > 0))
|
||||
return 0;
|
||||
if (zisone(z2))
|
||||
@@ -466,6 +490,12 @@ zfib(ZVALUE z, ZVALUE *res)
|
||||
ZVALUE t1, t2, t3;
|
||||
FULL i;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (zge31b(z)) {
|
||||
math_error("Very large Fibonacci number");
|
||||
not_reached();
|
||||
@@ -530,6 +560,12 @@ zpowi(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
long twos; /* count of times 2 is in result */
|
||||
ZVALUE ans, temp;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
sign = (z1.sign && zisodd(z2));
|
||||
z1.sign = 0;
|
||||
z2.sign = 0;
|
||||
@@ -657,6 +693,12 @@ ztenpow(long power, ZVALUE *res)
|
||||
ZVALUE ans;
|
||||
ZVALUE temp;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (power <= 0) {
|
||||
*res = _one_;
|
||||
return;
|
||||
@@ -696,6 +738,12 @@ zmodinv(ZVALUE u, ZVALUE v, ZVALUE *res)
|
||||
FULL q1, q2, ui3, vi3, uh, vh, A, B, C, D, T;
|
||||
ZVALUE u2, u3, v2, v3, qz, tmp1, tmp2, tmp3;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
v.sign = 0;
|
||||
if (zisneg(u) || (zrel(u, v) >= 0))
|
||||
zmod(u, v, &v3, 0);
|
||||
@@ -860,6 +908,12 @@ zgcd(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
ZVALUE gcd;
|
||||
bool needw;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (zisunit(z1) || zisunit(z2)) {
|
||||
*res = _one_;
|
||||
return;
|
||||
@@ -1179,6 +1233,12 @@ zlcm(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
{
|
||||
ZVALUE temp1, temp2;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
zgcd(z1, z2, &temp1);
|
||||
zequo(z1, temp1, &temp2);
|
||||
zfree(temp1);
|
||||
@@ -1335,6 +1395,8 @@ zlog10(ZVALUE z, bool *was_10_power)
|
||||
FLAG rel; /* relationship */
|
||||
int i;
|
||||
|
||||
/* NOTE: It is OK if was_10_power == NULL */
|
||||
|
||||
if (ziszero(z)) {
|
||||
math_error("Zero argument argument for zlog10");
|
||||
not_reached();
|
||||
@@ -1493,6 +1555,12 @@ zfacrem(ZVALUE z1, ZVALUE z2, ZVALUE *rem)
|
||||
ZVALUE temp1, temp2, temp3; /* temporaries */
|
||||
ZVALUE squares[32]; /* table of squares of factor */
|
||||
|
||||
/* firewall */
|
||||
if (rem == NULL) {
|
||||
math_error("%s: rem NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
z1.sign = 0;
|
||||
z2.sign = 0;
|
||||
/*
|
||||
@@ -1623,6 +1691,12 @@ zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
long count, onecount;
|
||||
long sh;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (ziszero(z1) || ziszero(z2)) {
|
||||
math_error("Zero argument in call to zgcdrem!!!");
|
||||
not_reached();
|
||||
@@ -1753,6 +1827,12 @@ zsqrt(ZVALUE z, ZVALUE *dest, long rnd)
|
||||
bool up, onebit;
|
||||
ZVALUE sqrt;
|
||||
|
||||
/* firewall */
|
||||
if (dest == NULL) {
|
||||
math_error("%s: dest NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (z.sign) {
|
||||
math_error("Square root of negative number");
|
||||
not_reached();
|
||||
@@ -2023,6 +2103,12 @@ zroot(ZVALUE z1, ZVALUE z2, ZVALUE *dest)
|
||||
LEN highbit, k;
|
||||
SIUNION sival;
|
||||
|
||||
/* firewall */
|
||||
if (dest == NULL) {
|
||||
math_error("%s: dest NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
sign = z1.sign;
|
||||
if (sign && ziseven(z2)) {
|
||||
math_error("Even root of negative number");
|
||||
@@ -2150,25 +2236,3 @@ zissquare(ZVALUE z)
|
||||
zfree(tmp);
|
||||
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
34
zio.c
@@ -125,6 +125,12 @@ math_str(char *str)
|
||||
char *cp;
|
||||
size_t len;
|
||||
|
||||
/* firewall */
|
||||
if (str == NULL) {
|
||||
math_error("%s: str NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (!outputisstring) {
|
||||
fputs(str, outfp);
|
||||
return;
|
||||
@@ -155,6 +161,12 @@ math_str(char *str)
|
||||
void
|
||||
math_fill(char *str, long width)
|
||||
{
|
||||
/* firewall */
|
||||
if (str == NULL) {
|
||||
math_error("%s: str NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (width > 0) {
|
||||
width -= (long)strlen(str);
|
||||
while (width-- > 0)
|
||||
@@ -179,6 +191,12 @@ math_fmt(char *fmt, ...)
|
||||
va_list ap;
|
||||
char buf[BUFSIZ+1];
|
||||
|
||||
/* firewall */
|
||||
if (fmt == NULL) {
|
||||
math_error("%s: fmt NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, BUFSIZ, fmt, ap);
|
||||
va_end(ap);
|
||||
@@ -295,6 +313,12 @@ math_cleardiversions(void)
|
||||
void
|
||||
math_setfp(FILE *newfp)
|
||||
{
|
||||
/* firewall */
|
||||
if (newfp == NULL) {
|
||||
math_error("%s: newfp NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
outfp = newfp;
|
||||
outputisstring = (oldiostates && (newfp == stdout));
|
||||
}
|
||||
@@ -714,6 +738,16 @@ str2z(char *s, ZVALUE *res)
|
||||
bool minus;
|
||||
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;
|
||||
shift = 0;
|
||||
if (*s == '+')
|
||||
|
181
zmath.c
181
zmath.c
@@ -255,17 +255,23 @@ alloc(LEN len)
|
||||
* is_const - determine if a HALF array is an pre-allocated array
|
||||
*
|
||||
* given:
|
||||
* h pointer to the beginning of the HALF array
|
||||
* h pointer to the beginning of the HALF array
|
||||
*
|
||||
* returns:
|
||||
* true - h is found in the half_tbl array
|
||||
* false - is is not 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
|
||||
*/
|
||||
int
|
||||
is_const(HALF* h)
|
||||
is_const(HALF *h)
|
||||
{
|
||||
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 */
|
||||
for (h_p = &half_tbl[0]; *h_p != NULL; ++h_p) {
|
||||
if (h == *h_p) {
|
||||
@@ -282,6 +288,13 @@ is_const(HALF* h)
|
||||
void
|
||||
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)) {
|
||||
free(h);
|
||||
++nfree;
|
||||
@@ -306,6 +319,12 @@ itoz(long i, ZVALUE *res)
|
||||
{
|
||||
long diddle, len;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
res->len = 1;
|
||||
res->sign = 0;
|
||||
diddle = 0;
|
||||
@@ -360,6 +379,12 @@ utoz(FULL i, ZVALUE *res)
|
||||
{
|
||||
long len;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
res->len = 1;
|
||||
res->sign = 0;
|
||||
if (i == 0) {
|
||||
@@ -387,6 +412,12 @@ stoz(SFULL i, ZVALUE *res)
|
||||
{
|
||||
long diddle, len;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
res->len = 1;
|
||||
res->sign = 0;
|
||||
diddle = 0;
|
||||
@@ -471,6 +502,13 @@ ztos(ZVALUE z)
|
||||
void
|
||||
zcopy(ZVALUE z, ZVALUE *res)
|
||||
{
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/* make copy */
|
||||
res->sign = z.sign;
|
||||
res->len = z.len;
|
||||
if (zisabsleone(z)) { /* zero or plus or minus one are easy */
|
||||
@@ -494,6 +532,12 @@ zadd(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
FULL carry;
|
||||
SIUNION sival;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (z1.sign && !z2.sign) {
|
||||
z1.sign = 0;
|
||||
zsub(z2, z1, res);
|
||||
@@ -547,6 +591,12 @@ zsub(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
SIUNION sival;
|
||||
ZVALUE dest;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (z1.sign != z2.sign) {
|
||||
z2.sign = z1.sign;
|
||||
zadd(z1, z2, res);
|
||||
@@ -617,6 +667,12 @@ zmuli(ZVALUE z, long n, ZVALUE *res)
|
||||
SIUNION sival;
|
||||
ZVALUE dest;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if ((n == 0) || ziszero(z)) {
|
||||
*res = _zero_;
|
||||
return;
|
||||
@@ -695,6 +751,16 @@ zreduce(ZVALUE z1, ZVALUE z2, ZVALUE *z1res, ZVALUE *z2res)
|
||||
{
|
||||
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))
|
||||
tmp = _one_;
|
||||
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;
|
||||
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)) {
|
||||
math_error("Division by zero in zdiv");
|
||||
not_reached();
|
||||
@@ -959,6 +1035,12 @@ zquo(ZVALUE z1, ZVALUE z2, ZVALUE *res, long rnd)
|
||||
ZVALUE tmp;
|
||||
long val;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
val = zdiv(z1, z2, res, &tmp, rnd);
|
||||
if (z2.sign)
|
||||
val = -val;
|
||||
@@ -978,6 +1060,12 @@ zmod(ZVALUE z1, ZVALUE z2, ZVALUE *res, long rnd)
|
||||
ZVALUE tmp;
|
||||
long val;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
val = zdiv(z1, z2, &tmp, res, rnd);
|
||||
zfree(tmp);
|
||||
return val;
|
||||
@@ -996,6 +1084,12 @@ zequo(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
HALF *a, *a0, *A, *b, *B, u, v, w, x;
|
||||
FULL f, g;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (ziszero(z1)) {
|
||||
*res = _zero_;
|
||||
return;
|
||||
@@ -1126,6 +1220,12 @@ zdivi(ZVALUE z, long n, ZVALUE *res)
|
||||
ZVALUE dest;
|
||||
LEN len;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (n == 0) {
|
||||
math_error("Division by zero");
|
||||
not_reached();
|
||||
@@ -1370,6 +1470,12 @@ zor(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
long len;
|
||||
ZVALUE bz, lz, dest;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (z1.len >= z2.len) {
|
||||
bz = z1;
|
||||
lz = z2;
|
||||
@@ -1400,6 +1506,12 @@ zand(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
LEN len;
|
||||
ZVALUE dest;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
len = ((z1.len <= z2.len) ? z1.len : z2.len);
|
||||
h1 = &z1.v[len-1];
|
||||
h2 = &z2.v[len-1];
|
||||
@@ -1430,6 +1542,12 @@ zxor(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
LEN len, j, k;
|
||||
ZVALUE dest;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
h1 = z1.v;
|
||||
h2 = z2.v;
|
||||
len = z1.len;
|
||||
@@ -1467,6 +1585,12 @@ zandnot(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
LEN len, j, k;
|
||||
ZVALUE dest;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
len = z1.len;
|
||||
if (z2.len >= len) {
|
||||
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;
|
||||
LEN hc; /* number of halfwords shift is by */
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (ziszero(z)) {
|
||||
*res = _zero_;
|
||||
return;
|
||||
@@ -1705,6 +1835,12 @@ zbitvalue(long n, ZVALUE *res)
|
||||
{
|
||||
ZVALUE z;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (n < 0) n = 0;
|
||||
z.sign = 0;
|
||||
z.len = (LEN)((n / BASEB) + 1);
|
||||
@@ -1866,6 +2002,12 @@ ztrim(ZVALUE *z)
|
||||
HALF *h;
|
||||
LEN len;
|
||||
|
||||
/* firewall */
|
||||
if (z == NULL) {
|
||||
math_error("%s: z NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
h = z->v + z->len - 1;
|
||||
len = z->len;
|
||||
while (*h == 0 && len > 1) {
|
||||
@@ -2021,3 +2163,34 @@ zpopcnt(ZVALUE z, int bitval)
|
||||
*/
|
||||
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
98
zmod.c
@@ -76,6 +76,12 @@ zsquaremod(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
FULL prod;
|
||||
FULL digit;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (ziszero(z2) || zisneg(z2)) {
|
||||
math_error("Mod of non-positive integer");
|
||||
not_reached();
|
||||
@@ -138,6 +144,12 @@ zminmod(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
int sign;
|
||||
int cv;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (ziszero(z2) || zisneg(z2)) {
|
||||
math_error("Mod of non-positive integer");
|
||||
not_reached();
|
||||
@@ -353,6 +365,12 @@ zmod5(ZVALUE *zp)
|
||||
FULL f;
|
||||
HALF u;
|
||||
|
||||
/* firewall */
|
||||
if (zp == NULL) {
|
||||
math_error("%s: zp NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
int subcount = 0;
|
||||
|
||||
if (zrel(*zp, *lastmod) < 0)
|
||||
@@ -431,6 +449,12 @@ zmod6(ZVALUE z1, ZVALUE *res)
|
||||
int sign;
|
||||
ZVALUE zp0, ztmp;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (ziszero(z1) || zisone(*lastmod)) {
|
||||
*res = _zero_;
|
||||
return;
|
||||
@@ -497,6 +521,12 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
||||
bool free_z1; /* true => need to free z1 */
|
||||
int i;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (zisneg(z3) || ziszero(z3)) {
|
||||
math_error("Non-positive modulus in zpowermod");
|
||||
not_reached();
|
||||
@@ -824,6 +854,12 @@ zredcmodinv(ZVALUE z, ZVALUE *res)
|
||||
FULL f;
|
||||
LEN N, i, j, len;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
N = z.len;
|
||||
tmp.sign = 0;
|
||||
tmp.len = N;
|
||||
@@ -923,6 +959,12 @@ zredcalloc(ZVALUE z1)
|
||||
void
|
||||
zredcfree(REDC *rp)
|
||||
{
|
||||
/* firewall */
|
||||
if (rp == NULL) {
|
||||
math_error("%s: rp NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
zfree(rp->mod);
|
||||
zfree(rp->inv);
|
||||
zfree(rp->one);
|
||||
@@ -949,6 +991,16 @@ zredcencode(REDC *rp, ZVALUE z1, ZVALUE *res)
|
||||
{
|
||||
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
|
||||
* big number.
|
||||
@@ -1037,6 +1089,16 @@ zredcdecode(REDC *rp, ZVALUE z1, ZVALUE *res)
|
||||
LEN len;
|
||||
FULL f;
|
||||
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;
|
||||
|
||||
/*
|
||||
@@ -1213,6 +1275,16 @@ zredcmul(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
ZVALUE z1tmp, z2tmp;
|
||||
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;
|
||||
z1.sign = 0;
|
||||
z2.sign = 0;
|
||||
@@ -1511,6 +1583,16 @@ zredcsquare(REDC *rp, ZVALUE z1, ZVALUE *res)
|
||||
FULL f;
|
||||
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;
|
||||
z1.sign = 0;
|
||||
if (zrel(z1, rp->mod) >= 0) {
|
||||
@@ -1707,6 +1789,16 @@ zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||
int sign;
|
||||
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)) {
|
||||
math_error("Negative power in zredcpower");
|
||||
not_reached();
|
||||
@@ -1987,6 +2079,12 @@ zhnrmod(ZVALUE v, ZVALUE zh, ZVALUE zn, ZVALUE zr, ZVALUE *res)
|
||||
int zrelval; /* return value of zrel() */
|
||||
int hisone; /* 1 => h == 1, 0 => h != 1 */
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* firewall
|
||||
*/
|
||||
|
26
zmul.c
26
zmul.c
@@ -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 */
|
||||
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
|
||||
* estimated size of the result.
|
||||
@@ -651,6 +661,12 @@ zsquare(ZVALUE z, ZVALUE *res)
|
||||
{
|
||||
LEN len;
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (ziszero(z)) {
|
||||
*res = _zero_;
|
||||
return;
|
||||
@@ -735,6 +751,16 @@ dosquare(HALF *vp, LEN size, HALF *ans)
|
||||
register HALF *hd, *h1, *h2, *h3; /* for inner loops */
|
||||
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.
|
||||
*/
|
||||
|
30
zprime.c
30
zprime.c
@@ -547,6 +547,12 @@ zfactor(ZVALUE n, ZVALUE zlimit, ZVALUE *res)
|
||||
{
|
||||
FULL f; /* factor found, or 0 */
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* determine the limit
|
||||
*/
|
||||
@@ -828,6 +834,12 @@ zpfact(ZVALUE z, ZVALUE *dest)
|
||||
CONST unsigned char *j; /* current jump increment */
|
||||
ZVALUE res, temp;
|
||||
|
||||
/* firewall */
|
||||
if (dest == NULL) {
|
||||
math_error("%s: dest NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/* firewall */
|
||||
if (zisneg(z)) {
|
||||
math_error("Negative argument for factorial");
|
||||
@@ -1212,6 +1224,12 @@ znextcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
|
||||
ZVALUE tmp1;
|
||||
ZVALUE tmp2;
|
||||
|
||||
/* firewall */
|
||||
if (cand == NULL) {
|
||||
math_error("%s: cand NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
z.sign = 0;
|
||||
mod.sign = 0;
|
||||
if (ziszero(mod)) {
|
||||
@@ -1292,6 +1310,12 @@ zprevcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
|
||||
ZVALUE tmp1;
|
||||
ZVALUE tmp2;
|
||||
|
||||
/* firewall */
|
||||
if (cand == NULL) {
|
||||
math_error("%s: cand NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
z.sign = 0;
|
||||
mod.sign = 0;
|
||||
if (ziszero(mod)) {
|
||||
@@ -1533,6 +1557,12 @@ zlcmfact(ZVALUE z, ZVALUE *dest)
|
||||
CONST unsigned short *pr; /* pointer to a small prime */
|
||||
ZVALUE res, temp;
|
||||
|
||||
/* firewall */
|
||||
if (dest == NULL) {
|
||||
math_error("%s: dest NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
if (zisneg(z) || ziszero(z)) {
|
||||
math_error("Non-positive argument for lcmfact");
|
||||
not_reached();
|
||||
|
62
zrand.c
62
zrand.c
@@ -284,7 +284,7 @@
|
||||
* of the digitization of chaotic system that consisted of a noisy
|
||||
* 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.
|
||||
*
|
||||
@@ -1044,6 +1044,12 @@ randreseed64(ZVALUE seed, ZVALUE *res)
|
||||
HALF *v64; /* 64 bit array of HALFs */
|
||||
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
|
||||
*/
|
||||
@@ -1172,6 +1178,9 @@ zsrand(CONST ZVALUE *pseed, CONST MATRIX *pmat100)
|
||||
long indx; /* index to shuffle slots for seeding */
|
||||
int i;
|
||||
|
||||
/* NOTE: It is OK for pseed == NULL */
|
||||
/* NOTE: It is OK for pmat100 == NULL */
|
||||
|
||||
/*
|
||||
* firewall
|
||||
*/
|
||||
@@ -1394,6 +1403,12 @@ zsetrand(CONST RAND *state)
|
||||
{
|
||||
RAND *ret; /* previous s100 state */
|
||||
|
||||
/* firewall */
|
||||
if (state == NULL) {
|
||||
math_error("%s: state NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 ret; /* bits transferred */
|
||||
|
||||
/* firewall */
|
||||
if (src == NULL) {
|
||||
math_error("%s: src NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
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
|
||||
*
|
||||
@@ -1918,6 +1949,12 @@ zrand(long cnt, ZVALUE *res)
|
||||
int trans; /* bits transferred */
|
||||
int indx; /* shuffle entry index */
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* firewall
|
||||
*/
|
||||
@@ -2187,6 +2224,12 @@ zrandrange(CONST ZVALUE low, CONST ZVALUE beyond, ZVALUE *res)
|
||||
ZVALUE rangem1; /* range - 1 */
|
||||
long bitlen; /* smallest power of 2 >= diff */
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* firewall
|
||||
*/
|
||||
@@ -2279,6 +2322,12 @@ randcopy(CONST RAND *state)
|
||||
{
|
||||
RAND *ret; /* return copy of state */
|
||||
|
||||
/* firewall */
|
||||
if (state == NULL) {
|
||||
math_error("%s: state NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* malloc state
|
||||
*/
|
||||
@@ -2323,6 +2372,16 @@ randfree(RAND *state)
|
||||
bool
|
||||
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
|
||||
*/
|
||||
@@ -2355,5 +2414,6 @@ randcmp(CONST RAND *s1, CONST RAND *s2)
|
||||
void
|
||||
randprint(CONST RAND *UNUSED(state), int UNUSED(flags))
|
||||
{
|
||||
/* NOTE: It is OK for state == NULL */
|
||||
math_str("RAND state");
|
||||
}
|
||||
|
37
zrandom.c
37
zrandom.c
@@ -2635,6 +2635,8 @@ zsetrandom(CONST RANDOM *state)
|
||||
RANDOM *ret; /* previous Blum state */
|
||||
RANDOM *p_blum; /* malloced RANDOM by randomcopy() */
|
||||
|
||||
/* NOTE: It is OK for state == NULL */
|
||||
|
||||
/*
|
||||
* initialize state if first call
|
||||
*/
|
||||
@@ -2768,6 +2770,12 @@ zrandom(long cnt, ZVALUE *res)
|
||||
RANDOM *p_blum; /* malloced RANDOM by randomcopy() */
|
||||
int t; /* temp shift value */
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* firewall
|
||||
*/
|
||||
@@ -2951,6 +2959,12 @@ zrandomrange(CONST ZVALUE low, CONST ZVALUE beyond, ZVALUE *res)
|
||||
ZVALUE rangem1; /* range - 1 */
|
||||
long bitlen; /* smallest power of 2 >= diff */
|
||||
|
||||
/* firewall */
|
||||
if (res == NULL) {
|
||||
math_error("%s: res NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* firewall
|
||||
*/
|
||||
@@ -3043,6 +3057,12 @@ randomcopy(CONST RANDOM *state)
|
||||
{
|
||||
RANDOM *ret; /* return copy of state */
|
||||
|
||||
/* firewall */
|
||||
if (state == NULL) {
|
||||
math_error("%s: state NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* malloc state
|
||||
*/
|
||||
@@ -3094,6 +3114,12 @@ randomcopy(CONST RANDOM *state)
|
||||
void
|
||||
randomfree(RANDOM *state)
|
||||
{
|
||||
/* firewall */
|
||||
if (state == NULL) {
|
||||
math_error("%s: state NULL", __func__);
|
||||
not_reached();
|
||||
}
|
||||
|
||||
/* free the values */
|
||||
zfree_random(state->n);
|
||||
zfree_random(state->r);
|
||||
@@ -3123,6 +3149,16 @@ randomfree(RANDOM *state)
|
||||
bool
|
||||
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
|
||||
*/
|
||||
@@ -3171,6 +3207,7 @@ randomcmp(CONST RANDOM *s1, CONST RANDOM *s2)
|
||||
void
|
||||
randomprint(CONST RANDOM *UNUSED(state), int UNUSED(flags))
|
||||
{
|
||||
/* NOTE: It is OK for state == NULL */
|
||||
math_str("RANDOM state");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user