[Qt-interest] Undefined behavior in QString::toStdWString() ?

Andreas Pakulat apaku at gmx.de
Thu Dec 11 16:03:52 CET 2008


On 11.12.08 15:45:58, Robert Hairgrove wrote:
> Here is the definition of QString::toStdWString() taken from the header 
> file qstring.h:
> 
> === begin code ===
> // Note: QStdWString is a typedef for std::wstring
> # ifndef QT_NO_STL_WCHAR
> inline QStdWString QString::toStdWString() const
> {
>      QStdWString str;
>      str.resize(length());
> 
> #if defined(_MSC_VER) && _MSC_VER >= 1400
>      // VS2005 crashes if the string is empty
>      if (!length())
>          return str;
> #endif
> 
>      str.resize(toWCharArray(&(*str.begin())));
>      return str;
> }
> === end code ===
> 
> This line:
>      str.resize(toWCharArray(&(*str.begin())));
> 
> Isn't this going to cause UDB if str is empty? Because we dereference 
> the iterator returned from begin() which, like dereferencing end(), is 
> not allowed.

Why is dereferencing begin() not allowd? begin() is different from end() as
it always points to the first element in your collection. end() however
always points _behind_ the last element, thats why you can't dereference it
usually.

Andreas

-- 
You are going to have a new love affair.



More information about the Qt-interest-old mailing list