From abc78dab1dd4fb0e9da99cd89ab2f9126d411cc4 Mon Sep 17 00:00:00 2001 From: Navyansh Kesarwani Date: Wed, 11 Jun 2025 12:26:34 +0530 Subject: [PATCH] fix(math/strided/special/dfloor): replace stack allocation with dynamic allocation in benchmark Replace fixed-size array declarations with dynamic memory allocation in the dfloor benchmark to prevent segmentation faults when processing large input sizes. The benchmark previously failed with segmentation faults for arrays with 1,000,000 elements due to stack memory limitations. Changes: - Replace stack-allocated arrays with malloc/free for x and y arrays - Add proper error handling for memory allocation failures - Add memory cleanup to prevent memory leaks - Add error checking in main benchmark loop Fixes: #7224 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../dfloor/benchmark/c/benchmark.length.c | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/math/strided/special/dfloor/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/math/strided/special/dfloor/benchmark/c/benchmark.length.c index dc6a43486daa..a802f7519c12 100644 --- a/lib/node_modules/@stdlib/math/strided/special/dfloor/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/math/strided/special/dfloor/benchmark/c/benchmark.length.c @@ -114,11 +114,24 @@ float rand_uniformf( float a, float b ) { */ static double benchmark( int iterations, int len ) { double elapsed; - double x[ len ]; - double y[ len ]; + double *x; + double *y; double t; int i; + // Allocate memory dynamically to avoid stack overflow + x = malloc( len * sizeof(double) ); + if ( x == NULL ) { + printf( "Error: failed to allocate memory for x array\n" ); + return -1.0; + } + y = malloc( len * sizeof(double) ); + if ( y == NULL ) { + printf( "Error: failed to allocate memory for y array\n" ); + free( x ); + return -1.0; + } + for ( i = 0; i < len; i++ ) { x[ i ] = rand_uniform( -10.0, 10.0 ); y[ i ] = 0.0; @@ -136,6 +149,9 @@ static double benchmark( int iterations, int len ) { if ( y[ 0 ] != y[ 0 ] ) { printf( "should not return NaN\n" ); } + // Free allocated memory + free( x ); + free( y ); return elapsed; } @@ -162,6 +178,10 @@ int main( void ) { count += 1; printf( "# c::%s:len=%d\n", NAME, len ); elapsed = benchmark( iter, len ); + if ( elapsed < 0.0 ) { + printf( "not ok %d benchmark failed - memory allocation error\n", count ); + continue; + } print_results( iter, elapsed ); printf( "ok %d benchmark finished\n", count ); }