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

Etienne Sandré-Chardonnal etienne.sandre at m4x.org
Fri Feb 10 16:27:49 CET 2017


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170210/b7ca701a/attachment.html>


More information about the Interest mailing list