kiss_fftndr: fix allocation size check, odd sizes and ndims == 1

kiss_fftndr_alloc had three problems:

- Its overflow check divided the required size (truncated to an int) by
  every dimension and failed when fewer than sizeof(kiss_fft_scalar)
  bytes per element remained. The buffers only need slightly more than
  that per element, so valid sizes were rejected: 230 of the even NxN
  sizes from 2 to 512 (starting at 54x54) returned NULL. Replace it with
  a direct check that dimOther*(dimReal+2) scalars fit in size_t with a
  margin and that dimOther fits the int that kiss_fftnd counts it in,
  and make prod() saturate instead of wrapping.

- An odd last dimension is not supported by kiss_fftr, but the failure
  of the kiss_fftr_alloc size query was ignored and a config with a
  NULL kiss_fftr state was returned. Now kiss_fftndr_alloc returns NULL.

- With ndims == 1 the inner kiss_fftnd has zero dimensions, and
  kiss_fftnd never wrote its output in that case (the even-ndims path
  leaves the result in tmpbuf and the loop body never runs), so
  kiss_fftndr/kiss_fftndri returned garbage. Make a zero-dimensional
  kiss_fftnd the identity, which fixes both directions.

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-15 20:02:21 +01:00
co-authored by Claude Opus 5.5
parent 57a701785b
commit 933c235c1c
5 changed files with 154 additions and 15 deletions
+9 -2
View File
@@ -44,6 +44,7 @@ ifneq ($(KISSFFT_OPENMP),1)
SELFTEST = st-$(KISSFFT_DATATYPE)
TESTREAL = tr-$(KISSFFT_DATATYPE)
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)
TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE)
TESTKFC = tkfc-$(KISSFFT_DATATYPE)
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)
TESTCPP = testcpp-$(KISSFFT_DATATYPE)
@@ -54,6 +55,7 @@ else
SELFTEST = st-$(KISSFFT_DATATYPE)-openmp
TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp
TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE)-openmp
TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp
TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp
@@ -105,7 +107,7 @@ endif
# Target: "make all"
#
all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTKFC) $(TESTFASTFILT)
all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTNDRALLOC) $(TESTKFC) $(TESTFASTFILT)
#
# Individual test make rules
@@ -123,6 +125,9 @@ $(TESTREAL): test_real.c
$(TESTSIZEVAL): test_size_validation.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
$(TESTNDRALLOC): test_fftndr_alloc.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
$(BENCHKISS): benchkiss.c pstats.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
@@ -182,6 +187,8 @@ endif
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTREAL)
$(warning ======allocation size validation regression test (type= $(KISSFFT_DATATYPE) ))
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTSIZEVAL)
$(warning ======kiss_fftndr allocation regression test (type= $(KISSFFT_DATATYPE) ))
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDRALLOC)
$(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
@@ -193,4 +200,4 @@ endif
#
clean:
rm -f *~ bm-* st-* tr-* tsizeval-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c
rm -f *~ bm-* st-* tr-* tsizeval-* tndralloc-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c