Skip to content

Commit

Permalink
Set NULL payloads and fix unit test (#71)
Browse files Browse the repository at this point in the history
* Set publish payload to NULL when zero length

* Add unit test for zero length payload

* Fix unit tests for deserialize publish

* Rename setupWillInfo to setupPublishInfo

* Remove void * from memset
  • Loading branch information
muneebahmed10 authored Sep 19, 2020
1 parent c4990cb commit 38f24af
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 75 deletions.
19 changes: 11 additions & 8 deletions source/core_mqtt_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,9 @@ static MQTTStatus_t deserializePublish( const MQTTPacketInfo_t * pIncomingPacket

LogDebug( ( "Packet identifier %hu.", *pPacketId ) );

/* Advance pointer two bytes to start of payload as in the QoS 0 case. */
pPacketIdentifierHigh += sizeof( uint16_t );

/* Packet identifier cannot be 0. */
if( *pPacketId == 0U )
{
Expand All @@ -1286,17 +1289,17 @@ static MQTTStatus_t deserializePublish( const MQTTPacketInfo_t * pIncomingPacket
{
/* Calculate the length of the payload. QoS 1 or 2 PUBLISH packets contain
* a packet identifier, but QoS 0 PUBLISH packets do not. */
if( pPublishInfo->qos == MQTTQoS0 )
{
pPublishInfo->payloadLength = ( pIncomingPacket->remainingLength - pPublishInfo->topicNameLength - sizeof( uint16_t ) );
pPublishInfo->pPayload = pPacketIdentifierHigh;
}
else
pPublishInfo->payloadLength = pIncomingPacket->remainingLength - pPublishInfo->topicNameLength - sizeof( uint16_t );

if( pPublishInfo->qos != MQTTQoS0 )
{
pPublishInfo->payloadLength = ( pIncomingPacket->remainingLength - pPublishInfo->topicNameLength - 2U * sizeof( uint16_t ) );
pPublishInfo->pPayload = pPacketIdentifierHigh + sizeof( uint16_t );
/* Two more bytes for the packet identifier. */
pPublishInfo->payloadLength -= sizeof( uint16_t );
}

/* Set payload if it exists. */
pPublishInfo->pPayload = ( pPublishInfo->payloadLength != 0U ) ? pPacketIdentifierHigh : NULL;

LogDebug( ( "Payload length %lu.", ( unsigned long ) pPublishInfo->payloadLength ) );
}

Expand Down
Loading

0 comments on commit 38f24af

Please sign in to comment.