Skip to content

Commit

Permalink
fix(stringlist): allocate string before assignment (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjreno authored Sep 2, 2022
1 parent ddb3a55 commit 94fc2f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Utilities/CharString.f90
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module CharacterStringModule
procedure, pass(rhs) :: character_eq_charstring
procedure, pass(lhs) :: charstring_eq_character
procedure :: write_unformatted
procedure :: strlen
generic :: assignment(=) => assign_to_charstring, assign_from_charstring
generic :: operator(==) => character_eq_charstring, charstring_eq_character
! not supported by gfortran 5 and 6
Expand Down Expand Up @@ -100,4 +101,15 @@ subroutine write_unformatted(this, unit, iostat, iomsg)
end if
end subroutine write_unformatted

function strlen(this) result(length)
class(CharacterStringType), intent(in) :: this
integer :: length

if (allocated(this%charstring)) then
length = len(this%charstring)
else
length = 0
end if
end function strlen

end module CharacterStringModule
6 changes: 4 additions & 2 deletions src/Utilities/StringList.f90
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ function GetStringFromList(list, indx) result(string)
class(*), pointer :: obj
type(CharacterStringType), pointer :: charcont
!
string = ''
obj => list%GetItem(indx)
charcont => CastAsCharacterStringType(obj)
if (associated(charcont)) then
string = charcont
allocate (character(len=charcont%strlen()) :: string)
string(:) = charcont
else
string = ''
end if
!
return
Expand Down

0 comments on commit 94fc2f9

Please sign in to comment.