Merge pull request #145 from facontidavide/fix/size-validation

[fix 1/8] kiss_fft: validate sizes in the alloc functions and next_fast_size
This commit is contained in:
mborgerding
2026-11-15 13:29:49 -05:00
committed by GitHub
8 changed files with 191 additions and 9 deletions
+3 -1
View File
@@ -23,7 +23,7 @@ static int ncached=0;
static kiss_fft_cfg find_cached_fft(int nfft,int inverse) static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
{ {
size_t len; size_t len = 0;
kfc_cfg cur=cache_root; kfc_cfg cur=cache_root;
kfc_cfg prev=NULL; kfc_cfg prev=NULL;
while ( cur ) { while ( cur ) {
@@ -35,6 +35,8 @@ static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
if (cur== NULL) { if (cur== NULL) {
/* no cached node found, need to create a new one*/ /* no cached node found, need to create a new one*/
kiss_fft_alloc(nfft,inverse,0,&len); kiss_fft_alloc(nfft,inverse,0,&len);
if (len == 0)
return NULL; /* invalid nfft */
#ifdef USE_SIMD #ifdef USE_SIMD
int padding = (16-sizeof(struct cached_fft)) & 15; int padding = (16-sizeof(struct cached_fft)) & 15;
// make sure the cfg aligns on a 16 byte boundary // make sure the cfg aligns on a 16 byte boundary
+4 -2
View File
@@ -339,8 +339,8 @@ kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem
KISS_FFT_ALIGN_CHECK(mem) KISS_FFT_ALIGN_CHECK(mem)
kiss_fft_cfg st=NULL; kiss_fft_cfg st=NULL;
// check for overflow condition {memneeded > SIZE_MAX}. // reject empty/negative sizes and the overflow condition {memneeded > SIZE_MAX}.
if (nfft >= (SIZE_MAX - 2*sizeof(struct kiss_fft_state))/sizeof(kiss_fft_cpx)) if (nfft <= 0 || (size_t)nfft >= (SIZE_MAX - 2*sizeof(struct kiss_fft_state))/sizeof(kiss_fft_cpx))
return NULL; return NULL;
size_t memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fft_state) size_t memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fft_state)
@@ -413,6 +413,8 @@ int kiss_fft_next_fast_size(int n)
{ {
if (n <= 0) /* nothing to factor; avoid infinite loop for n<=0 */ if (n <= 0) /* nothing to factor; avoid infinite loop for n<=0 */
return 1; return 1;
if (n > 2125764000) /* 2^5 * 3^12 * 5^3 is the largest 2,3,5-smooth int; n++ would overflow */
return n;
while(1) { while(1) {
int m=n; int m=n;
while ( (m%2) == 0 ) m/=2; while ( (m%2) == 0 ) m/=2;
+2 -1
View File
@@ -173,7 +173,8 @@ void KISS_FFT_API kiss_fft_cleanup(void);
/* /*
* Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5).
* Returns 1 for n<=0, and n itself if no such k fits in an int (n > 2125764000).
*/ */
int KISS_FFT_API kiss_fft_next_fast_size(int n); int KISS_FFT_API kiss_fft_next_fast_size(int n);
+7
View File
@@ -29,6 +29,13 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m
for (i=0;i<ndims;++i) { for (i=0;i<ndims;++i) {
size_t sublen=0; size_t sublen=0;
/* reject invalid dims and products that overflow int (st->dimprod) or the tmpbuf size */
if (dims[i] <= 0 || dimprod > INT_MAX / dims[i]
|| (size_t)(dimprod * dims[i]) > (size_t)(-1) / 2 / sizeof(kiss_fft_cpx)) {
if (lenmem != NULL)
*lenmem = (size_t)(-1);
return NULL;
}
kiss_fft_alloc (dims[i], inverse_fft, NULL, &sublen); kiss_fft_alloc (dims[i], inverse_fft, NULL, &sublen);
memneeded += sublen; /* st->states[i] */ memneeded += sublen; /* st->states[i] */
dimprod *= dims[i]; dimprod *= dims[i];
+3 -3
View File
@@ -26,14 +26,14 @@ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenme
kiss_fftr_cfg st = NULL; kiss_fftr_cfg st = NULL;
size_t subsize = 0, memneeded; size_t subsize = 0, memneeded;
if (nfft & 1) { if (nfft <= 0 || (nfft & 1)) {
KISS_FFT_ERROR("Real FFT optimization must be even."); KISS_FFT_ERROR("Real FFT optimization must be even and positive.");
return NULL; return NULL;
} }
nfft >>= 1; nfft >>= 1;
kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( (size_t)nfft * 3 / 2);
if (lenmem == NULL) { if (lenmem == NULL) {
st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
+1
View File
@@ -48,6 +48,7 @@ target_compile_definitions(tkfc PRIVATE KFC_TEST)
add_kissfft_test_executable(ffr twotonetest.c) add_kissfft_test_executable(ffr twotonetest.c)
add_kissfft_test_executable(tr test_real.c) add_kissfft_test_executable(tr test_real.c)
add_kissfft_test_executable(tsizeval test_size_validation.c)
add_kissfft_test_executable(next_fast_size next_fast_size.c) add_kissfft_test_executable(next_fast_size next_fast_size.c)
+9 -2
View File
@@ -43,6 +43,7 @@ ifneq ($(KISSFFT_OPENMP),1)
BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE) BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE)
SELFTEST = st-$(KISSFFT_DATATYPE) SELFTEST = st-$(KISSFFT_DATATYPE)
TESTREAL = tr-$(KISSFFT_DATATYPE) TESTREAL = tr-$(KISSFFT_DATATYPE)
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)
TESTKFC = tkfc-$(KISSFFT_DATATYPE) TESTKFC = tkfc-$(KISSFFT_DATATYPE)
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE) TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)
TESTCPP = testcpp-$(KISSFFT_DATATYPE) TESTCPP = testcpp-$(KISSFFT_DATATYPE)
@@ -52,6 +53,7 @@ else
BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE)-openmp BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE)-openmp
SELFTEST = st-$(KISSFFT_DATATYPE)-openmp SELFTEST = st-$(KISSFFT_DATATYPE)-openmp
TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp
TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp
TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp
@@ -103,7 +105,7 @@ endif
# Target: "make all" # Target: "make all"
# #
all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTKFC) $(TESTFASTFILT) all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTKFC) $(TESTFASTFILT)
# #
# Individual test make rules # Individual test make rules
@@ -118,6 +120,9 @@ $(TESTKFC): ../kfc.c
$(TESTREAL): test_real.c $(TESTREAL): test_real.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm $(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 $(BENCHKISS): benchkiss.c pstats.c
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
@@ -175,6 +180,8 @@ endif
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(SELFTEST) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(SELFTEST)
$(warning ======real FFT (type= $(KISSFFT_DATATYPE) )) $(warning ======real FFT (type= $(KISSFFT_DATATYPE) ))
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTREAL) @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))) $(warning ======timing test (type=$(KISSFFT_DATATYPE)))
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHKISS) -x $(NUMFFTS) -n $(NFFT) @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 @[ -x ./$(BENCHFFTW) ] && LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHFFTW) -x $(NUMFFTS) -n $(NFFT) || true
@@ -186,4 +193,4 @@ endif
# #
clean: 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
+162
View File
@@ -0,0 +1,162 @@
/*
* Copyright (c) 2003-2010, Mark Borgerding. All rights reserved.
* This file is part of KISS FFT - https://github.com/mborgerding/kissfft
*
* SPDX-License-Identifier: BSD-3-Clause
* See COPYING file for more information.
*/
/* Regression tests for size validation in the allocation functions:
* 1. kiss_fftnd_alloc must not overflow the product of the dimensions (an int):
* {65536,65536} wraps to 0 and {46342,46342} to a negative number, so
* the library reports a bogus size and accepts a buffer that is far too small
* 2. kiss_fft_alloc(0) and kiss_fftr_alloc(0) must fail
* 3. kiss_fft_next_fast_size must not overflow int for n above the largest
* representable 2,3,5-smooth number (2125764000)
*
* Huge allocations are never attempted: the nd tests only use the size query
* and a small caller-supplied buffer, and the nfft == 0 objects are never
* transformed.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include "kiss_fft.h"
#include "kiss_fftr.h"
#include "kiss_fftnd.h"
static int failures = 0;
static void fail(const char *msg, long a, long b)
{
printf("FAIL: %s (%ld,%ld)\n", msg, a, b);
++failures;
}
/* ---- 1. kiss_fftnd_alloc: dimension product overflow ---------------------- */
#define SMALL_BUF_SIZE (16u * 1024 * 1024) /* above the wrapped size, far below the real one */
#define BUF_ALIGN 64
static void test_nd_overflow(int d0, int d1)
{
int dims[2];
size_t len = 0, buflen;
double need; /* the tmpbuf alone needs this many bytes */
void *raw, *buf;
kiss_fftnd_cfg st;
dims[0] = d0;
dims[1] = d1;
need = (double)d0 * (double)d1 * (double)sizeof(kiss_fft_cpx);
/* size query: must report "too big" ((size_t)-1) or a size that really fits the data,
* not a wrapped-around number */
(void)kiss_fftnd_alloc(dims, 2, 0, NULL, &len);
if (len != (size_t)-1 && !((double)len >= need && (double)len <= 2 * need))
fail("kiss_fftnd_alloc size query reports a bogus size, dims", d0, d1);
/* a 16MB buffer is far too small for the transform and must be refused */
raw = malloc(SMALL_BUF_SIZE + BUF_ALIGN);
if (raw == NULL) {
fail("out of memory", d0, d1);
return;
}
buf = (void *)(((uintptr_t)raw + BUF_ALIGN - 1) & ~(uintptr_t)(BUF_ALIGN - 1));
buflen = SMALL_BUF_SIZE;
st = kiss_fftnd_alloc(dims, 2, 0, buf, &buflen);
if (st != NULL)
fail("kiss_fftnd_alloc accepted a 16MB buffer for dims", d0, d1);
free(raw);
}
/* ---- 2. nfft == 0 --------------------------------------------------------- */
static void test_zero_and_negative_size(void)
{
kiss_fft_cfg c;
kiss_fftr_cfg r;
c = kiss_fft_alloc(0, 0, NULL, NULL);
if (c != NULL)
fail("kiss_fft_alloc(0) did not return NULL", 0, 0);
kiss_fft_free(c);
r = kiss_fftr_alloc(0, 0, NULL, NULL);
if (r != NULL)
fail("kiss_fftr_alloc(0) did not return NULL", 0, 0);
kiss_fftr_free(r);
c = kiss_fft_alloc(-1, 0, NULL, NULL);
if (c != NULL)
fail("kiss_fft_alloc(-1) did not return NULL", -1, 0);
kiss_fft_free(c);
}
/* ---- 3. kiss_fft_next_fast_size ------------------------------------------ */
/* n has no prime factors other than 2, 3 and 5 */
static int is_smooth(long long n)
{
if (n < 1)
return 0;
while (n % 2 == 0)
n /= 2;
while (n % 3 == 0)
n /= 3;
while (n % 5 == 0)
n /= 5;
return n == 1;
}
/* smallest smooth number >= n, computed in 64 bits */
static long long ref_next_fast_size(long long n)
{
while (!is_smooth(n))
++n;
return n;
}
static void test_next_fast_size(void)
{
static const int spot[] = {1001, 1000001, 2000000000, 2100000001, 2125763999, 2125764000};
static const int huge[] = {2125764001, 2147483000, INT_MAX};
int n, i, bad = 0, firstn = 0;
for (n = 1; n <= 2000; ++n) {
if (kiss_fft_next_fast_size(n) != ref_next_fast_size(n)) {
if (!bad)
firstn = n;
++bad;
}
}
if (bad) {
printf("(%d of 2000 sizes in 1..2000 wrong)\n", bad);
fail("kiss_fft_next_fast_size differs from reference, first n", firstn, 0);
}
for (i = 0; i < (int)(sizeof(spot) / sizeof(spot[0])); ++i) {
int got = kiss_fft_next_fast_size(spot[i]);
if (got != ref_next_fast_size(spot[i]))
fail("kiss_fft_next_fast_size wrong, n", spot[i], got);
}
/* the next smooth number does not fit in an int: n itself (still a valid size) is returned */
for (i = 0; i < (int)(sizeof(huge) / sizeof(huge[0])); ++i) {
int got = kiss_fft_next_fast_size(huge[i]);
if (got != huge[i])
fail("kiss_fft_next_fast_size should return n when no larger fast size fits, n", huge[i], got);
}
}
int main(void)
{
test_nd_overflow(65536, 65536);
test_nd_overflow(46342, 46342);
test_zero_and_negative_size();
test_next_fast_size();
printf("%s\n", failures ? "FAIL" : "PASS");
return failures ? 1 : 0;
}