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
+14 -13
View File
@@ -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<ndims;++k) {
check /= dims[k];
if (check <= sizeof(kiss_fft_scalar)) {
if (lenmem!=NULL)
*lenmem = (size_t)(-1);
return NULL;
}
}
if (lenmem==NULL) {
ptr = (char*) malloc(memneeded);
}else{