QDataStream usage
-
Hi,
I have doubt regrading usage of QDataStream::readRawData method. I am not sure that whether QDataStream will append ‘\0’ at the end of messageData string(see below code). I have checked QDataStream::readbytes implementation, it does append the ‘\0’ character at the end. Please confirm whether QDataStream::readRawData will append ‘\0’ at the end of string or not. Please also provide comment for below usage of QDataStream.
Code snippet:
@
QDataStream streamToDeSerialize(&aSerializedData,QIODevice::ReadOnly);
streamToDeSerialize>>mSessionId;
streamToDeSerialize>>mAsyncRequestId;
streamToDeSerialize>>mMessageType;
streamToDeSerialize>>mMessageDataLength;
char *messageData = new char[mMessageDataLength];
streamToDeSerialize.readRawData(messageData,mMessageDataLength);
@Thanks,
Sumit Lonialedit: Code highlighting added / Denis Kormalev
-
I hereby confirm that QDataStream::readRawData does not append \0.
I also provide a comment for your usage of QDataStream. You probably want to do something with the return value of QDataStream::readRawData. Maybe something like:
@
int bytesRead = streamToDeSerialize.readRawData(messageData,mMessageDataLength);
messageData[bytesRead] = 0;
@ -
Thanks for replying
I am having doubt over usage, Here is complete usage:
profileDataStream>>mMarketingDataLength; char *messageData = new char[mMarketingDataLength]; profileDataStream.readRawData(messageData,mMarketingDataLength); data = QByteArray(messageData,mMarketingDataLength); mMarketingInfo = new MarketingConsentInfo( data ); delete[] messageData;
We are reading stream data from into c style string using readRawData. Then copying this c style string into QByteArray , QByteArray uses memcpy and add '\0' character at last position of array. This can lead to over writing of valid last character of stream with null character since we are not allocating space for it. I think we should allocate extra space for null character in this usage.
-
Resending as formatting was not good.
Thanks for replying
I am having doubt over usage, Here is complete usage:
profileDataStream>>mMarketingDataLength;
char *messageData = new char[mMarketingDataLength];
profileDataStream.readRawData(messageData,mMarketingDataLength);
data = QByteArray(messageData,mMarketingDataLength);
mMarketingInfo = new MarketingConsentInfo( data );
delete[] messageData;We are reading stream data from into c style string using readRawData. Then copying this c style string into QByteArray , QByteArray uses memcpy and add ’\0’ character at last position of array. This can lead to over writing of valid last character of stream with null character since we are not allocating space for it. I think we should allocate extra space for null character in this usage.
-
lonial, please use @ marks for code snippets.
-
[quote author="jobor" date="1280868636"]I hereby confirm that QDataStream::readRawData does not append \0.
[/quote]This might be good to add to the documentation, it is not really clear about that part. Though it can be guessed and/or tested, it just sounds silly... :)