From 1248b1cbe238616ab3a1dd79f65f76f92d256a6e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 16 Sep 2013 22:24:45 +0200 Subject: 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, 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 Reviewed-by: Thiago Macieira --- src/corelib/global/qflags.h | 16 ++++++++-------- src/corelib/io/qurl.h | 26 +++++++++++++------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/corelib/global/qflags.h b/src/corelib/global/qflags.h index 5b7edbafa6..dd4222b89f 100644 --- a/src/corelib/global/qflags.h +++ b/src/corelib/global/qflags.h @@ -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; } diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index 637eac87b6..76e3c0f8ca 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -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() const { return E1(i); } - Q_DECL_CONSTEXPR inline operator QFlags() const { return E2(i); } + Q_DECL_CONSTEXPR inline operator QFlags() const { return QFlag(i); } + Q_DECL_CONSTEXPR inline operator QFlags() 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)); } -- cgit v1.2.3