this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #45 from kpawlak/task-logging-and-check

Logging improvements and buffers security

authored by

mborgerding and committed by
GitHub
f0a47de6 cf5813a1

+77 -24
+6 -5
_kiss_fft_guts.h
··· 15 15 #define _kiss_fft_guts_h 16 16 17 17 #include "kiss_fft.h" 18 + #include "kiss_fft_log.h" 18 19 #include <limits.h> 19 20 20 21 #define MAXFACTORS 32 21 - /* e.g. an fft of length 128 has 4 factors 22 + /* e.g. an fft of length 128 has 4 factors 22 23 as far as kissfft is concerned 23 24 4*4*4*2 24 25 */ ··· 56 57 #if defined(CHECK_OVERFLOW) 57 58 # define CHECK_OVERFLOW_OP(a,op,b) \ 58 59 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ 59 - fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } 60 + KISS_FFT_WARNING("overflow (%d " #op" %d) = %ld", (a),(b),(SAMPPROD)(a) op (SAMPPROD)(b)); } 60 61 #endif 61 62 62 63 ··· 146 147 147 148 /* a debugging function */ 148 149 #define pcpx(c)\ 149 - fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) 150 + KISS_FFT_DEBUG("%g + %gi\n",(double)((c)->r),(double)((c)->i)) 150 151 151 152 152 153 #ifdef KISS_FFT_USE_ALLOCA 153 154 // define this to allow use of alloca instead of malloc for temporary buffers 154 - // Temporary buffers are used in two case: 155 + // Temporary buffers are used in two case: 155 156 // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 156 157 // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. 157 158 #include <alloca.h> 158 159 #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) 159 - #define KISS_FFT_TMP_FREE(ptr) 160 + #define KISS_FFT_TMP_FREE(ptr) 160 161 #else 161 162 #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) 162 163 #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr)
+26 -10
kiss_fft.c
··· 203 203 int Norig = st->nfft; 204 204 205 205 kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); 206 + if (scratch == NULL){ 207 + KISS_FFT_ERROR("Memory allocation failed."); 208 + return; 209 + } 206 210 207 211 for ( u=0; u<m; ++u ) { 208 212 k=u; ··· 244 248 const kiss_fft_cpx * Fout_end = Fout + p*m; 245 249 246 250 #ifdef _OPENMP 247 - // use openmp extensions at the 251 + // use openmp extensions at the 248 252 // top-level (not recursive) 249 253 if (fstride==1 && p<=5 && m!=1) 250 254 { ··· 252 256 253 257 // execute the p different work units in different threads 254 258 # pragma omp parallel for 255 - for (k=0;k<p;++k) 259 + for (k=0;k<p;++k) 256 260 kf_work( Fout +k*m, f+ fstride*in_stride*k,fstride*p,in_stride,factors,st); 257 261 // all threads have joined by this point 258 262 259 263 switch (p) { 260 264 case 2: kf_bfly2(Fout,fstride,st,m); break; 261 - case 3: kf_bfly3(Fout,fstride,st,m); break; 265 + case 3: kf_bfly3(Fout,fstride,st,m); break; 262 266 case 4: kf_bfly4(Fout,fstride,st,m); break; 263 - case 5: kf_bfly5(Fout,fstride,st,m); break; 267 + case 5: kf_bfly5(Fout,fstride,st,m); break; 264 268 default: kf_bfly_generic(Fout,fstride,st,m,p); break; 265 269 } 266 270 return; ··· 276 280 do{ 277 281 // recursive call: 278 282 // DFT of size m*p performed by doing 279 - // p instances of smaller DFTs of size m, 283 + // p instances of smaller DFTs of size m, 280 284 // each one takes a decimated version of the input 281 285 kf_work( Fout , f, fstride*p, in_stride, factors,st); 282 286 f += fstride*in_stride; ··· 285 289 286 290 Fout=Fout_beg; 287 291 288 - // recombine the p smaller DFTs 292 + // recombine the p smaller DFTs 289 293 switch (p) { 290 294 case 2: kf_bfly2(Fout,fstride,st,m); break; 291 - case 3: kf_bfly3(Fout,fstride,st,m); break; 295 + case 3: kf_bfly3(Fout,fstride,st,m); break; 292 296 case 4: kf_bfly4(Fout,fstride,st,m); break; 293 - case 5: kf_bfly5(Fout,fstride,st,m); break; 297 + case 5: kf_bfly5(Fout,fstride,st,m); break; 294 298 default: kf_bfly_generic(Fout,fstride,st,m,p); break; 295 299 } 296 300 } 297 301 298 302 /* facbuf is populated by p1,m1,p2,m2, ... 299 - where 303 + where 300 304 p[i] * m[i] = m[i-1] 301 305 m0 = n */ 302 - static 306 + static 303 307 void kf_factor(int n,int * facbuf) 304 308 { 305 309 int p=4; ··· 369 373 if (fin == fout) { 370 374 //NOTE: this is not really an in-place FFT algorithm. 371 375 //It just performs an out-of-place FFT into a temp buffer 376 + if (fout == NULL){ 377 + KISS_FFT_ERROR("fout buffer NULL."); 378 + return; 379 + } 380 + 372 381 kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); 382 + if (tmpbuf == NULL){ 383 + KISS_FFT_ERROR("Memory allocation error."); 384 + return; 385 + } 386 + 387 + 388 + 373 389 kf_work(tmpbuf,fin,1,in_stride, st->factors,st); 374 390 memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); 375 391 KISS_FFT_TMP_FREE(tmpbuf);
+36
kiss_fft_log.h
··· 1 + /* 2 + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. 3 + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft 4 + * 5 + * SPDX-License-Identifier: BSD-3-Clause 6 + * See COPYING file for more information. 7 + */ 8 + 9 + #ifndef kiss_fft_log_h 10 + #define kiss_fft_log_h 11 + 12 + #define ERROR 1 13 + #define WARNING 2 14 + #define INFO 3 15 + #define DEBUG 4 16 + 17 + #define STRINGIFY(x) #x 18 + #define TOSTRING(x) STRINGIFY(x) 19 + 20 + #if defined(NDEBUG) 21 + # define KISS_FFT_LOG_MSG(severity, ...) ((void)0) 22 + #else 23 + # define KISS_FFT_LOG_MSG(severity, ...) \ 24 + fprintf(stderr, "[" #severity "] " __FILE__ ":" TOSTRING(__LINE__) " "); \ 25 + fprintf(stderr, __VA_ARGS__); \ 26 + fprintf(stderr, "\n") 27 + #endif 28 + 29 + #define KISS_FFT_ERROR(...) KISS_FFT_LOG_MSG(ERROR, __VA_ARGS__) 30 + #define KISS_FFT_WARNING(...) KISS_FFT_LOG_MSG(WARNING, __VA_ARGS__) 31 + #define KISS_FFT_INFO(...) KISS_FFT_LOG_MSG(INFO, __VA_ARGS__) 32 + #define KISS_FFT_DEBUG(...) KISS_FFT_LOG_MSG(DEBUG, __VA_ARGS__) 33 + 34 + 35 + 36 + #endif /* kiss_fft_log_h */
+9 -9
tools/kiss_fftr.c
··· 27 27 size_t subsize = 0, memneeded; 28 28 29 29 if (nfft & 1) { 30 - fprintf(stderr,"Real FFT optimization must be even.\n"); 30 + KISS_FFT_ERROR("Real FFT optimization must be even."); 31 31 return NULL; 32 32 } 33 33 nfft >>= 1; ··· 67 67 kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; 68 68 69 69 if ( st->substate->inverse) { 70 - fprintf(stderr,"kiss fft usage error: improper alloc\n"); 70 + KISS_FFT_ERROR("kiss fft usage error: improper alloc"); 71 71 exit(1); 72 72 } 73 73 ··· 79 79 * contains the sum of the even-numbered elements of the input time sequence 80 80 * The imag part is the sum of the odd-numbered elements 81 81 * 82 - * The sum of tdc.r and tdc.i is the sum of the input time sequence. 82 + * The sum of tdc.r and tdc.i is the sum of the input time sequence. 83 83 * yielding DC of input time sequence 84 - * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... 84 + * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... 85 85 * yielding Nyquist bin of input time sequence 86 86 */ 87 - 87 + 88 88 tdc.r = st->tmpbuf[0].r; 89 89 tdc.i = st->tmpbuf[0].i; 90 90 C_FIXDIV(tdc,2); ··· 92 92 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); 93 93 freqdata[0].r = tdc.r + tdc.i; 94 94 freqdata[ncfft].r = tdc.r - tdc.i; 95 - #ifdef USE_SIMD 95 + #ifdef USE_SIMD 96 96 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); 97 97 #else 98 98 freqdata[ncfft].i = freqdata[0].i = 0; 99 99 #endif 100 100 101 101 for ( k=1;k <= ncfft/2 ; ++k ) { 102 - fpk = st->tmpbuf[k]; 102 + fpk = st->tmpbuf[k]; 103 103 fpnk.r = st->tmpbuf[ncfft-k].r; 104 104 fpnk.i = - st->tmpbuf[ncfft-k].i; 105 105 C_FIXDIV(fpk,2); ··· 122 122 int k, ncfft; 123 123 124 124 if (st->substate->inverse == 0) { 125 - fprintf (stderr, "kiss fft usage error: improper alloc\n"); 125 + KISS_FFT_ERROR("kiss fft usage error: improper alloc"); 126 126 exit (1); 127 127 } 128 128 ··· 145 145 C_MUL (fok, tmp, st->super_twiddles[k-1]); 146 146 C_ADD (st->tmpbuf[k], fek, fok); 147 147 C_SUB (st->tmpbuf[ncfft - k], fek, fok); 148 - #ifdef USE_SIMD 148 + #ifdef USE_SIMD 149 149 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0); 150 150 #else 151 151 st->tmpbuf[ncfft - k].i *= -1;