Bugs with pointers in structures
Posted: Mon May 02, 2016 4:09 am
I have found at least two bugs in the code cause by incorrect use of pointers.
According to the Fortran 95 standard, the association status of a pointer is initially undefined. Nullify or an initialisation to NULL() can be used to set it.
The array VKPT_SHIFT in nonl_struct is nullified in NONL_ALLOC, allowing the associated test in SPHER to work fine generally. But there is no nullify in NONL_ALLOC_SPHPRO, so when SPHER is called in response to e.g. setting RWIGS, the association status may be undefined.
Similarly, PSPNL_SPLINE in the potcar type can be of undefined association inside the P argument to SPHER, giving the wrong result to the associated test.
It seems that ifort takes the time to nullify pointers on creation, so this bug is hidden. However, gfortran does not (or not any more, as some previous versions apparently used to). So my gfortran-compiled binary crashes whenever I set RWIGS in INCAR.
The fixes are either appropriate nullify statements or initialisation to NULL(). [I have done one of each to fix the problem: NULLIFY(NONL_S%VKPT_SHIFT) in NONL_ALLOC_SPHPRO in nonl.F and REAL(q),POINTER :: PSPNL_SPLINE(:,:,:) => NULL() in TYPE potcar in pseudo.F.]
According to the Fortran 95 standard, the association status of a pointer is initially undefined. Nullify or an initialisation to NULL() can be used to set it.
The array VKPT_SHIFT in nonl_struct is nullified in NONL_ALLOC, allowing the associated test in SPHER to work fine generally. But there is no nullify in NONL_ALLOC_SPHPRO, so when SPHER is called in response to e.g. setting RWIGS, the association status may be undefined.
Similarly, PSPNL_SPLINE in the potcar type can be of undefined association inside the P argument to SPHER, giving the wrong result to the associated test.
It seems that ifort takes the time to nullify pointers on creation, so this bug is hidden. However, gfortran does not (or not any more, as some previous versions apparently used to). So my gfortran-compiled binary crashes whenever I set RWIGS in INCAR.
The fixes are either appropriate nullify statements or initialisation to NULL(). [I have done one of each to fix the problem: NULLIFY(NONL_S%VKPT_SHIFT) in NONL_ALLOC_SPHPRO in nonl.F and REAL(q),POINTER :: PSPNL_SPLINE(:,:,:) => NULL() in TYPE potcar in pseudo.F.]