Users can override KISS_FFT_MALLOC and KISS_FFT_FREE, but several
places bypassed them:
- kiss_fftnd_alloc and kiss_fftndr_alloc called malloc() directly,
so their configs (released with kiss_fft_free / kiss_fftndr_free)
went to a different allocator than the one that freed them.
- kiss_fftndr_free was defined as plain free.
- kfc_cleanup released KISS_FFT_MALLOC'd cache entries with free().
- Under USE_SIMD, KISS_FFT_ALIGN_CHECK and KISS_FFT_ALIGN_SIZE_UP were
defined only inside "#ifndef KISS_FFT_MALLOC", so overriding the
allocator broke the build. Define them unconditionally.
Note that under USE_SIMD the nd/ndr configs now come from the SIMD
default allocator (_mm_malloc / aligned_alloc) instead of malloc, so
they must be released with kiss_fft_free / kiss_fftndr_free.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ME1hnNaoD3ZcMj9H8TSYkZ
- 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
kiss_fft_alloc and kiss_fftr_alloc internally use KISS_FFT_MALLOC to allocate memory, which is defined to _mm_malloc in a SIMD enabled application. Calling free() on memory allocated with _mm_malloc will result in unpredictable behavior. The configuration data allocated with _mm_malloc should be freed with a corresponding _mm_free.