diff --git a/test/test_real.c b/test/test_real.c index 52e9be8..e7a0dd9 100644 --- a/test/test_real.c +++ b/test/test_real.c @@ -85,8 +85,13 @@ int main(int argc,char ** argv) int nfft = 8*3*5; double ts,tfft,trfft; int i; - if (argc>1) + if (argc>1) { nfft = atoi(argv[1]); + if (nfft <= 0) { + fprintf(stderr,"Error: nfft must be a positive integer\n"); + return 1; + } + } kiss_fft_cpx cin[nfft]; kiss_fft_cpx cout[nfft]; kiss_fft_cpx sout[nfft]; diff --git a/test/testcpp.cc b/test/testcpp.cc index 50acada..5715a3f 100644 --- a/test/testcpp.cc +++ b/test/testcpp.cc @@ -25,6 +25,11 @@ using namespace std; template void dotest(int nfft) { + if (nfft <= 0) { + cerr << "invalid nfft:" << nfft << endl; + return; + } + typedef kissfft FFT; typedef std::complex cpx_type; @@ -59,7 +64,8 @@ void dotest(int nfft) complex dif = acc - x; difpower += norm(dif); } - cout << " RMSE:" << sqrt(difpower/totalpower) << "\t"; + const long double denom = (totalpower > 0) ? totalpower : 1e-30L; + cout << " RMSE:" << sqrt(difpower/denom) << "\t"; double t0 = curtime(); int nits=20e6/nfft; diff --git a/test/testkiss.py b/test/testkiss.py index 474de4d..2e69330 100755 --- a/test/testkiss.py +++ b/test/testkiss.py @@ -100,15 +100,14 @@ def dofft(x, isreal): x = 2147483647.0 * x scale = len(x) / 2147483647.0 - cmd = util + ' -n ' - cmd += ','.join([str(d) for d in dims]) + cmd = [util, '-n', ','.join([str(d) for d in dims])] if do_real: - cmd += ' -R ' + cmd.append('-R') - print(cmd) + print(' '.join(cmd)) from subprocess import Popen, PIPE - p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) + p = Popen(cmd, stdin=PIPE, stdout=PIPE) p.stdin.write(dopack(x)) p.stdin.close() diff --git a/test/twotonetest.c b/test/twotonetest.c index e93fac3..66a0b63 100644 --- a/test/twotonetest.c +++ b/test/twotonetest.c @@ -75,6 +75,9 @@ double two_tone_test( int nfft, int bin1,int bin2) sigpow += mag2; } kiss_fft_cleanup(); + free(cfg); + free(tbuf); + free(kout); /*printf("TEST %d,%d,%d noise @ %fdB\n",nfft,bin1,bin2,10*log10(noisepow/sigpow +1e-30) );*/ return 10*log10(sigpow/(noisepow+1e-50) ); } @@ -82,7 +85,13 @@ double two_tone_test( int nfft, int bin1,int bin2) int main(int argc,char ** argv) { int nfft = 4*2*2*3*5; - if (argc>1) nfft = atoi(argv[1]); + if (argc>1) { + nfft = atoi(argv[1]); + if (nfft <= 0) { + fprintf(stderr,"Error: nfft must be a positive integer\n"); + return 1; + } + } int i,j; double minsnr = 500; diff --git a/tools/fftutil.c b/tools/fftutil.c index 2d947d6..ef9be29 100644 --- a/tools/fftutil.c +++ b/tools/fftutil.c @@ -26,9 +26,16 @@ void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse) bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft ); st = kiss_fft_alloc( nfft ,isinverse ,0,0); - while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) { - kiss_fft( st , buf ,bufout); - fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout ); + while (1) { + size_t nread = fread(buf, sizeof(kiss_fft_cpx), nfft, fin); + if (nread == (size_t)nfft) { + kiss_fft( st , buf ,bufout); + fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout ); + continue; + } + if (nread != 0) + fprintf(stderr,"short read on complex input\n"); + break; } free(st); free(buf); @@ -47,9 +54,16 @@ void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse) buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * dimprod); st = kiss_fftnd_alloc (dims, ndims, isinverse, 0, 0); - while (fread (buf, sizeof (kiss_fft_cpx) * dimprod, 1, fin) > 0) { - kiss_fftnd (st, buf, buf); - fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout); + while (1) { + size_t nread = fread(buf, sizeof(kiss_fft_cpx), dimprod, fin); + if (nread == (size_t)dimprod) { + kiss_fftnd(st, buf, buf); + fwrite(buf, sizeof(kiss_fft_cpx), dimprod, fout); + continue; + } + if (nread != 0) + fprintf(stderr,"short read on nd complex input\n"); + break; } free (st); free (buf); @@ -81,17 +95,24 @@ void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse) st = kiss_fftndr_alloc(dims, ndims, isinverse, 0, 0); - while ( fread (ibuf, sizeof(kiss_fft_scalar), insize, fin) > 0) { - if (isinverse) { - kiss_fftndri(st, - (kiss_fft_cpx*)ibuf, - (kiss_fft_scalar*)obuf); - }else{ - kiss_fftndr(st, - (kiss_fft_scalar*)ibuf, - (kiss_fft_cpx*)obuf); + while (1) { + size_t nread = fread(ibuf, sizeof(kiss_fft_scalar), insize, fin); + if (nread == (size_t)insize) { + if (isinverse) { + kiss_fftndri(st, + (kiss_fft_cpx*)ibuf, + (kiss_fft_scalar*)obuf); + } else { + kiss_fftndr(st, + (kiss_fft_scalar*)ibuf, + (kiss_fft_cpx*)obuf); + } + fwrite(obuf, sizeof(kiss_fft_scalar), outsize, fout); + continue; } - fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout); + if (nread != 0) + fprintf(stderr,"short read on nd real input\n"); + break; } free(st); free(ibuf); @@ -110,14 +131,28 @@ void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse) st = kiss_fftr_alloc( nfft ,isinverse ,0,0); if (isinverse==0) { - while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) { - kiss_fftr( st , rbuf ,cbuf); - fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout ); + while (1) { + size_t nread = fread(rbuf, sizeof(kiss_fft_scalar), nfft, fin); + if (nread == nfft) { + kiss_fftr( st , rbuf ,cbuf); + fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout ); + continue; + } + if (nread != 0) + fprintf(stderr,"short read on real input\n"); + break; } }else{ - while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) { - kiss_fftri( st , cbuf ,rbuf); - fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout ); + while (1) { + size_t nread = fread(cbuf, sizeof(kiss_fft_cpx), (nfft/2+1), fin); + if (nread == (size_t)(nfft/2+1)) { + kiss_fftri( st , cbuf ,rbuf); + fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout ); + continue; + } + if (nread != 0) + fprintf(stderr,"short read on complex input\n"); + break; } } free(st); diff --git a/tools/kiss_fastfir.c b/tools/kiss_fastfir.c index e066328..a7e81d8 100644 --- a/tools/kiss_fastfir.c +++ b/tools/kiss_fastfir.c @@ -377,7 +377,6 @@ void do_file_filter( cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0); /* use length to minimize buffer shift*/ - n_samps_buf = 8*4096/sizeof(kffsamp_t); n_samps_buf = nfft + 4*(nfft-n_imp_resp+1); if (verbose) fprintf(stderr,"bufsize=%d\n",(int)(sizeof(kffsamp_t)*n_samps_buf) ); @@ -466,12 +465,31 @@ int main(int argc,char**argv) exit(1); } fseek(filtfile,0,SEEK_END); - nh = ftell(filtfile) / sizeof(kffsamp_t); + { + long filt_bytes = ftell(filtfile); + if (filt_bytes < 0) { + fprintf(stderr,"could not determine filter file size\n"); + exit(1); + } + if ((size_t)filt_bytes < sizeof(kffsamp_t)) { + fprintf(stderr,"filter file too small\n"); + exit(1); + } + nh = (size_t)filt_bytes / sizeof(kffsamp_t); + } if (verbose) fprintf(stderr,"%d samples in FIR filter\n",(int)nh); h = (kffsamp_t*)malloc(sizeof(kffsamp_t)*nh); + if (h == NULL) { + fprintf(stderr,"failed to allocate filter coefficients\n"); + exit(1); + } fseek(filtfile,0,SEEK_SET); if (fread(h,sizeof(kffsamp_t),nh,filtfile) != nh) - fprintf(stderr,"short read on filter file\n"); + { + fprintf(stderr,"short read on filter file\n"); + free(h); + exit(1); + } fclose(filtfile);