Details
-
Bug
-
Resolution: Out of scope
-
P3: Somewhat important
-
None
-
5.12.0
Description
QT_RESTRICTED_CAST_FROM_ASCII doesn't prevent casting string into QVariant as it does for QString. Customer reports:
"Consider the following code snippet:
====================================
#include <QtCore/QVariant>
void foo(const char* p)
{
QString s1("ABCDE");
QString s2(p);
QVariant v1("ABCDE");
QVariant v2(p);
}
====================================
If I compile it with -DQT_NO_CAST_FROM_ASCII I get 4 compile errors, one from each of constructor calls for s1, s2, v1, v2. And that's what is expected to happen.
However, if I compile it with -DQT_RESTRICTED_CAST_FROM_ASCII I only get an error for s2. The fact that I don't get an error for v2 here means that the protection that the macro QT_RESTRICTED_CAST_FROM_ASCII
offers is broken. That's a pity because the macro would help to simplify our code a lot.
The problem is that QVariant only checks for the macro QT_NO_CAST_FROM_ASCII as in qvariant.h, line 223ff:
#ifndef QT_NO_CAST_FROM_ASCII
QT_ASCII_CAST_WARN QVariant(const char *str);
#endif
IMHO it would make sense to check both macros here instead:
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
QT_ASCII_CAST_WARN QVariant(const char *str);
#endif
That would mean that I would have to write
QVariant v1(QString("ABCDE"));
instead of just
QVariant v1("ABCDE");
to make all lines with string literals compile, but IMHO that's better than to have no protection against unwanted conversions.
A nicer solution would be to use the template magic with character arrays as in qstring.h, line 685ff:
#if defined(QT_RESTRICTED_CAST_FROM_ASCII)
template <int N>
inline QString(const char (&ch;)[N])
....
#endif
But that might be too much effort for not enough benefit.
Btw, there are other files that only check for QT_NO_CAST_FROM_ASCII but not for QT_RESTRICTED_CAST_FROM_ASCII, for example:
qbytearray.h
qcbormap.h
qcborstream.h
qcborvalue.h
qjsonvalue.h
qstringbuilder.h"
Attachments
For Gerrit Dashboard: QTBUG-72367 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
250753,2 | Make QVariant(const char *) private on QT_RESTRICTED_CAST_FROM_ASCII | dev | qt/qtbase | Status: ABANDONED | 0 | 0 |