[petsc-dev] Need help with Fortran code/compiler issue

Barry Smith bsmith at mcs.anl.gov
Fri Apr 8 14:17:15 CDT 2016


   Thanks, I've done it.

> On Apr 8, 2016, at 3:23 AM, Lawrence Mitchell <lawrence.mitchell at imperial.ac.uk> wrote:
> 
>> 
>> On 7 Apr 2016, at 19:46, Barry Smith <bsmith at mcs.anl.gov> wrote:
>> 
>> 
>> src/sys/classes/bag/f2003-src/fsrc/bagenum.F has the code fragment
>> 
>>    Subroutine PetscBagRegisterEnum(bag,addr,FArray,def,n,h,ierr)
>>     use,intrinsic :: iso_c_binding
>>     implicit none
>> 
>>     PetscBag   bag
>>     character(*)                n,h
>>     character(*)                FArray(*)
>>     PetscEnum                   :: def
>>     PetscErrorCode,intent(out)  :: ierr
>>     PetscReal addr(*)
>> 
>>     Type(C_Ptr),Dimension(:),Pointer :: CArray
>>     character(kind=c_char),pointer   :: nullc => null()
>>     PetscInt   :: i,Len
>>     Character(kind=C_char,len=256),Dimension(:),Pointer::list1
>> 
>>     do i=1,256
>>       if (len_trim(Farray(i)) .eq. 0) then
>>         Len = i-1
>>         goto 100
>>       endif
>>       if (len_trim(Farray(i)) .gt. 255) then
>>         ierr = PETSC_ERR_ARG_OUTOFRANGE
>>         return
>>       endif
>>     enddo
>>     ierr = PETSC_ERR_ARG_OUTOFRANGE
>>     return
>> 
>> 100  continue
>> 
>>     Allocate(list1(Len),stat=ierr);
>>     Allocate(CArray(Len+1),stat=ierr)
>>     do i=1,Len
>>        list1(i) = trim(FArray(i))//C_NULL_CHAR
>>     enddo
>> 
>>     CArray = (/(c_loc(list1(i)),i=1,Len),c_loc(nullc)/)
>>     call PetscBagRegisterEnumPrivate(bag,addr,CArray,def,n,h,ierr)
>>     DeAllocate(CArray)
>>     DeAllocate(list1)
>>     End Subroutine
>> 
>> which produces the following warning with one Fortran compiler:
>> 
>> /Users/petsc/petsc.clone-4/src/sys/objects/f2003-src/fsrc/optionenum.F:35:0:
>> 
>>      CArray = (/(c_loc(list1(i)),i=1,Len),c_loc(nullc)/)
>> ^
>> Warning: 'list1.data' may be used uninitialized in this function [-Wmaybe-uninitialized]
>> 
>> Do any Fortran programmers know if the code is correct or if this is an incorrect warning from the compiler?
> 
> 
> This is occurring because the return value from allocate(list1(Len),stat=ierr) is not being checked and so list1 may be NULL later on.
> 
> This squashes the warning for me:
> 
> Allocate(list1(Len),stat=ierr)
> if (ierr .ne. 0) then
>    ierr = PETSC_ERR_MEM
>    return
> endif
> 
> One should presumably do the same after the allocation of CArray as well.
> 
> It looks like the same change would apply in optionenum.F
> Cheers,
> 
> Lawrence




More information about the petsc-dev mailing list