Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bug: nc_get_variable_info segfaults if you call with dim lengths onlyΒ #874

@hkershaw-brown

Description

@hkershaw-brown

πŸ› Your bug may already be reported!
🐜

Describe the bug

  1. call nc_get_variable_info(ncid, varname, dimlens=dimlens)
  2. What was the expected outcome?
    dimlens returns the length of each dimension in the variable
  3. What actually happened?
    segfault

Error Message

forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source             
libpthread-2.31.s  00007FA20796C8C0  Unknown               Unknown  Unknown
test_variable_inf  0000000000575CAE  netcdf_utilities_        2206  netcdf_utilities_mod.f90
test_variable_inf  000000000064C880  MAIN__                     33  test_variable_info.f90
test_variable_inf  000000000040EB5D  Unknown               Unknown  Unknown
libc-2.31.so       00007FA203E5929D  __libc_start_main     Unknown  Unknown
test_variable_inf  000000000040EA8A  Unknown               Unknown  Unknown

Which model(s) are you working with?

mpas (refactor) , but could be any code using nc_get_variable_info

Version of DART

Which version of DART are you using?
v11.11.0

Have you modified the DART code?

No,
call nc_get_variable_info(ncid, varname, ndims=ndims, dimlens=dimlens) is ok.
call nc_get_variable_info(ncid, varname, dimlens=dimlens) is not ok.

here is a reproducer:

use netcdf_utilities_mod, only: nc_get_variable_info
use utilities_mod, only : initialize_utilities, finalize_utilities
use netcdf, only: nf90_open, nf90_close, nf90_nowrite

implicit none

integer :: ncid
integer, parameter :: dimlen_size = 5
integer :: dimlens(dimlen_size)  ! Array to hold dimension lengths
integer :: ndims
integer :: ret
character(len=256) :: filename
character(len=256) :: varname

call initialize_utilities()
filename = "test_file.nc"
varname = "temperature"
dimlens = -1

ret = nf90_open(filename, nf90_nowrite, ncid)
if (ret /= 0) then
    print *, "Error opening file:", filename
    stop
end if

! Call nc_get_variable with ndims and dimlens
call nc_get_variable_info(ncid, varname, ndims=ndims, dimlens=dimlens)
print*, 'ndims', ndims, 'dimlens', dimlens

! Call nc_get_variable_info with only dimlens
call nc_get_variable_info(ncid, varname, dimlens=dimlens)

! Print the dimension lengths
print*, "Dimension lengths for variable:", varname
print*, dimlens

! Close the NetCDF file
ret = nf90_close(ncid)
if (ret /= 0) then
    print *, "Error closing file:", filename
    stop
end if

call finalize_utilities()

end program test_nc_get_variable_info

test_netcdf.nc : ncgen -o test_file.nc test_file.cdl

cat test_file.cdl 
netcdf test_file {
dimensions:
    time = 1 ; // One timestep
    lat = 10 ;
    lon = 10 ;
variables:
    float temperature(time, lat, lon) ;
        temperature:units = "K" ;
        temperature:long_name = "Temperature" ;
    float pressure(time, lat, lon) ;
        pressure:units = "hPa" ;
        pressure:long_name = "Pressure" ;
    double time(time) ;
        time:units = "hours since 2000-01-01 00:00:00" ;
        time:calendar = "gregorian" ;
    float lat(lat) ;
        lat:units = "degrees_north" ;
        lat:long_name = "Latitude" ;
    float lon(lon) ;
        lon:units = "degrees_east" ;
        lon:long_name = "Longitude" ;
data:
    lat = -90, -70, -50, -30, -10, 10, 30, 50, 70, 90 ;
    lon = -180, -140, -100, -60, -20, 20, 60, 100, 140, 180 ;
    time = 0 ;
    temperature = 
      280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
      290, 291, 292, 293, 294, 295, 296, 297, 298, 299,
      300, 301, 302, 303, 304, 305, 306, 307, 308, 309,
      310, 311, 312, 313, 314, 315, 316, 317, 318, 319,
      320, 321, 322, 323, 324, 325, 326, 327, 328, 329,
      330, 331, 332, 333, 334, 335, 336, 337, 338, 339,
      340, 341, 342, 343, 344, 345, 346, 347, 348, 349,
      350, 351, 352, 353, 354, 355, 356, 357, 358, 359,
      360, 361, 362, 363, 364, 365, 366, 367, 368, 369,
      370, 371, 372, 373, 374, 375, 376, 377, 378, 379 ;
}
[hkershaw:work](main) > lldb ./test_variable_info
(lldb) target create "./test_variable_info"
Current executable set to '/Users/hkershaw/DART/Bugs/netcdf_utils/DART/models/mpas_atm/work/test_variable_info' (arm64).
(lldb) r 
Process 21621 launched: '/Users/hkershaw/DART/Bugs/netcdf_utils/DART/models/mpas_atm/work/test_variable_info' (arm64)

 --------------------------------------
 Starting ... at YYYY MM DD HH MM SS = 
                 2025  5  9 16 27 26
 --------------------------------------

  set_nml_output Echo NML values to log file only
 ndims           3 dimlens          10          10           1           0           0
Process 21621 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x000000010024c888 test_variable_info`__netcdf_utilities_mod_MOD_nc_get_variable_info at netcdf_utilities_mod.f90:2206:61
   2203	endif
   2204	
   2205	if (present(   ndims)) ndims             = myndims
-> 2206	if (present( dimlens)) dimlens(1:ndims)  = mydimlens(1:ndims)
   2207	if (present(dimnames)) dimnames(1:ndims) = mydimnames(1:ndims)
   2208	
   2209	end subroutine nc_get_variable_info
Target 0: (test_variable_info) stopped.

nc_get_variable_info assuming ndims argument is present, same issue for dimnames only.

if (present( dimlens)) dimlens(1:ndims) = mydimlens(1:ndims)

Build information

Please describe:
mac M gfortran (hence lldb to get a backtrace), also ifort on Derecho (/glade/derecho/scratch/hkershaw/DART/Bugs/netcdf_util/models/mpas_atm/work)

Metadata

Metadata

Labels

BugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions