Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stringlist): allocate string before assignment #1030

Merged
merged 2 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 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,11 @@ 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we say:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking len should report 0 if not allocated but should have verified. I'll make the change.

length = len(this%charstring)
end function strlen

end module CharacterStringModule
4 changes: 3 additions & 1 deletion 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
allocate (character(len=charcont%strlen()) :: string)
string = charcont
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be:

string(:) = charcont

but please verify

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion is to replace line 70 right?

else
string = ''
end if
!
return
Expand Down