Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out-of-bounds accesses and modernize some line-number do loops #559

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions sorc/ncep_post.fd/MDL2SIGMA.f
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
!! 20-03-25 J MENG - remove grib1
!! 21-03-11 B Cui - change local arrays to dimension (im,jsta:jend)
!! 21-10-14 J MENG - 2D DECOMPOSITION
!! 2022-09-01 S Trahan - fixed bugs where extreme atmospheric conditions can cause out-of-bounds access
!!
!! USAGE: CALL MDL2P
!! INPUT ARGUMENT LIST:
Expand Down Expand Up @@ -277,8 +278,9 @@ SUBROUTINE MDL2SIGMA
AHF =D00
FAC =D00
DONEFSL1=.TRUE.
ELSEIF(T(I,J,NL1XF(I,J))<SPVAL &
& .AND. Q(I,J,NL1XF(I,J))<SPVAL)THEN
ELSEIF(NL1XF(I,J)<LP1) THEN ! must check the "<LP1" first; do not combine these into a single "if"
IF(T(I,J,NL1XF(I,J))<SPVAL &
& .AND. Q(I,J,NL1XF(I,J))<SPVAL)THEN
!== B =T(I,J,NL1XF(I,J)) !Marina Tsidulko Dec22, 2003
B =T(I,J,NL1XF(I,J))*(H1+D608*Q(I,J,NL1XF(I,J)))
DENOM=(ALPINT(I,J,NL1XF(I,J)+1)-ALPINT(I,J,NL1XF(I,J)-1))
Expand All @@ -287,6 +289,7 @@ SUBROUTINE MDL2SIGMA
/DENOM !Marina Tsidulko Dec22, 2003
FAC =H2*LOG(PMID(I,J,NL1XF(I,J)))
DONEFSL1=.TRUE.
END IF
END IF
!
if(DONEFSL1)FSL1(I,J)=(PNL1-PFSIGO)/(PFSIGO+PNL1) &
Expand Down Expand Up @@ -656,8 +659,9 @@ SUBROUTINE MDL2SIGMA
TSLDONE=.TRUE.
!
!
ELSEIF(T(I,J,NL1XF(I,J))<SPVAL &
& .AND. Q(I,J,NL1XF(I,J))<SPVAL)THEN
ELSEIF(NL1XF(I,J)<LP1) THEN ! must check the "<LP1" first; do not combine these into a single "if"
IF(T(I,J,NL1XF(I,J))<SPVAL &
& .AND. Q(I,J,NL1XF(I,J))<SPVAL)THEN
!
! INTERPOLATION BETWEEN LOWER AND UPPER BOUNDS.
!
Expand All @@ -674,6 +678,7 @@ SUBROUTINE MDL2SIGMA
/DENOMF !Marina Tsidulko Dec22, 2003
!
DONEFSL1=.TRUE.
ENDIF
ENDIF
!
IF(DONEFSL1)then
Expand Down
12 changes: 6 additions & 6 deletions sorc/ncep_post.fd/MISCLN.f
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ SUBROUTINE MISCLN

CALL FDLVL(ITYPEFDLVL,T7D,Q7D,U7D,V6D,P7D,ICINGFD,AERFD)
!
DO 10 IFD = 1,NFD
loop_10: DO IFD = 1,NFD
!
! FD LEVEL TEMPERATURE.
iget1 = IGET(059)
Expand Down Expand Up @@ -1328,7 +1328,7 @@ SUBROUTINE MISCLN
ENDIF
ENDIF

10 CONTINUE
END DO loop_10
DEALLOCATE(T7D,Q7D,U7D,V6D,P7D,ICINGFD,AERFD)
ENDIF

Expand Down Expand Up @@ -1807,7 +1807,7 @@ SUBROUTINE MISCLN

!
! LOOP OVER NBND BOUNDARY LAYERS.
DO 20 LBND = 1,NBND
boundary_layer_loop: DO LBND = 1,NBND
!
! BOUNDARY LAYER PRESSURE.
IF (IGET(067)>0) THEN
Expand Down Expand Up @@ -2118,7 +2118,7 @@ SUBROUTINE MISCLN
ENDIF
!
! END OF ETA BOUNDARY LAYER LOOP.
20 CONTINUE
END DO boundary_layer_loop
deallocate(OMGBND,PWTBND,QCNVBND)
!
! BEST LIFTED INDEX FROM BOUNDARY LAYER FIELDS.
Expand Down Expand Up @@ -2208,7 +2208,7 @@ SUBROUTINE MISCLN
ENDDO
ENDDO
!
DO 80 LBND = 1,NBND
loop_80: DO LBND = 1,NBND
CALL CALTHTE(PBND(ista,jsta,LBND),TBND(ista,jsta,LBND), &
QBND(ista,jsta,LBND),EGRID1)
!$omp parallel do private(i,j)
Expand All @@ -2223,7 +2223,7 @@ SUBROUTINE MISCLN
ENDIF
ENDDO
ENDDO
80 CONTINUE
ENDDO loop_80
!
DPBND = 0.
CALL CALCAPE(ITYPE,DPBND,P1D,T1D,Q1D,LB2,EGRID1, &
Expand Down
4 changes: 4 additions & 0 deletions sorc/ncep_post.fd/UPP_PHYSICS.f
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ END SUBROUTINE CALCAPE
!> 2021-09-03 | J Meng | Modified to add 0-3km CAPE/CINS, LFC, effective helicity, downdraft CAPE, dendritic growth layer depth, ESP
!> 2021-09-01 | E Colon | Equivalent level height index for RTMA
!> 2022-08-27 | S Trahan | Fixed bug in CALCAPE2 where extreme atmospheric conditions cause an out-of-bounds access
!> 2022-09-01 | S Trahan | Fixed another bug in CALCAPE2 where extreme atmospheric conditions cause an out-of-bounds access
!>
!> @author Russ Treadon W/NP2 @date 1993-02-10
SUBROUTINE CALCAPE2(ITYPE,DPBND,P1D,T1D,Q1D,L1D, &
Expand Down Expand Up @@ -1320,6 +1321,9 @@ SUBROUTINE CALCAPE2(ITYPE,DPBND,P1D,T1D,Q1D,L1D, &
THUNDER(I,J) = .FALSE.
ENDIF
ENDIF

! Limit LCL to prevent out-of-bounds accesses later
LCL(I,J) = max(min(LCL(I,J),LM-1),1)
WenMeng-NOAA marked this conversation as resolved.
Show resolved Hide resolved
ENDDO
ENDDO
!-----------------------------------------------------------------------
Expand Down