Skip to content

Commit

Permalink
Add a new macro to detect transport error in core_mqtt_serializer.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Dakshit Babbar committed Sep 11, 2024
1 parent 19fd967 commit 5ead3f8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions source/core_mqtt_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@
*/
#define MQTT_REMAINING_LENGTH_INVALID ( ( size_t ) 268435456 )

/**
* @brief A value that represents transport layer error while retreiving the remaining length.
*
* This value is greater than what is allowed by the MQTT specification.
*/
#define MQTT_REMAINING_LENGTH_TRANSPORT_ERROR ( ( size_t ) 268435457 )

/**
* @brief The minimum remaining length for a QoS 0 PUBLISH.
*
Expand Down Expand Up @@ -822,22 +829,22 @@ static size_t getRemainingLength( TransportRecv_t recvFunc,
}
else if( bytesReceived < 0)
{
remainingLength = -1;
remainingLength = MQTT_REMAINING_LENGTH_TRANSPORT_ERROR;
}
else
{
remainingLength = MQTT_REMAINING_LENGTH_INVALID;
}
}

if( remainingLength == MQTT_REMAINING_LENGTH_INVALID || remainingLength < 0)
if( remainingLength == MQTT_REMAINING_LENGTH_INVALID || remainingLength == MQTT_REMAINING_LENGTH_TRANSPORT_ERROR)
{
break;
}
} while( ( encodedByte & 0x80U ) != 0U );

/* Check that the decoded remaining length conforms to the MQTT specification. */
if( (remainingLength != MQTT_REMAINING_LENGTH_INVALID) && (remainingLength >=0) )
if( (remainingLength != MQTT_REMAINING_LENGTH_INVALID) && (remainingLength != MQTT_REMAINING_LENGTH_TRANSPORT_ERROR) )
{
expectedSize = remainingLengthEncodedSize( remainingLength );

Expand Down Expand Up @@ -2595,7 +2602,7 @@ MQTTStatus_t MQTT_GetIncomingPacketTypeAndLength( TransportRecv_t readFunc,
LogError( ( "Incoming packet remaining length invalid." ) );
status = MQTTBadResponse;
}
else if( pIncomingPacket->remainingLength < 0)
else if( pIncomingPacket->remainingLength == MQTT_REMAINING_LENGTH_TRANSPORT_ERROR)
{
/* MQTT Connection status cannot be updated here hence bubble up
* MQTTStatusDisconnectPending status to the calling API that can update it. */
Expand Down

0 comments on commit 5ead3f8

Please sign in to comment.