Merge pull request #130 from ErnoKilp/erno/fix_coverity_findings

Fix High and Medium Severity level coverity findings
This commit is contained in:
mborgerding
2026-08-12 10:54:37 -04:00
committed by GitHub
6 changed files with 105 additions and 33 deletions
+6 -1
View File
@@ -85,8 +85,13 @@ int main(int argc,char ** argv)
int nfft = 8*3*5; int nfft = 8*3*5;
double ts,tfft,trfft; double ts,tfft,trfft;
int i; int i;
if (argc>1) if (argc>1) {
nfft = atoi(argv[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 cin[nfft];
kiss_fft_cpx cout[nfft]; kiss_fft_cpx cout[nfft];
kiss_fft_cpx sout[nfft]; kiss_fft_cpx sout[nfft];
+7 -1
View File
@@ -25,6 +25,11 @@ using namespace std;
template <class T> template <class T>
void dotest(int nfft) void dotest(int nfft)
{ {
if (nfft <= 0) {
cerr << "invalid nfft:" << nfft << endl;
return;
}
typedef kissfft<T> FFT; typedef kissfft<T> FFT;
typedef std::complex<T> cpx_type; typedef std::complex<T> cpx_type;
@@ -59,7 +64,8 @@ void dotest(int nfft)
complex<long double> dif = acc - x; complex<long double> dif = acc - x;
difpower += norm(dif); 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(); double t0 = curtime();
int nits=20e6/nfft; int nits=20e6/nfft;
+4 -5
View File
@@ -100,15 +100,14 @@ def dofft(x, isreal):
x = 2147483647.0 * x x = 2147483647.0 * x
scale = len(x) / 2147483647.0 scale = len(x) / 2147483647.0
cmd = util + ' -n ' cmd = [util, '-n', ','.join([str(d) for d in dims])]
cmd += ','.join([str(d) for d in dims])
if do_real: if do_real:
cmd += ' -R ' cmd.append('-R')
print(cmd) print(' '.join(cmd))
from subprocess import Popen, PIPE 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.write(dopack(x))
p.stdin.close() p.stdin.close()
+10 -1
View File
@@ -75,6 +75,9 @@ double two_tone_test( int nfft, int bin1,int bin2)
sigpow += mag2; sigpow += mag2;
} }
kiss_fft_cleanup(); 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) );*/ /*printf("TEST %d,%d,%d noise @ %fdB\n",nfft,bin1,bin2,10*log10(noisepow/sigpow +1e-30) );*/
return 10*log10(sigpow/(noisepow+1e-50) ); 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 main(int argc,char ** argv)
{ {
int nfft = 4*2*2*3*5; 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; int i,j;
double minsnr = 500; double minsnr = 500;
+44 -9
View File
@@ -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 ); bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
st = kiss_fft_alloc( nfft ,isinverse ,0,0); st = kiss_fft_alloc( nfft ,isinverse ,0,0);
while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) { while (1) {
size_t nread = fread(buf, sizeof(kiss_fft_cpx), nfft, fin);
if (nread == (size_t)nfft) {
kiss_fft( st , buf ,bufout); kiss_fft( st , buf ,bufout);
fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout ); fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout );
continue;
}
if (nread != 0)
fprintf(stderr,"short read on complex input\n");
break;
} }
free(st); free(st);
free(buf); 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); buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * dimprod);
st = kiss_fftnd_alloc (dims, ndims, isinverse, 0, 0); st = kiss_fftnd_alloc (dims, ndims, isinverse, 0, 0);
while (fread (buf, sizeof (kiss_fft_cpx) * dimprod, 1, fin) > 0) { while (1) {
kiss_fftnd (st, buf, buf); size_t nread = fread(buf, sizeof(kiss_fft_cpx), dimprod, fin);
fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout); 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 (st);
free (buf); 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); st = kiss_fftndr_alloc(dims, ndims, isinverse, 0, 0);
while ( fread (ibuf, sizeof(kiss_fft_scalar), insize, fin) > 0) { while (1) {
size_t nread = fread(ibuf, sizeof(kiss_fft_scalar), insize, fin);
if (nread == (size_t)insize) {
if (isinverse) { if (isinverse) {
kiss_fftndri(st, kiss_fftndri(st,
(kiss_fft_cpx*)ibuf, (kiss_fft_cpx*)ibuf,
(kiss_fft_scalar*)obuf); (kiss_fft_scalar*)obuf);
}else{ } else {
kiss_fftndr(st, kiss_fftndr(st,
(kiss_fft_scalar*)ibuf, (kiss_fft_scalar*)ibuf,
(kiss_fft_cpx*)obuf); (kiss_fft_cpx*)obuf);
} }
fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout); fwrite(obuf, sizeof(kiss_fft_scalar), outsize, fout);
continue;
}
if (nread != 0)
fprintf(stderr,"short read on nd real input\n");
break;
} }
free(st); free(st);
free(ibuf); 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); st = kiss_fftr_alloc( nfft ,isinverse ,0,0);
if (isinverse==0) { if (isinverse==0) {
while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) { while (1) {
size_t nread = fread(rbuf, sizeof(kiss_fft_scalar), nfft, fin);
if (nread == nfft) {
kiss_fftr( st , rbuf ,cbuf); kiss_fftr( st , rbuf ,cbuf);
fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout ); fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout );
continue;
}
if (nread != 0)
fprintf(stderr,"short read on real input\n");
break;
} }
}else{ }else{
while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) { 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); kiss_fftri( st , cbuf ,rbuf);
fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout ); fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout );
continue;
}
if (nread != 0)
fprintf(stderr,"short read on complex input\n");
break;
} }
} }
free(st); free(st);
+20 -2
View File
@@ -377,7 +377,6 @@ void do_file_filter(
cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0); cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0);
/* use length to minimize buffer shift*/ /* use length to minimize buffer shift*/
n_samps_buf = 8*4096/sizeof(kffsamp_t);
n_samps_buf = nfft + 4*(nfft-n_imp_resp+1); n_samps_buf = nfft + 4*(nfft-n_imp_resp+1);
if (verbose) fprintf(stderr,"bufsize=%d\n",(int)(sizeof(kffsamp_t)*n_samps_buf) ); 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); exit(1);
} }
fseek(filtfile,0,SEEK_END); 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); if (verbose) fprintf(stderr,"%d samples in FIR filter\n",(int)nh);
h = (kffsamp_t*)malloc(sizeof(kffsamp_t)*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); fseek(filtfile,0,SEEK_SET);
if (fread(h,sizeof(kffsamp_t),nh,filtfile) != nh) 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); fclose(filtfile);