[petsc-dev] Function pointer conversion: undefined behavior

Jed Brown jedbrown at mcs.anl.gov
Fri Jan 25 10:42:04 CST 2013


On Fri, Jan 25, 2013 at 10:26 AM, Karl Rupp <rupp at mcs.anl.gov> wrote:

> Hi,
>
> the Nightly test results improved slightly:
>
>   http://krupp.iue.tuwien.ac.at/**petsc-test/<http://krupp.iue.tuwien.ac.at/petsc-test/>
>
> However, the custom Fortran stuff in C89 logs here:
>
> http://ftp.mcs.anl.gov/pub/**petsc/nightlylogs/build_arch-**
> linux-c89_thrash.log<http://ftp.mcs.anl.gov/pub/petsc/nightlylogs/build_arch-linux-c89_thrash.log>
>
> seem to be quite hard to fix:
>    warning: ISO C forbids conversion of function pointer
>             to object pointer type [-pedantic]
>

Yup, this problem is well-known. I've been removing them as it makes sense,
but since we've never used an architecture where void(*)(void) has a
different size than void*, it hasn't been urgent to eradicate every last
instance. That said, we're nearly there with the
PetscObjectSetFortranCallback interface I put in two weeks ago (there are
just a few places still using the old way, then we can remove this code).

I think there are a couple other places, maybe in the MPI stuff, where
function pointers and data pointers are conflated, though I think they are
also easy enough to replace.


>
> These warnings are caused by assignments of the form
>   void * ptr = f;
> where f is a function pointer argument:
>   void (PETSC_STDCALL *f)(TS*,double*,Vec*,Vec*,**void*,PetscErrorCode*)
> A similar warning is obtained when assigning an instance of void* to such
> a function pointer.
>
> As an example, let's take the following function in
> mat/matfd/ftn-custom/**zfdmatrixf.c:
>
> void PETSC_STDCALL matfdcoloringsetfunctionts_(
>    MatFDColoring *fd,
>    void (PETSC_STDCALL *f)(TS*,double*,Vec*,Vec*,**void*,PetscErrorCode*))
> {
>   (*fd)->ftn_func_pointer = (void*) f;
>   ...
> }
>
> A look at MatFDColoring shows
>   struct  _p_MatFDColoring{
>     ...
>     void    *ftn_func_pointer,*ftn_func_**cntx;
>   };
> so the only fix I can think of is to replace void* with e.g.
>   void (*ftn_func_pointer)(void);
> in order to remain in the 'function pointer world'. This is, nevertheless,
> still fairly ugly. Does anyone have a better solution for this?
>
> Best regards,
> Karli
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20130125/f22c29df/attachment.html>


More information about the petsc-dev mailing list