[FFmpeg-devel] [PATCH 1/4] lavu: add simple array implementation

wm4 nfxjfg at googlemail.com
Fri Mar 21 14:03:34 CET 2014


On Fri, 21 Mar 2014 13:28:35 +0100
Lukasz Marek <lukasz.m.luki2 at gmail.com> wrote:

> >>>> So, this is not legal:
> >>>>
> >>>>         struct foo *foo_ptr;
> >>>>         void *void_ptr = foo_ptr;
> >>>>         *(void **)void_ptr = NULL;
> >>>>
> >>>
> >>> Is this also disallowed?:
> >>>
> >>> foo *foo_ptr;
> >>> void *tab_ptr = &foo_ptr;
> >>> *(intptr_t **)tab_ptr = NULL;
> >>>
> >>> As I said function takes void* but it is in fact void**
> >>>
> >>
> >> You dereference a foo** as intptr_t**. They are different types, so
> >> it's a strict aliasing violation,
> >>
> >
> > I cast void* to intptr_t** and dereference it.

Doesn't help. The _real_ type matters.

> >
> My point is:
> - is casting void * to anything also relevant to strict aliasing?

No. Casting to void*, you typically don't dereference the pointer, you
merely convert a pointer type to another pointer type. void* can't be
dereferenced, so there's never an aliasing problem.

But if you convert the void* pointer to a different pointer type (than
the original pointer type was) you might run into trouble, at least
theoretically.

> - is this relevant at all as there is no second pointer that would alias
> the same memory ?

Yes. The compiler could optimize your code into a NOP. Btw., this is
not a problem in practice yet, and av_freep() has the same problem, and
it wasn't miscompiled yet. But relying on undefined behavior is dirty,
and you might run into trouble with future compilers.



More information about the ffmpeg-devel mailing list