diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp index ba3acd8bba5f8..68ecaa19d2d5d 100644 --- a/flang/lib/Lower/ConvertExprToHLFIR.cpp +++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp @@ -1219,8 +1219,11 @@ struct BinaryOp> { fir::FirOpBuilder &builder, const Op &, hlfir::Entity string, hlfir::Entity length) { + // The input length may be a user input and needs to be sanitized as per + // Fortran 2018 7.4.4.2 point 5. + mlir::Value safeLength = fir::factory::genMaxWithZero(builder, loc, length); return hlfir::EntityWithAttributes{ - builder.create(loc, string, length)}; + builder.create(loc, string, safeLength)}; } static void genResultTypeParams(mlir::Location, fir::FirOpBuilder &, hlfir::Entity, diff --git a/flang/test/Lower/HLFIR/array-ctor-character.f90 b/flang/test/Lower/HLFIR/array-ctor-character.f90 index 7304eb24a647f..85e6ed27fe077 100644 --- a/flang/test/Lower/HLFIR/array-ctor-character.f90 +++ b/flang/test/Lower/HLFIR/array-ctor-character.f90 @@ -86,3 +86,17 @@ subroutine takes_char(c) print *, "expect: abcdef" call test_dynamic_length() end + +subroutine test_set_length_sanitize(i, c1) + integer(8) :: i + character(*) :: c1 + call takes_char([character(len=i):: c1]) +end subroutine +! CHECK-LABEL: func.func @_QPtest_set_length_sanitize( +! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare {{.*}}Ec1 +! CHECK: %[[VAL_9:.*]]:2 = hlfir.declare {{.*}}Ei +! CHECK: %[[VAL_25:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref +! CHECK: %[[VAL_26:.*]] = arith.constant 0 : i64 +! CHECK: %[[VAL_27:.*]] = arith.cmpi sgt, %[[VAL_25]], %[[VAL_26]] : i64 +! CHECK: %[[VAL_28:.*]] = arith.select %[[VAL_27]], %[[VAL_25]], %[[VAL_26]] : i64 +! CHECK: %[[VAL_29:.*]] = hlfir.set_length %[[VAL_6]]#0 len %[[VAL_28]] : (!fir.boxchar<1>, i64) -> !hlfir.expr>