[Development] Qt 5.2 header diff: QtXmlPatterns

Olivier Goffart olivier at woboq.com
Tue Nov 5 13:00:01 CET 2013


On Tuesday 05 November 2013 12:24:32 Marc Mutz wrote:
> On Tuesday, November 05, 2013 01:07:32 Thiago Macieira wrote:
> > -            return (void *)qptrdiff(data);
> > +            char *null = 0;
> > +            return null + qptrdiff(data);
> 
> Since this is equivalent to
> 
>     return 0[qptrdiff(data)];
> 
> isn't that dereferencing the nullptr and therefore undefined behaviour?

That's indeed an undefined behaviour.
The compiler is allowed to optimize this function by removing it.

> What's wrong with
> 
>    reinterpret_cast<void*>(qptrdiff(data));

Yes, that's how it should be.

> Or simply
> 
>   return ptr;
> 
> which forms a union with `data`?

No, that does not work.  ptr is never actually used, and can never be used.
That's because storing the pointer is not done using ptr,  but it is done with
 data = qptrdiff(somePointer);    (indirectly via 
QAbstractXmlNodeModel::createIndex)

As a result, if one store for example the pointer 0xabcdef00 on a 32bit big 
endian machine, it would result in memory:
  0x00000000abcdef00
with data = 0x00000000abcdef00  as expected,  but  ptr = 0

-- 
Olivier

Woboq - Qt services and support - http://woboq.com - http://code.woboq.org
  




More information about the Development mailing list