[Development] It could be a little bug

Marc Mutz marc.mutz at kdab.com
Mon Nov 4 12:24:37 CET 2013


On Saturday, November 02, 2013 02:35:20 Thiago Macieira wrote:
> On sexta-feira, 1 de novembro de 2013 15:35:50, Marc Mutz wrote:
> > According to the standard, they are equivalent, since reading a value
> > from an  enum object that does have one of the declared enum values
> > results in undefined behaviour. That said, we're violating that in many
> > places in Qt, e.g. in QFlags, so it might be that the code in question
> > also abuses m_state that way. in that case, the existing code would be
> > more robust, but faces the compiler optimising away the & NoErrors due
> > to the reason mentioned above.
> 
> QFlags is not violating anything.

Clang's -fsanitize=undefined disagrees:

/home/marc/qtbase-clang-
build/src/corelib/../../include/QtCore/../../../qtbase/src/corelib/io/qurl.h:353:10: runtime error: 
load of value 32505856, which is not a valid value for type 'QUrl::UrlFormattingOption'
/home/marc/qtbase-clang-
build/src/corelib/../../include/QtCore/../../../qtbase/src/corelib/io/qurl.h:90: runtime error: load of 
value 32505856, which is not a valid value for type 'QUrl::UrlFormattingOption'
/home/marc/qtbase-clang-
build/src/corelib/../../include/QtCore/../../../qtbase/src/corelib/io/qurl.h:66: runtime error: load of 
value 32505857, which is not a valid value for type 'QUrl::UrlFormattingOption'
/home/marc/qtbase-clang-
build/src/corelib/../../include/QtCore/../../../qtbase/src/corelib/io/qurl.h:66: runtime error: load of 
value 32505857, which is not a valid value for type 'QUrl::UrlFormattingOption'
PASS   : tst_QDataUrl::emptyData()

and

/home/marc/qtbase-clang-
build/src/corelib/../../include/QtCore/../../../qtbase/src/corelib/global/qflags.h:100: runtime error: 
load of value 4161798143, which is not a valid value for type 
'QUrl::ComponentFormattingOption'
PASS   : tst_QUrl::comparison()


> First of all, an enum is required to be backed by an integer big enough to
> hold at least all values of the enum. That means all OR combinations of
> existing values will fit the enum and will not result in undefined
> behaviour.
> 
> I can't think of any place where we might be doing that. It's definitely
> not in the API, but it might happen in backend code where QFlags was
> deemed too heavyweight for a simple flags, but in most cases the value is
> stored directly in an int.
> 
> And even if that were the case, QFlags does not store an enum. It stores an
> int, which has a very defined behaviour of storing bitwise OR combinations
> of known values.

The issue is with the casts to enums in some of the QFlags operators, IIRC, but the log I cited 
is old and the line numbers might not match anymore.

Thanks,
Marc



More information about the Development mailing list