[Interest] It's time again to play Find-That-Bug

william.crocker at analog.com william.crocker at analog.com
Sat Sep 14 23:08:56 CEST 2013


>
> Can you find it?
>
>       QString
>       MakeDigest( QStringList list ) {
>           QCryptographicHash ch(QCryptographicHash::Md5);
>           foreach( QString s, list )
>               ch.addData( s.toAscii() );
>           return ch.result();
>       }
>

Yes. The problem is in the conversion from QByteArray (ch.result)
to the return type, QString. (I have the bad habit of using
QString for everything as they provide more operations).

The constructor QString(const QByteArray&) will stop if
it finds a zero byte and digests can contain a zero byte
anywhere in the set of 16 for an MD5.

I did not see this coming as QStrings can contain null
characters QChar('\0') and it knows what the length
of the QByteArray is. So, why stop at a null byte?
I can understand that with QString(const char *).

I guess I have to read ALL of the documentation,
not just for the hard parts.

Bill




More information about the Interest mailing list