diff --git a/flang/lib/Evaluate/variable.cpp b/flang/lib/Evaluate/variable.cpp --- a/flang/lib/Evaluate/variable.cpp +++ b/flang/lib/Evaluate/variable.cpp @@ -204,9 +204,11 @@ *ubi = *length; } if (lbi && literal) { - CHECK(*ubi >= *lbi); auto newStaticData{StaticDataObject::Create()}; - auto items{*ubi - *lbi + 1}; + auto items{0}; // If the lower bound is greater, the length is 0 + if (*ubi >= *lbi) { + items = *ubi - *lbi + 1; + } auto width{(*literal)->itemBytes()}; auto bytes{items * width}; auto startByte{(*lbi - 1) * width}; diff --git a/flang/test/Semantics/resolve49.f90 b/flang/test/Semantics/resolve49.f90 --- a/flang/test/Semantics/resolve49.f90 +++ b/flang/test/Semantics/resolve49.f90 @@ -17,6 +17,7 @@ end type character :: a(10) character :: b(5) + character :: c(0) integer :: n n = 3 b = a(n:7) @@ -26,6 +27,7 @@ a(n+3:) = b a(:n+2) = b n = iachar(1_'ABCDEFGHIJ'(1:1)) + c = 'ABCDEFGHIJ'(1:0) end ! Test pointer assignment with bounds