[Interest] QDataStream null terminated string / Bug or expected behavior?

Mike Chinander chinander at gmail.com
Fri Feb 10 16:33:43 CET 2017


I think it's str.c_str() that's adding the null character at the end of
your string, not anything with QDatastream or QBytearray.

On Fri, Feb 10, 2017 at 9:27 AM, Etienne Sandré-Chardonnal <
etienne.sandre at m4x.org> wrote:

> Thanks for the link. Yes, I'm doing something bad, but there is no strong
> reason to encode a string litteral differently than a QByteArray and not
> allowing this. But since retro-compatibility is more important I suppose
> this cannot be changed...
>
> Is there a page summarizing all QDataStream >> overloads? They are not in
> QDataStream documentation, even as non-members. Seems difficult to find
> them all without looking into all the storage classes.
>
> 2017-02-09 22:09 GMT+01:00 Thiago Macieira <thiago.macieira at intel.com>:
>
>> On quinta-feira, 9 de fevereiro de 2017 18:18:07 PST Etienne
>> Sandré-Chardonnal
>> wrote:
>> > Dear all,
>> >
>> > I just found out a bug in my code which was due to the way I wrote/read
>> > from QDataStream.
>> >
>> > I write a string like that in one program, from a std::string. This uses
>> > the null-terminated char* overload of << :
>> >
>> > std::string str("test");
>> > stream << str.c_str();
>> >
>> > I read the data like that in another program:
>> >
>> > QByteArray ba;
>> > stream >> ba;
>> >
>> > Then ba.size() returns 5 and it contains the null character. Therefore,
>> > comparing it to "test" failed.
>> >
>> > In the documentation I cannot find a QByteArray & overload of operator
>> >>.
>> > QByteArray is not supposed to be castable to char*. What is happening
>> here?
>>
>> The two operators are there:
>> Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
>> Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
>>
>> And they're documented:
>> http://doc.qt.io/qt-5/qbytearray.html#operator-lt-lt
>> http://doc.qt.io/qt-5/qbytearray.html#operator-gt-gt
>>
>> The bug is in your code for marshalling one type and demarshalling
>> another.
>> That's not guaranteed to work. The behaviour of the two functions is
>> different. See for yourself:
>>
>> https://code.woboq.org/qt5/qtbase/src/corelib/tools/
>> qbytearray.cpp.html#_ZlsR11QDataStreamRK10QByteArray
>> https://code.woboq.org/qt5/qtbase/src/corelib/io/
>> qdatastream.cpp.html#_ZN11QDataStreamlsEPKc
>>
>> --
>> Thiago Macieira - thiago.macieira (AT) intel.com
>>   Software Architect - Intel Open Source Technology Center
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170210/f3f088bf/attachment.html>


More information about the Interest mailing list