Q(UrlTwo)Flags: avoid undefined behavior

Loading an enum with a value that isn't in the enum is undefined,
according to Clang's usan.

So when doing logical operations on QFlags<E>, don't go through
the QFlags(E) constructor, but via QFlags(QFlag) (=int) instead.

Apply the same change to QUrlTwoFlags.

Change-Id: I5f27e22c4d831482fcbba88b97cb124fb005e3fd
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Marc Mutz 2013-09-16 22:24:45 +02:00 committed by The Qt Project
parent 6272a816d1
commit 1248b1cbe2
2 changed files with 21 additions and 21 deletions

View File

@ -111,14 +111,14 @@ public:
Q_DECL_CONSTEXPR inline operator Int() const { return i; }
Q_DECL_CONSTEXPR inline QFlags operator|(QFlags f) const { return QFlags(Enum(i | f.i)); }
Q_DECL_CONSTEXPR inline QFlags operator|(Enum f) const { return QFlags(Enum(i | Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator^(QFlags f) const { return QFlags(Enum(i ^ f.i)); }
Q_DECL_CONSTEXPR inline QFlags operator^(Enum f) const { return QFlags(Enum(i ^ Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator&(int mask) const { return QFlags(Enum(i & mask)); }
Q_DECL_CONSTEXPR inline QFlags operator&(uint mask) const { return QFlags(Enum(i & mask)); }
Q_DECL_CONSTEXPR inline QFlags operator&(Enum f) const { return QFlags(Enum(i & Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator~() const { return QFlags(Enum(~i)); }
Q_DECL_CONSTEXPR inline QFlags operator|(QFlags f) const { return QFlags(QFlag(i | f.i)); }
Q_DECL_CONSTEXPR inline QFlags operator|(Enum f) const { return QFlags(QFlag(i | Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator^(QFlags f) const { return QFlags(QFlag(i ^ f.i)); }
Q_DECL_CONSTEXPR inline QFlags operator^(Enum f) const { return QFlags(QFlag(i ^ Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator&(int mask) const { return QFlags(QFlag(i & mask)); }
Q_DECL_CONSTEXPR inline QFlags operator&(uint mask) const { return QFlags(QFlag(i & mask)); }
Q_DECL_CONSTEXPR inline QFlags operator&(Enum f) const { return QFlags(QFlag(i & Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator~() const { return QFlags(QFlag(~i)); }
Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }

View File

@ -86,33 +86,33 @@ public:
inline QUrlTwoFlags &operator^=(E1 f) { i ^= f; return *this; }
inline QUrlTwoFlags &operator^=(E2 f) { i ^= f; return *this; }
Q_DECL_CONSTEXPR inline operator QFlags<E1>() const { return E1(i); }
Q_DECL_CONSTEXPR inline operator QFlags<E2>() const { return E2(i); }
Q_DECL_CONSTEXPR inline operator QFlags<E1>() const { return QFlag(i); }
Q_DECL_CONSTEXPR inline operator QFlags<E2>() const { return QFlag(i); }
Q_DECL_CONSTEXPR inline operator int() const { return i; }
Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(QUrlTwoFlags f) const
{ return QUrlTwoFlags(E1(i | f.i)); }
{ return QUrlTwoFlags(QFlag(i | f.i)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E1 f) const
{ return QUrlTwoFlags(E1(i | f)); }
{ return QUrlTwoFlags(QFlag(i | f)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E2 f) const
{ return QUrlTwoFlags(E2(i | f)); }
{ return QUrlTwoFlags(QFlag(i | f)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(QUrlTwoFlags f) const
{ return QUrlTwoFlags(E1(i ^ f.i)); }
{ return QUrlTwoFlags(QFlag(i ^ f.i)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E1 f) const
{ return QUrlTwoFlags(E1(i ^ f)); }
{ return QUrlTwoFlags(QFlag(i ^ f)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E2 f) const
{ return QUrlTwoFlags(E2(i ^ f)); }
{ return QUrlTwoFlags(QFlag(i ^ f)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(int mask) const
{ return QUrlTwoFlags(E1(i & mask)); }
{ return QUrlTwoFlags(QFlag(i & mask)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(uint mask) const
{ return QUrlTwoFlags(E1(i & mask)); }
{ return QUrlTwoFlags(QFlag(i & mask)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E1 f) const
{ return QUrlTwoFlags(E1(i & f)); }
{ return QUrlTwoFlags(QFlag(i & f)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E2 f) const
{ return QUrlTwoFlags(E2(i & f)); }
{ return QUrlTwoFlags(QFlag(i & f)); }
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator~() const
{ return QUrlTwoFlags(E1(~i)); }
{ return QUrlTwoFlags(QFlag(~i)); }
inline bool testFlag(E1 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
inline bool testFlag(E2 f) const { return (i & f) == f && (f != 0 || i == int(f)); }