NOAA-GFDL / GFDL_atmos_cubed_sphere

The GFDL atmos_cubed_sphere dynamical core code
Other
56 stars 118 forks source link

In module_diag_hailcast.F90, KBAS=1 is possible, and this reads out of bounds a few lines later. #309

Closed SamuelTrahanNOAA closed 8 months ago

SamuelTrahanNOAA commented 8 months ago

Describe the bug

You will find this around line 790 or so in module_diag_hailcast.F90:

    DO k=1,nz
         ... more stuff ...
         IF ((cwitot .ge. 1.E-12) .and. (k .lt. KBAS)) THEN
            KBAS = k
         ENDIF
    ENDDO

It can set KBAS=1 when the loop reaches k=1.

Later:

       ELSE IF ((k.ge.KBAS+1).AND.(RWA_adiabat(k).ge.1.E-12)) THEN
          RWA_new(k) = RWA_adiabat(k)*(h1d(k)-h1d(k-1)) - RWA_new(k-1)
          IF (RWA_new(k).LT.0) RWA_new(k) = 0.
       ENDIF
...
    DO k=KBAS,nz
       RWA_new(k) = RWA_new(k) / (h1d(k)-h1d(k-1))
    ENDDO

If KBAS=1, then the first step of this loop will access h1d(k-1)

To Reproduce See https://github.com/ufs-community/ufs-weather-model/issues/2065 for a test case.

Expected behavior Don't read out-of-bounds

System Environment WCOSS2 Dogwood.

Additional context Test case fails on WCOSS2 dogwood but passes on Hera.

ywangwof commented 8 months ago

The changes look good to me.