E3SM-Project / E3SM-Project/scorpio
Remove any instances of unintended "implicit save"s of variables
- Dominant language
- C++
- Stars
- 22
- Forks
- 20
- Avg merge
- 12d 19m
- Merged PRs (30d)
- 1
Description
@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
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.