Commit 550437edbed3dd6d24d7cef4447a95aef1310bbd

Ryan C. Gordon 2018-08-08T12:13:56

libm: Fixed all static analysis warnings.

diff --git a/src/libm/e_rem_pio2.c b/src/libm/e_rem_pio2.c
index df7c2b8..5e055d6 100644
--- a/src/libm/e_rem_pio2.c
+++ b/src/libm/e_rem_pio2.c
@@ -154,7 +154,7 @@ int32_t attribute_hidden __ieee754_rem_pio2(double x, double *y)
 	}
 	tx[2] = z;
 	nx = 3;
-	while(tx[nx-1]==zero) nx--;	/* skip zero term */
+	while((nx > 0) && tx[nx-1]==zero) nx--;	/* skip zero term */
 	n  =  __kernel_rem_pio2(tx,y,e0,nx,2,two_over_pi);
 	if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;}
 	return n;
diff --git a/src/libm/k_rem_pio2.c b/src/libm/k_rem_pio2.c
index 162b8e9..6cf68a9 100644
--- a/src/libm/k_rem_pio2.c
+++ b/src/libm/k_rem_pio2.c
@@ -128,6 +128,8 @@
 #include "math_libm.h"
 #include "math_private.h"
 
+#include "SDL_assert.h"
+
 static const int init_jk[] = {2,3,4,6}; /* initial value for jk */
 
 static const double PIo2[] = {
@@ -147,13 +149,19 @@ one    = 1.0,
 two24   =  1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
 twon24  =  5.96046447753906250000e-08; /* 0x3E700000, 0x00000000 */
 
-int32_t attribute_hidden __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec, const int32_t *ipio2)
+int32_t attribute_hidden __kernel_rem_pio2(double *x, double *y, int e0, int nx, const unsigned int prec, const int32_t *ipio2)
 {
 	int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih;
 	double z,fw,f[20],fq[20],q[20];
 
+	if (nx < 1) {
+		return 0;
+	}
+
     /* initialize jk*/
+	SDL_assert(prec < SDL_arraysize(init_jk));
 	jk = init_jk[prec];
+	SDL_assert(jk > 0);
 	jp = jk;
 
     /* determine jx,jv,q0, note that 3>q0 */
@@ -164,6 +172,7 @@ int32_t attribute_hidden __kernel_rem_pio2(double *x, double *y, int e0, int nx,
     /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
 	j = jv-jx; m = jx+jk;
 	for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (double) ipio2[j];
+	for(i=m+1;i<SDL_arraysize(f);i++) f[i] = zero;
 
     /* compute q[0],q[1],...q[jk] */
 	for (i=0;i<=jk;i++) {
@@ -179,6 +188,7 @@ recompute:
 	    iq[i] =  (int32_t)(z-two24*fw);
 	    z     =  q[j-1]+fw;
 	}
+	iq[jz] = 0;
 
     /* compute n */
 	z  = scalbn(z,q0);		/* actual value of z */
@@ -238,7 +248,8 @@ recompute:
     /* chop off zero terms */
 	if(z==0.0) {
 	    jz -= 1; q0 -= 24;
-	    while(iq[jz]==0) { jz--; q0-=24;}
+		SDL_assert(jz >= 0);
+	    while(iq[jz]==0) { jz--; SDL_assert(jz >= 0); q0-=24;}
 	} else { /* break z into 24-bit if necessary */
 	    z = scalbn(z,-q0);
 	    if(z>=two24) {
diff --git a/src/libm/math_private.h b/src/libm/math_private.h
index d5ff0c4..d0ef66a 100644
--- a/src/libm/math_private.h
+++ b/src/libm/math_private.h
@@ -221,7 +221,7 @@ __ieee754_sqrt(double)
      extern double __kernel_sin(double, double, int) attribute_hidden;
      extern double __kernel_cos(double, double) attribute_hidden;
      extern double __kernel_tan(double, double, int) attribute_hidden;
-     extern int32_t __kernel_rem_pio2(double *, double *, int, int, int,
+     extern int32_t __kernel_rem_pio2(double *, double *, int, int, const unsigned int,
                                   const int32_t *) attribute_hidden;
 
 #endif /* _MATH_PRIVATE_H_ */