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

Hong hzhang at mcs.anl.gov
Wed Apr 18 10:32:26 CDT 2018


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/59479656cffe26dd9a9d446f6d00df
> e5e3168168/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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20180418/139d352f/attachment.html>


More information about the petsc-dev mailing list