diff --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp index f9e34b21c800f..bb47f6b7a3292 100644 --- a/flang/runtime/derived.cpp +++ b/flang/runtime/derived.cpp @@ -188,8 +188,10 @@ void Finalize( SubscriptValue extent[maxRank]; const typeInfo::Value *bounds{comp.bounds()}; for (int dim{0}; dim < comp.rank(); ++dim) { - extent[dim] = bounds[2 * dim].GetValue(&descriptor).value_or(0) - - bounds[2 * dim + 1].GetValue(&descriptor).value_or(0) + 1; + SubscriptValue lb{bounds[2 * dim].GetValue(&descriptor).value_or(0)}; + SubscriptValue ub{ + bounds[2 * dim + 1].GetValue(&descriptor).value_or(0)}; + extent[dim] = ub >= lb ? ub - lb + 1 : 0; } StaticDescriptor staticDescriptor; Descriptor &compDesc{staticDescriptor.descriptor()};