scorpio icon indicating copy to clipboard operation
scorpio copied to clipboard

Remove any instances of unintended "implicit save"s of variables

Open jayeshkrishna opened this issue 3 years ago • 0 comments

@ndk mentioned this issue on slack. In Fortran, variables initialized using an initialization expression implicitly assume the save attribute (essentially behaving like static variables in C) and this behavior can have unintended consequences. In the code from piolib.F90 below the value of imode is "saved" across multiple function calls (e.g. If the first call provides the "mode" argument to PIO_openfile() and the second omits it, the second call will use the value of imode set by the first call, not imode=0)

  integer function PIO_openfile(iosystem, file, iotype, fname,mode) result(ierr)
    integer :: imode=0, i, nl
    if(present(mode)) imode = mode
    ierr = PIOc_openfile( iosystem%iosysid, file%fh, iotype, cfname, imode)

The fix is to convert the initialization expression to an assignment (since we want imode to be set to 0 for every call to PIO_openfile())

  integer function PIO_openfile(iosystem, file, iotype, fname,mode) result(ierr)
    integer :: imode, i, nl
    imode = 0
    if(present(mode)) imode = mode
    ierr = PIOc_openfile( iosystem%iosysid, file%fh, iotype, cfname, imode)

We need to look through the Fortran interface and find all instances of unintended saves of variables and fix them.

Also see related discussion in confluence - https://acme-climate.atlassian.net/wiki/spaces/NGDNA/pages/2462515522/Global+search+on+fortran+source+files+for+potential+cases+of+implicit+save+bug?focusedCommentId=2472640956#comment-2472640956

jayeshkrishna avatar Mar 16 '21 12:03 jayeshkrishna