[petsc-dev] MatCreateSubmatrices causes invalid memory write access in fortran

Tim Steinhoff kandanovian at gmail.com
Thu Apr 19 03:52:08 CDT 2018


Hong,
we are fine with that. Thanks!
Volker

2018-04-18 17:32 GMT+02:00 Hong <hzhang at mcs.anl.gov>:
> Tim :
>>
>> Hi all,
>>
>> with PETSc v3.9 there seems to be a out of bounds write in the fortran
>> interface of MatCreateSubmatrices.
>>
>> https://bitbucket.org/petsc/petsc/src/59479656cffe26dd9a9d446f6d00dfe5e3168168/src/mat/interface/ftn-custom/zmatrixf.c?at=maint&fileviewer=file-view-default#zmatrixf.c-609
>>
>> The reference mentions that in fortran one should pass a preallocated
>> array for submat with at least the size of m (actually it's probably a
>> typo and should be n)
>>
>> http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatCreateSubMatrices.html
>>
>> Then n+1 entries are copied into that array, where the last entry
>> seems to be some unneeded dummy (as mentioned).
>> for (i=0; i<=*n; i++) { /* lsmat[*n] might be a dummy matrix for
>> saving data struc */
>>   smat[i] = lsmat[i];
>> }
>> This leads to some bad errors afterwards, when that array is only of
>> the size of n as recommended.
>
>
> We need this extra space for reuse -- I agree it makes Fortran API odd.
>
> I made following fix to the help manual:
> diff --git a/src/mat/interface/matrix.c b/src/mat/interface/matrix.c
>
>     When finished using the submatrices, the user should destroy
> -   them with MatDestroyMatrices().
> +   them with MatDestroySubMatrices().
>
>     Fortran Note:
>     The Fortran interface is slightly different from that given below; it
> -   requires one to pass in  as submat a Mat (integer) array of size at
> least m.
> +   requires one to pass in  as submat a Mat (integer) array of size at
> least n+1.
>
> If you agree, I'll push it to the petsc released version.
>
> Hong
>


More information about the petsc-dev mailing list