Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for passing the Coverity Static Analysis #314

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ jobs:
echo -e "::group::${{ env.bashInfo }} Generate Coverage Report ${{ env.bashEnd }}"
# Generate coverage report, excluding extra directories
lcov --rc lcov_branch_coverage=1 -r build/coverage.info -o build/coverage.info '*test*' '*CMakeCCompilerId*' '*mocks*'
lcov --rc branch_coverage=1 -r build/coverage.info -o build/coverage.info
echo "::endgroup::"
lcov --rc lcov_branch_coverage=1 --list build/coverage.info
lcov --rc branch_coverage=1 --list build/coverage.info
echo -e "${{ env.bashPass }} ${{env.stepName}} ${{ env.bashEnd }}"
- name: Check Coverage
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,27 @@ or the following:
submodule is cloned as described [above](#checkout-cmock-submodule))

1. Run the _cmake_ command:

For Linux machines:
```
cmake -S test -B build/ \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_CLONE_SUBMODULES=ON \
-DUNITTEST=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
```
Note: For Mac users, additionally add the `-DCMAKE_C_STANDARD=99` flag to the
above command.
For Mac machines:
```
cmake -S test -B build/ \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_CLONE_SUBMODULES=ON \
-DUNITTEST=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG' \
-DCMAKE_C_STANDARD=99
```
1. Run this command to build the library and unit tests: `make -C build all`.
Expand Down
66 changes: 35 additions & 31 deletions source/core_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2208,38 +2208,39 @@ static MQTTStatus_t sendPublishWithoutCopy( MQTTContext_t * pContext,
totalMessageLength += pPublishInfo->payloadLength;
}

/* If not already set, set the dup flag before storing a copy of the publish
* this is because on retrieving back this copy we will get it in the form of an
* array of TransportOutVector_t that holds the data in a const pointer which cannot be
* changed after retrieving. */
if( pPublishInfo->dup != true )
{
MQTT_UpdateDuplicatePublishFlag( pMqttHeader, true );

dupFlagChanged = true;
}

/* store a copy of the publish for retransmission purposes */
if( ( pPublishInfo->qos > MQTTQoS0 ) &&
( pContext->storeFunction != NULL ) )
{
MQTTVec_t mqttVec;
/* If not already set, set the dup flag before storing a copy of the publish
* this is because on retrieving back this copy we will get it in the form of an
* array of TransportOutVector_t that holds the data in a const pointer which cannot be
* changed after retrieving. */
if( pPublishInfo->dup != true )
{
status = MQTT_UpdateDuplicatePublishFlag( pMqttHeader, true );

mqttVec.pVector = pIoVector;
mqttVec.vectorLen = ioVectorLength;
dupFlagChanged = ( status == MQTTSuccess );
}

if( pContext->storeFunction( pContext, packetId, &mqttVec ) != true )
if( status == MQTTSuccess )
{
status = MQTTPublishStoreFailed;
}
}
MQTTVec_t mqttVec;

/* change the value of the dup flag to its original, if it was changed */
if( dupFlagChanged )
{
MQTT_UpdateDuplicatePublishFlag( pMqttHeader, false );
mqttVec.pVector = pIoVector;
mqttVec.vectorLen = ioVectorLength;

if( pContext->storeFunction( pContext, packetId, &mqttVec ) != true )
{
status = MQTTPublishStoreFailed;
}
}

dupFlagChanged = false;
/* change the value of the dup flag to its original, if it was changed */
if( ( status == MQTTSuccess ) && ( dupFlagChanged == true ) )
{
status = MQTT_UpdateDuplicatePublishFlag( pMqttHeader, false );
}
}

if( ( status == MQTTSuccess ) &&
Expand Down Expand Up @@ -2608,8 +2609,7 @@ static MQTTStatus_t handleCleanSession( MQTTContext_t * pContext )
{
pContext->clearFunction( pContext, packetId );
}
} while( ( packetId != MQTT_PACKET_ID_INVALID ) &&
( status == MQTTSuccess ) );
} while( packetId != MQTT_PACKET_ID_INVALID );
}

