Menu

#33 Optional argument not checked before use

Development v3.x
closed-fixed
5
2014-12-01
2014-11-12
No

While working on getting the PGI compiler to work with pFUnit, I believe I found a bug in the code. This bug caused a segmentation violation in the assertBasicSuite self-tests when built with the PGI compiler.

Consider the following subroutine in source/AssertBasic.F90

~~~~
subroutine assertTrue_(condition, message, location)
logical, intent(in) :: condition
character(len=*), optional, intent(in) :: message
type (SourceLocation), optional, intent(in) :: location
character(len=:), allocatable :: message_
type (SourceLocation) :: location_

  if(present(message))then
     message_ = message
  else
     message_ = NULL_MESSAGE
  end if

  if(present(location))then
     location_ = location
  else
     location_ = UNKNOWN_SOURCE_LOCATION
  end if

! PGI – The code below (#if 0) is a bug.
! PGI - Changed “message” and “location” to “message_” and “location_” because It's
! PGI - illegal to reference a non-present optional argument.

if 0

  if (.not. condition) call throw(trim(message), location)

else

  if (.not. condition) call throw(trim(message_), location_)

endif

end subroutine assertTrue_

~~~~
As I annotated in the code above, the use of optional arguments “message” and “location” are illegal in this manner when they are not “present”.

Also it just seems odd that we would go out of our way to create “message_” and “location_” and not use them. I imagine the use of “message” and “location” in the call to throw() was a typo.

Anyway, this restriction is in the F2003 Language Specification (section 12.4.1.6 “Restrictions on dummy arguments not present”) items (1) and (4). There’s similar restrictions in the F2008 specification (section 12.5.2.12).

It looks like there’s a similar problem with the assertEqualString_ subroutine in the same file (there’s a call to throw() that references optional argument message without checking whether it is present).

By the way, so far I’ve successfully compiled/ran 11/17 of the serial self-tests with my development PGI compiler. I have not tried the MPI self-tests yet. We are expecting the upcoming PGI 15.1 compiler to build/run all of the pFUnit self-tests.

[ML]

Discussion

  • Michael L. Rilee

    Fixed by TLC in AssertBasic.F90. Checked generated code, but found no similar error.

     
  • Michael L. Rilee

    • status: open --> closed-fixed
     
  • Michael L. Rilee

    All known defects associated with misuse of optional arguments have been corrected.

     

Log in to post a comment.

MongoDB Logo MongoDB