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

Robert Hairgrove evorgriahr at hispeed.ch
Thu Dec 11 15:45:58 CET 2008


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.



More information about the Qt-interest-old mailing list