Unidata / netcdf-fortran

Official GitHub repository for netCDF-Fortran libraries, which depend on the netCDF C library. Install the netCDF C library first.
Other
244 stars 98 forks source link

Fix Stack Overflow problem by moving large arrays from the stack to the heap #410

Closed Roy-KC closed 7 months ago

Roy-KC commented 1 year ago

Fixes needed to generate a clean build on Win32 platforms (all tests passed).

nf03_test4/f90tst_vars4.F90 failed with a "Stack Overflow" error. The Stack size on Windows is only 1 Mb. Several copies of the data array of size 655k end up on the stack causing the crash. This test actually uncovers a larger problem with an overloaded family of functions (see netcdf_expanded.F90 line 1950) where a large array is placed on the stack (defaultIntArray). This problem is compounded with the following call to RESHAPE: values(COLONS) = reshape(defaultIntArray(:), shape(values))

Because the RHS contains "values", the compiler chooses to create a temporary array (also on the stack). I made changes to the M4 files to correct the problem. I used M4 to generate "netcdf_expanded.F90" and "netcdf_eightbyte.F90". I hand-edited "netcdf4_eightbyte.F90" because I did not find a way to auto-generate the file.

The affected functions can easily be located by searching for "reshape".

I'll be happy to answer specific questions. Thank you for considering the PR.

Roy-KC commented 7 months ago

I renamed the PR because the defect is not Windows specific. It is bad practice to place large arrays on the stack. Windows platform uncovered the defect because it has a smaller stack size. Since the PR was posted a while ago, the changes are now marked "Outdated" (but they are still relevant).

WardF commented 7 months ago

Thank you; taking a look now, I appreciate your patience!