Skip to content

[flang] string transfering may have an access out-of-bounds #160995

@foxtran

Description

@foxtran

Assume that one has the following code:

subroutine f_c_character(rhs, lhs, len)
   character(kind=c_char), intent(out) :: lhs(*)
   character(len=*), intent(in) :: rhs
   integer, intent(in) :: len
   integer :: length
   length = min(len-1, len_trim(rhs))

   lhs(1:length+1) = transfer(rhs(1:length), lhs(1:length)) // c_null_char

end subroutine f_c_character

Here is an assignment between two strings with possible truncation if rhs has larger length than lhs.

However, flang runtime says that the Assign: mismatching element counts in array assignment

Here is an example with current master where the error occurs:
https://godbolt.org/z/bnacoM958

gfortran + ASan does not give any errors (except leaking).

This code works fine:

subroutine f_c_character(rhs, lhs, len)
   character(kind=c_char), intent(out) :: lhs(*)
   character(len=*), intent(in) :: rhs
   integer, intent(in) :: len
   integer :: length
   length = min(len-1, len_trim(rhs))

   lhs(1:length) = transfer(rhs(1:length), lhs(1:length))
   lhs(length+1:length+1) = c_null_char

end subroutine f_c_character

Metadata

Metadata

Assignees

No one assigned

    Labels

    flangFlang issues not falling into any other categoryquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions