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

Lukasz Marek lukasz.m.luki2 at gmail.com
Fri Mar 21 13:28:35 CET 2014


On 21 March 2014 02:33, Lukasz Marek <lukasz.m.luki2 at gmail.com> wrote:

> On 21.03.2014 02:24, wm4 wrote:
>
>> On Fri, 21 Mar 2014 02:18:10 +0100
>> Lukasz Marek <lukasz.m.luki2 at gmail.com> wrote:
>>
>>  +    intptr_t *tab = *(intptr_t**)tab_ptr;
>>>>>>>>>
>>>>>>>>
>>>>>>>> undefined behavior (strict aliasing violation)
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Can you tell how it is wrong?
>>>>>>>
>>>>>>
>>>>>> yes sure
>>>>>>
>>>>>> quoting the spec:
>>>>>>
>>>>>> 7 An object shall have its stored value accessed only by an lvalue
>>>>>> expression that has one of
>>>>>>      the following types:76)
>>>>>>      - a type compatible with the effective type of the object,
>>>>>>      - a qualified version of a type compatible with the effective
>>>>>> type of the object,
>>>>>>      - a type that is the signed or unsigned type corresponding to
>>>>>> the effective type of the
>>>>>>          object,
>>>>>>      - a type that is the signed or unsigned type corresponding to a
>>>>>> qualified version of the
>>>>>>          effective type of the object,
>>>>>>      - an aggregate or union type that includes one of the
>>>>>> aforementioned types among its
>>>>>>          members (including, recursively, a member of a subaggregate
>>>>>> or contained union), or
>>>>>>      - a character type.
>>>>>>
>>>>>>
>>>>>> i dont think intptr_t and some pointer are compatible types
>>>>>> but maybe iam missing something
>>>>>>
>>>>>
>>>>> tab_ptr is pointer to pointer to array data
>>>>> so this can be replaced by something similar and (maybe) less
>>>>> misleading
>>>>>
>>>>> intptr_t *tab = *(void**)tab_ptr;
>>>>>
>>>>> This line is copied from Nicolas implementation/original
>>>>> implementation.
>>>>>
>>>>> Still don't know what strict aliasing have anything to do here.
>>>>>
>>>>
>>>> This is simply undefined behavior according to C. Basically, you can't
>>>> derference a pointer that was casted from a different pointer type.
>>>> Unless one of the exceptions applies (like unions or char*).
>>>>
>>>> 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.
>
>
My point is:
- is casting void * to anything also relevant to strict aliasing?
- is this relevant at all as there is no second pointer that would alias
the same memory ?


More information about the ffmpeg-devel mailing list