if( pContext->outgoingPublishRecordMaxCount > 0U )
Expand Down Expand Up @@ -2864,7 +2864,7 @@ MQTTStatus_t MQTT_CancelCallback( const MQTTContext_t * pContext,

/*-----------------------------------------------------------*/

MQTTStatus_t MQTT_CheckConnectStatus( MQTTContext_t * pContext )
MQTTStatus_t MQTT_CheckConnectStatus( const MQTTContext_t * pContext )
{
MQTTConnectionStatus_t connectStatus;
MQTTStatus_t status = MQTTSuccess;
Expand Down Expand Up @@ -3729,11 +3729,11 @@ const char * MQTT_Status_strerror( MQTTStatus_t status )

/*-----------------------------------------------------------*/

size_t MQTT_GetBytesInMQTTVec( MQTTVec_t * pVec )
size_t MQTT_GetBytesInMQTTVec( const MQTTVec_t * pVec )
{
size_t memoryRequired = 0;
size_t i;
TransportOutVector_t * pTransportVec = pVec->pVector;
const TransportOutVector_t * pTransportVec = pVec->pVector;
size_t vecLen = pVec->vectorLen;

for( i = 0; i < vecLen; i++ )
Expand All @@ -3747,16 +3747,20 @@ size_t MQTT_GetBytesInMQTTVec( MQTTVec_t * pVec )
/*-----------------------------------------------------------*/

void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem,
MQTTVec_t * pVec )
const MQTTVec_t * pVec )
{
TransportOutVector_t * pTransportVec = pVec->pVector;
const TransportOutVector_t * pTransportVec = pVec->pVector;
const size_t vecLen = pVec->vectorLen;
void * copyDest = NULL;
const void * copySource = NULL;
size_t index = 0;
size_t i = 0;

for( i = 0; i < vecLen; i++ )
{
memcpy( &pAllocatedMem[ index ], pTransportVec[ i ].iov_base, pTransportVec[ i ].iov_len );
copyDest = &pAllocatedMem[ index ];
copySource = pTransportVec[ i ].iov_base;
( void ) memcpy( copyDest, copySource, pTransportVec[ i ].iov_len );
index += pTransportVec[ i ].iov_len;
}
}
Expand Down
27 changes: 15 additions & 12 deletions source/core_mqtt_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,8 @@ static uint8_t * encodeString( uint8_t * pDestination,
uint16_t sourceLength )
{
uint8_t * pBuffer = NULL;

/* Typecast const char * typed source buffer to const uint8_t *.
* This is to use same type buffers in memcpy. */
const uint8_t * pSourceBuffer = ( const uint8_t * ) pSource;
void * copyDest = NULL;
const void * copySource = NULL;

assert( pDestination != NULL );

Expand All @@ -546,9 +544,12 @@ static uint8_t * encodeString( uint8_t * pDestination,
pBuffer++;

/* Copy the string into pBuffer. */
if( pSourceBuffer != NULL )
copyDest = pBuffer;
copySource = pSource;

if( copySource != NULL )
{
( void ) memcpy( pBuffer, pSourceBuffer, sourceLength );
( void ) memcpy( copyDest, copySource, sourceLength );
}

/* Return the pointer to the end of the encoded string. */
Expand Down Expand Up @@ -713,7 +714,8 @@ static void serializePublishCommon( const MQTTPublishInfo_t * pPublishInfo,
bool serializePayload )
{
uint8_t * pIndex = NULL;
const uint8_t * pPayloadBuffer = NULL;
void * copyDest = NULL;
const void * copySource = NULL;

/* The first byte of a PUBLISH packet contains the packet type and flags. */
uint8_t publishFlags = MQTT_PACKET_TYPE_PUBLISH;
Expand Down Expand Up @@ -787,11 +789,10 @@ static void serializePublishCommon( const MQTTPublishInfo_t * pPublishInfo,
LogDebug( ( "Copying PUBLISH payload of length =%lu to buffer",
( unsigned long ) pPublishInfo->payloadLength ) );

/* Typecast const void * typed payload buffer to const uint8_t *.
* This is to use same type buffers in memcpy. */
pPayloadBuffer = ( const uint8_t * ) pPublishInfo->pPayload;
copyDest = pIndex;
copySource = pPublishInfo->pPayload;

( void ) memcpy( pIndex, pPayloadBuffer, pPublishInfo->payloadLength );
( void ) memcpy( copyDest, copySource, pPublishInfo->payloadLength );
/* Move the index to after the payload. */
pIndex = &pIndex[ pPublishInfo->payloadLength ];
}
Expand Down Expand Up @@ -2635,10 +2636,12 @@ MQTTStatus_t MQTT_UpdateDuplicatePublishFlag( uint8_t * pHeader,

if( pHeader == NULL )
{
LogError( ( "Header cannot be NULL" ) );
status = MQTTBadParameter;
}
else if( ( ( *pHeader ) & 0xF0 ) != MQTT_PACKET_TYPE_PUBLISH )
else if( ( ( *pHeader ) & 0xF0U ) != MQTT_PACKET_TYPE_PUBLISH )
{
LogError( ( "Header is not publish packet header" ) );
status = MQTTBadParameter;
}
else if( set == true )
Expand Down
12 changes: 5 additions & 7 deletions source/include/core_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ typedef void (* MQTTEventCallback_t )( struct MQTTContext * pContext,
*
* @param[in] pContext Initialised MQTT Context.
* @param[in] packetId Outgoing publish packet identifier.
* @param[in] pMqttVec Pointer to the opaque mqtt vector structure. Users should use MQTT_SerializeMQTTVec
* and MQTT_GetBytesInMQTTVec functions to get the memory required and to serialize the
* @param[in] pMqttVec Pointer to the opaque mqtt vector structure. Users should use MQTT_GetBytesInMQTTVec
* and MQTT_SerializeMQTTVec functions to get the memory required and to serialize the
* MQTTVec_t in the provided memory respectively.
*
* @return True if the copy is successful else false.
Expand Down Expand Up @@ -152,8 +152,6 @@ typedef bool ( * MQTTRetrievePacketForRetransmit)( struct MQTTContext * pContext
*
* @param[in] pContext Initialised MQTT Context.
* @param[in] packetId Copied publish packet identifier.
*
* @return True if the clear is successful else false.
*/
/* @[define_mqtt_retransmitclearpacket] */
typedef void (* MQTTClearPacketForRetransmit)( struct MQTTContext * pContext,
Expand Down Expand Up @@ -597,7 +595,7 @@ MQTTStatus_t MQTT_InitRetransmits( MQTTContext_t * pContext,
* @endcode
*/
/* @[declare_mqtt_checkconnectstatus] */
MQTTStatus_t MQTT_CheckConnectStatus( MQTTContext_t * pContext );
MQTTStatus_t MQTT_CheckConnectStatus( const MQTTContext_t * pContext );
/* @[declare_mqtt_checkconnectstatus] */

/**
Expand Down Expand Up @@ -1235,7 +1233,7 @@ const char * MQTT_Status_strerror( MQTTStatus_t status );
* @return The bytes in the provided #MQTTVec array which can then be used to set aside memory to be used with MQTT_SerializeMQTTVec( void * pAllocatedMem, MQTTVec_t *pVec ) function.
*/
/* @[declare_mqtt_getbytesinmqttvec] */
size_t MQTT_GetBytesInMQTTVec( MQTTVec_t * pVec );
size_t MQTT_GetBytesInMQTTVec( const MQTTVec_t * pVec );
/* @[declare_mqtt_getbytesinmqttvec] */

/**
Expand All @@ -1246,7 +1244,7 @@ size_t MQTT_GetBytesInMQTTVec( MQTTVec_t * pVec );
*/
/* @[declare_mqtt_serializemqttvec] */
void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem,
MQTTVec_t * pVec );
const MQTTVec_t * pVec );
/* @[declare_mqtt_serializemqttvec] */

/* *INDENT-OFF* */
Expand Down
Loading