kiss_fft: validate sizes in the alloc functions and next_fast_size

- 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
This commit is contained in:
Davide Faconti
2026-11-11 10:12:39 +01:00
co-authored by Claude Opus 5.5
parent f3fd3a58f1
commit 24fa326186
8 changed files with 191 additions and 9 deletions
+9 -2
View File
@@ -43,6 +43,7 @@ ifneq ($(KISSFFT_OPENMP),1)
BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE)
SELFTEST = st-$(KISSFFT_DATATYPE)
TESTREAL = tr-$(KISSFFT_DATATYPE)
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)
TESTKFC = tkfc-$(KISSFFT_DATATYPE)
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)
TESTCPP = testcpp-$(KISSFFT_DATATYPE)
@@ -52,6 +53,7 @@ else
BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE)-openmp
SELFTEST = st-$(KISSFFT_DATATYPE)-openmp
TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp
TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp
TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp
@@ -103,7 +105,7 @@ endif
# Target: "make all"
#
all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTKFC) $(TESTFASTFILT)
all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTKFC) $(TESTFASTFILT)
#
# Individual test make rules
@@ -118,6 +120,9 @@ $(TESTKFC): ../kfc.c
$(TESTREAL): test_real.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
$(TESTSIZEVAL): test_size_validation.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
$(BENCHKISS): benchkiss.c pstats.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
@@ -175,6 +180,8 @@ endif
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(SELFTEST)
$(warning ======real FFT (type= $(KISSFFT_DATATYPE) ))
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTREAL)
$(warning ======allocation size validation regression test (type= $(KISSFFT_DATATYPE) ))
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTSIZEVAL)
$(warning ======timing test (type=$(KISSFFT_DATATYPE)))
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHKISS) -x $(NUMFFTS) -n $(NFFT)
@[ -x ./$(BENCHFFTW) ] && LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHFFTW) -x $(NUMFFTS) -n $(NFFT) || true
@@ -186,4 +193,4 @@ endif
#
clean:
rm -f *~ bm-* st-* tr-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c
rm -f *~ bm-* st-* tr-* tsizeval-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c