mirror of
https://github.com/mborgerding/kissfft.git
synced 2026-11-18 20:09:30 -05:00
- kiss_fft_alloc(0) passed the overflow check and computed
sizeof(kiss_fft_cpx)*(nfft-1) with nfft-1 == -1, returning a config.
Reject nfft <= 0 (negative sizes were only rejected by accident of the
signed/unsigned comparison, which is now an explicit cast).
- kiss_fftr_alloc(0) halved 0 and built a config around a NULL
sub-state. Reject nfft <= 0 along with odd sizes, and compute
nfft*3/2 in size_t.
- kiss_fftnd_alloc multiplied the dimensions in an int, so {65536,65536}
wrapped to 0 and {46342,46342} to a negative value: the size query
reported a bogus size and a far too small buffer was accepted. Reject
dims <= 0 and products that do not fit in an int (st->dimprod) or whose
buffer size overflows size_t, reporting *lenmem = (size_t)-1 as
kiss_fftndr_alloc does.
- kiss_fft_next_fast_size incremented n past INT_MAX for
n > 2125764000 (the largest 2,3,5-smooth int) and returned a negative
value. It now returns n itself there (documented in kiss_fft.h): no
2,3,5-smooth int >= n exists, and n is still a valid, if slower, size
for callers that pass the result straight to an alloc.
- kfc.c read an uninitialized length when the size query rejected nfft;
it now returns NULL for an invalid size.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ME1hnNaoD3ZcMj9H8TSYkZ
112 lines
2.5 KiB
C
112 lines
2.5 KiB
C
/*
|
|
* Copyright (c) 2003-2004, Mark Borgerding. All rights reserved.
|
|
* This file is part of KISS FFT - https://github.com/mborgerding/kissfft
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
* See COPYING file for more information.
|
|
*/
|
|
|
|
#include "kfc.h"
|
|
|
|
typedef struct cached_fft *kfc_cfg;
|
|
|
|
struct cached_fft
|
|
{
|
|
int nfft;
|
|
int inverse;
|
|
kiss_fft_cfg cfg;
|
|
kfc_cfg next;
|
|
};
|
|
|
|
static kfc_cfg cache_root=NULL;
|
|
static int ncached=0;
|
|
|
|
static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
|
|
{
|
|
size_t len = 0;
|
|
kfc_cfg cur=cache_root;
|
|
kfc_cfg prev=NULL;
|
|
while ( cur ) {
|
|
if ( cur->nfft == nfft && inverse == cur->inverse )
|
|
break;/*found the right node*/
|
|
prev = cur;
|
|
cur = prev->next;
|
|
}
|
|
if (cur== NULL) {
|
|
/* no cached node found, need to create a new one*/
|
|
kiss_fft_alloc(nfft,inverse,0,&len);
|
|
if (len == 0)
|
|
return NULL; /* invalid nfft */
|
|
#ifdef USE_SIMD
|
|
int padding = (16-sizeof(struct cached_fft)) & 15;
|
|
// make sure the cfg aligns on a 16 byte boundary
|
|
len += padding;
|
|
#endif
|
|
cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len ));
|
|
if (cur == NULL)
|
|
return NULL;
|
|
cur->cfg = (kiss_fft_cfg)(cur+1);
|
|
#ifdef USE_SIMD
|
|
cur->cfg = (kiss_fft_cfg) ((char*)(cur+1)+padding);
|
|
#endif
|
|
kiss_fft_alloc(nfft,inverse,cur->cfg,&len);
|
|
cur->nfft=nfft;
|
|
cur->inverse=inverse;
|
|
cur->next = NULL;
|
|
if ( prev )
|
|
prev->next = cur;
|
|
else
|
|
cache_root = cur;
|
|
++ncached;
|
|
}
|
|
return cur->cfg;
|
|
}
|
|
|
|
void kfc_cleanup(void)
|
|
{
|
|
kfc_cfg cur=cache_root;
|
|
kfc_cfg next=NULL;
|
|
while (cur){
|
|
next = cur->next;
|
|
free(cur);
|
|
cur=next;
|
|
}
|
|
ncached=0;
|
|
cache_root = NULL;
|
|
}
|
|
void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
|
|
{
|
|
kiss_fft( find_cached_fft(nfft,0),fin,fout );
|
|
}
|
|
|
|
void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
|
|
{
|
|
kiss_fft( find_cached_fft(nfft,1),fin,fout );
|
|
}
|
|
|
|
#ifdef KFC_TEST
|
|
static void check(int nc)
|
|
{
|
|
if (ncached != nc) {
|
|
fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
kiss_fft_cpx buf1[1024],buf2[1024];
|
|
memset(buf1,0,sizeof(buf1));
|
|
check(0);
|
|
kfc_fft(512,buf1,buf2);
|
|
check(1);
|
|
kfc_fft(512,buf1,buf2);
|
|
check(1);
|
|
kfc_ifft(512,buf1,buf2);
|
|
check(2);
|
|
kfc_cleanup();
|
|
check(0);
|
|
return 0;
|
|
}
|
|
#endif
|