From 933c235c1c2ad21cdef444515a9e55c7f8170299 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Wed, 11 Nov 2026 09:27:24 +0100 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01ME1hnNaoD3ZcMj9H8TSYkZ --- kiss_fftnd.c | 6 ++ kiss_fftndr.c | 27 +++++---- test/CMakeLists.txt | 1 + test/Makefile | 11 +++- test/test_fftndr_alloc.c | 124 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 test/test_fftndr_alloc.c diff --git a/kiss_fftnd.c b/kiss_fftnd.c index 550d231..a7ff84c 100644 --- a/kiss_fftnd.c +++ b/kiss_fftnd.c @@ -166,6 +166,12 @@ void kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) const kiss_fft_cpx * bufin=fin; kiss_fft_cpx * bufout; + if (st->ndims == 0) { /* a zero-dimensional transform is the identity */ + if (fin != fout) + *fout = *fin; + return; + } + /*arrange it so the last bufout == fout*/ if ( st->ndims & 1 ) { bufout = fout; diff --git a/kiss_fftndr.c b/kiss_fftndr.c index c1cd27b..52b7d58 100644 --- a/kiss_fftndr.c +++ b/kiss_fftndr.c @@ -22,8 +22,11 @@ struct kiss_fftndr_state static size_t prod(const int *dims, int ndims) { size_t x=1; - while (ndims--) + while (ndims--) { + if (*dims > 0 && x > (size_t)-1 / (size_t)*dims) + return (size_t)-1; /* overflow: saturate so the size check below fails */ x *= *dims++; + } return x; } @@ -37,9 +40,18 @@ kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void size_t dimOther = prod(dims,ndims-1); size_t memneeded; char * ptr = NULL; - int k,check; (void)kiss_fftr_alloc(dimReal,inverse_fft,NULL,&nr); + if (nr == 0) + return NULL; /* dimReal not supported by kiss_fftr (e.g. odd) */ + + /* check for overflow: the buffers below need about 2*dimOther*(dimReal+2) scalars; keep a 2x margin */ + /* kiss_fftnd counts the other dimensions in an int */ + if (dimOther > INT_MAX || dimOther > (size_t)-1 / 4 / sizeof(kiss_fft_scalar) / ((size_t)dimReal + 2)) { + if (lenmem!=NULL) + *lenmem = (size_t)(-1); + return NULL; + } (void)kiss_fftnd_alloc(dims,ndims-1,inverse_fft,NULL,&nd); ntmp = MAX( 2*dimOther , dimReal+2) * sizeof(kiss_fft_scalar) // freq buffer for one pass @@ -47,17 +59,6 @@ kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof( struct kiss_fftndr_state )) + KISS_FFT_ALIGN_SIZE_UP(nr) + KISS_FFT_ALIGN_SIZE_UP(nd) + KISS_FFT_ALIGN_SIZE_UP(ntmp); - /* check for overflow */ - check = memneeded; - for (k=0;k +#include +#include +#include "kiss_fftndr.h" +#include "kiss_fftr.h" + +static int failures = 0; + +static void fail(const char *msg, int a, int b) +{ + printf("FAIL: %s (%d,%d)\n", msg, a, b); + ++failures; +} + +/* allocate through the caller-supplied-buffer path; returns NULL on failure */ +static kiss_fftndr_cfg alloc_ndr(const int *dims, int ndims, void **buf) +{ + size_t len = 0; + *buf = NULL; + kiss_fftndr_alloc(dims, ndims, 0, NULL, &len); + if (len == 0 || len == (size_t)-1) + return NULL; + *buf = malloc(len); + if (*buf == NULL) + return NULL; + return kiss_fftndr_alloc(dims, ndims, 0, *buf, &len); +} + +static void test_valid_sizes_accepted(void) +{ + int n, first = 0, rejected = 0; + for (n = 2; n <= 512; n += 2) { + int dims[2]; + void *buf; + dims[0] = n; + dims[1] = n; + if (alloc_ndr(dims, 2, &buf) == NULL) { + if (!first) + first = n; + ++rejected; + } + free(buf); + } + if (rejected) { + printf("(%d valid even NxN sizes in 2..512 rejected)\n", rejected); + fail("kiss_fftndr_alloc rejected a valid square size, first", first, first); + } +} + +static void test_odd_last_dim_rejected(void) +{ + int dims[2] = {4, 5}; + void *buf; + if (alloc_ndr(dims, 2, &buf) != NULL) + fail("kiss_fftndr_alloc accepted an odd last dimension", dims[0], dims[1]); + free(buf); +} + +/* the inner kiss_fftnd counts dims[0..ndims-2] in an int: a product above INT_MAX + * must be reported as too large by the size query (no allocation is attempted) */ +static void test_int_overflow_rejected(void) +{ + int dims[3] = {46341, 46341, 2}; /* 46341^2 > INT_MAX */ + size_t len = 0; + kiss_fftndr_alloc(dims, 3, 0, NULL, &len); + if (len != (size_t)-1) + fail("kiss_fftndr_alloc size query accepted dims whose product overflows int", dims[0], dims[1]); +} + +#ifndef USE_SIMD +static void test_one_dimension_matches_fftr(void) +{ + enum { N = 8 }; + int dims[1] = {N}; + kiss_fft_scalar in[N]; + kiss_fft_cpx got[N / 2 + 1], want[N / 2 + 1]; + void *buf; + kiss_fftndr_cfg ndr; + kiss_fftr_cfg r; + int k; + + for (k = 0; k < N; ++k) + in[k] = (kiss_fft_scalar)(100 * (k + 1)); + + ndr = alloc_ndr(dims, 1, &buf); + r = kiss_fftr_alloc(N, 0, NULL, NULL); + if (ndr == NULL || r == NULL) { + fail("allocation failed for ndims == 1", N, 0); + } else { + kiss_fftndr(ndr, in, got); + kiss_fftr(r, in, want); + for (k = 0; k <= N / 2; ++k) + if (got[k].r != want[k].r || got[k].i != want[k].i) + fail("ndims == 1 output differs from kiss_fftr at bin", k, 0); + } + free(buf); + kiss_fftr_free(r); +} +#endif + +int main(void) +{ + test_valid_sizes_accepted(); + test_odd_last_dim_rejected(); + test_int_overflow_rejected(); +#ifndef USE_SIMD + test_one_dimension_matches_fftr(); +#endif + printf("%s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +}