1
0
mirror of https://github.com/FreeRTOS/coreMQTT synced 2025-06-29 23:25:58 +08:00

fix: MISRA 15.1 (goto) violations - part 2 (#718)

* fix: MISRA 15.1 (goto) violations - part 2

This PR has changes for iot_mqtt_api.c, iot_mqtt_operation.c. and iot_mqtt_subscription.c.
This commit is contained in:
abhidixi11 2020-01-17 09:44:03 -08:00 committed by GitHub
parent acd02af830
commit 70a2697c91
4 changed files with 424 additions and 455 deletions

View File

@ -56,6 +56,7 @@ iot
iotlink iotlink
iotlistdouble iotlistdouble
iotlog iotlog
iotlogdebug
iotmqtt iotmqtt
iotmqttcallbackinfo iotmqttcallbackinfo
iotmqttconnectinfo iotmqttconnectinfo
@ -85,6 +86,7 @@ keepaliveseconds
lu lu
lwt lwt
malloc malloc
misra
mqtt mqtt
mqttconnection mqttconnection
mqttoperation mqttoperation

View File

@ -438,13 +438,11 @@ static IotNetworkError_t _createNetworkConnection( const IotMqttNetworkInfo_t *
IotLogError( "Network information cannot be NULL." ); IotLogError( "Network information cannot be NULL." );
status = IOT_NETWORK_BAD_PARAMETER; status = IOT_NETWORK_BAD_PARAMETER;
goto cleanup;
} }
else if( pNetworkInfo->createNetworkConnection == true )
{
/* Create a new network connection if requested. Otherwise, copy the existing /* Create a new network connection if requested. Otherwise, copy the existing
* network connection. */ * network connection. */
if( pNetworkInfo->createNetworkConnection == true )
{
status = pNetworkInfo->pNetworkInterface->create( pNetworkInfo->u.setup.pNetworkServerInfo, status = pNetworkInfo->pNetworkInterface->create( pNetworkInfo->u.setup.pNetworkServerInfo,
pNetworkInfo->u.setup.pNetworkCredentialInfo, pNetworkInfo->u.setup.pNetworkCredentialInfo,
pNetworkConnection ); pNetworkConnection );
@ -458,8 +456,6 @@ static IotNetworkError_t _createNetworkConnection( const IotMqttNetworkInfo_t *
else else
{ {
IotLogError( "Failed to create network connection: %d", status ); IotLogError( "Failed to create network connection: %d", status );
goto cleanup;
} }
} }
else else
@ -470,7 +466,6 @@ static IotNetworkError_t _createNetworkConnection( const IotMqttNetworkInfo_t *
*pCreatedNewNetworkConnection = false; *pCreatedNewNetworkConnection = false;
} }
cleanup:
return status; return status;
} }
@ -492,7 +487,6 @@ static _mqttConnection_t * _createMqttConnection( bool awsIotMqttMode,
IotLogError( "Failed to allocate memory for new connection." ); IotLogError( "Failed to allocate memory for new connection." );
status = false; status = false;
goto cleanup;
} }
else else
{ {
@ -505,7 +499,6 @@ static _mqttConnection_t * _createMqttConnection( bool awsIotMqttMode,
/* Start a new MQTT connection with a reference count of 1. */ /* Start a new MQTT connection with a reference count of 1. */
pMqttConnection->references = 1; pMqttConnection->references = 1;
}
/* Create the references mutex for a new connection. It is a recursive mutex. */ /* Create the references mutex for a new connection. It is a recursive mutex. */
referencesMutexCreated = IotMutex_Create( &( pMqttConnection->referencesMutex ), true ); referencesMutexCreated = IotMutex_Create( &( pMqttConnection->referencesMutex ), true );
@ -515,9 +508,11 @@ static _mqttConnection_t * _createMqttConnection( bool awsIotMqttMode,
IotLogError( "Failed to create references mutex for new connection." ); IotLogError( "Failed to create references mutex for new connection." );
status = false; status = false;
goto cleanup; }
} }
if( status == true )
{
/* Create the subscription mutex for a new connection. */ /* Create the subscription mutex for a new connection. */
subscriptionMutexCreated = IotMutex_Create( &( pMqttConnection->subscriptionMutex ), false ); subscriptionMutexCreated = IotMutex_Create( &( pMqttConnection->subscriptionMutex ), false );
@ -526,9 +521,9 @@ static _mqttConnection_t * _createMqttConnection( bool awsIotMqttMode,
IotLogError( "Failed to create subscription mutex for new connection." ); IotLogError( "Failed to create subscription mutex for new connection." );
status = false; status = false;
goto cleanup;
} }
else
{
/* Create the new connection's subscription and operation lists. */ /* Create the new connection's subscription and operation lists. */
IotListDouble_Create( &( pMqttConnection->subscriptionList ) ); IotListDouble_Create( &( pMqttConnection->subscriptionList ) );
IotListDouble_Create( &( pMqttConnection->pendingProcessing ) ); IotListDouble_Create( &( pMqttConnection->pendingProcessing ) );
@ -537,18 +532,14 @@ static _mqttConnection_t * _createMqttConnection( bool awsIotMqttMode,
/* Check if keep-alive is active for this connection. */ /* Check if keep-alive is active for this connection. */
if( keepAliveSeconds != 0 ) if( keepAliveSeconds != 0 )
{ {
if( _createKeepAliveOperation( pNetworkInfo, status = _createKeepAliveOperation( pNetworkInfo,
keepAliveSeconds, keepAliveSeconds,
pMqttConnection ) == false ) pMqttConnection );
{ }
status = false;
goto cleanup;
} }
} }
/* Clean up mutexes and connection if this function failed. */ /* Clean up mutexes and connection if this function failed. */
cleanup:
if( status == false ) if( status == false )
{ {
if( subscriptionMutexCreated == true ) if( subscriptionMutexCreated == true )
@ -654,9 +645,9 @@ static IotMqttError_t _subscriptionCommonSetup( IotMqttOperationType_t operation
if( _checkInit() == false ) if( _checkInit() == false )
{ {
status = IOT_MQTT_NOT_INITIALIZED; status = IOT_MQTT_NOT_INITIALIZED;
goto cleanup;
} }
else
{
/* Check that all elements in the subscription list are valid. */ /* Check that all elements in the subscription list are valid. */
if( _IotMqtt_ValidateSubscriptionList( operation, if( _IotMqtt_ValidateSubscriptionList( operation,
mqttConnection->awsIotMqttMode, mqttConnection->awsIotMqttMode,
@ -664,9 +655,11 @@ static IotMqttError_t _subscriptionCommonSetup( IotMqttOperationType_t operation
subscriptionCount ) == false ) subscriptionCount ) == false )
{ {
status = IOT_MQTT_BAD_PARAMETER; status = IOT_MQTT_BAD_PARAMETER;
goto cleanup; }
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Check that a reference pointer is provided for a waitable operation. */ /* Check that a reference pointer is provided for a waitable operation. */
if( ( flags & IOT_MQTT_FLAG_WAITABLE ) == IOT_MQTT_FLAG_WAITABLE ) if( ( flags & IOT_MQTT_FLAG_WAITABLE ) == IOT_MQTT_FLAG_WAITABLE )
{ {
@ -676,11 +669,10 @@ static IotMqttError_t _subscriptionCommonSetup( IotMqttOperationType_t operation
IotMqtt_OperationType( operation ) ); IotMqtt_OperationType( operation ) );
status = IOT_MQTT_BAD_PARAMETER; status = IOT_MQTT_BAD_PARAMETER;
goto cleanup; }
} }
} }
cleanup:
return status; return status;
} }
@ -704,14 +696,10 @@ static IotMqttError_t _subscriptionCreateAndSerialize( IotMqttOperationType_t op
pCallbackInfo, pCallbackInfo,
ppSubscriptionOperation ); ppSubscriptionOperation );
if( status != IOT_MQTT_SUCCESS ) if( status == IOT_MQTT_SUCCESS )
{
goto cleanup;
}
else
{ {
pSubscriptionOperation = ( *ppSubscriptionOperation ); pSubscriptionOperation = ( *ppSubscriptionOperation );
}
/* Check the subscription operation data and set the operation type. */ /* Check the subscription operation data and set the operation type. */
IotMqtt_Assert( pSubscriptionOperation->u.operation.status == IOT_MQTT_STATUS_PENDING ); IotMqtt_Assert( pSubscriptionOperation->u.operation.status == IOT_MQTT_STATUS_PENDING );
@ -724,17 +712,15 @@ static IotMqttError_t _subscriptionCreateAndSerialize( IotMqttOperationType_t op
&( pSubscriptionOperation->u.operation.pMqttPacket ), &( pSubscriptionOperation->u.operation.pMqttPacket ),
&( pSubscriptionOperation->u.operation.packetSize ), &( pSubscriptionOperation->u.operation.packetSize ),
&( pSubscriptionOperation->u.operation.packetIdentifier ) ); &( pSubscriptionOperation->u.operation.packetIdentifier ) );
if( status != IOT_MQTT_SUCCESS )
{
goto cleanup;
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Check the serialized MQTT packet. */ /* Check the serialized MQTT packet. */
IotMqtt_Assert( pSubscriptionOperation->u.operation.pMqttPacket != NULL ); IotMqtt_Assert( pSubscriptionOperation->u.operation.pMqttPacket != NULL );
IotMqtt_Assert( pSubscriptionOperation->u.operation.packetSize > 0 ); IotMqtt_Assert( pSubscriptionOperation->u.operation.packetSize > 0 );
}
cleanup:
return status; return status;
} }
@ -762,11 +748,8 @@ static IotMqttError_t _subscriptionCommon( IotMqttOperationType_t operation,
pCallbackInfo, pCallbackInfo,
&pSubscriptionOperation ); &pSubscriptionOperation );
if( status != IOT_MQTT_SUCCESS ) if( status == IOT_MQTT_SUCCESS )
{ {
goto cleanup;
}
/* Add the subscription list for a SUBSCRIBE. */ /* Add the subscription list for a SUBSCRIBE. */
if( operation == IOT_MQTT_SUBSCRIBE ) if( operation == IOT_MQTT_SUBSCRIBE )
{ {
@ -774,13 +757,11 @@ static IotMqttError_t _subscriptionCommon( IotMqttOperationType_t operation,
pSubscriptionOperation->u.operation.packetIdentifier, pSubscriptionOperation->u.operation.packetIdentifier,
pSubscriptionList, pSubscriptionList,
subscriptionCount ); subscriptionCount );
}
}
if( status != IOT_MQTT_SUCCESS ) if( status == IOT_MQTT_SUCCESS )
{ {
goto cleanup;
}
}
/* Set the reference, if provided. */ /* Set the reference, if provided. */
_setOperationReference( pOperationReference, pSubscriptionOperation ); _setOperationReference( pOperationReference, pSubscriptionOperation );
@ -810,14 +791,11 @@ static IotMqttError_t _subscriptionCommon( IotMqttOperationType_t operation,
/* Clear the previously set (and now invalid) reference. */ /* Clear the previously set (and now invalid) reference. */
_setOperationReference( pOperationReference, IOT_MQTT_OPERATION_INITIALIZER ); _setOperationReference( pOperationReference, IOT_MQTT_OPERATION_INITIALIZER );
}
goto cleanup;
} }
} }
/* Clean up if this function failed. */ /* Clean up if this function failed. */
cleanup:
if( status != IOT_MQTT_SUCCESS ) if( status != IOT_MQTT_SUCCESS )
{ {
if( pSubscriptionOperation != NULL ) if( pSubscriptionOperation != NULL )
@ -911,8 +889,8 @@ bool _IotMqtt_IncrementConnectionReferences( _mqttConnection_t * pMqttConnection
{ {
( pMqttConnection->references )++; ( pMqttConnection->references )++;
/* In some implementations IotLog() maps to C standard printing API /* In some implementations IotLogDebug() maps to C standard printing API
* that need specific primitive types for format specifiers. Also, * that needs specific primitive types for format specifiers. Also,
* inttypes.h may not be available on some C99 compilers, despite * inttypes.h may not be available on some C99 compilers, despite
* stdint.h being available. */ * stdint.h being available. */
/* coverity[misra_c_2012_directive_4_6_violation] */ /* coverity[misra_c_2012_directive_4_6_violation] */
@ -944,8 +922,8 @@ void _IotMqtt_DecrementConnectionReferences( _mqttConnection_t * pMqttConnection
( pMqttConnection->references )--; ( pMqttConnection->references )--;
IotMqtt_Assert( pMqttConnection->references >= 0 ); IotMqtt_Assert( pMqttConnection->references >= 0 );
/* In some implementations IotLog() maps to C standard printing API /* In some implementations IotLogDebug() maps to C standard printing API
* that need specific primitive types for format specifiers. Also, * that needs specific primitive types for format specifiers. Also,
* inttypes.h may not be available on some C99 compilers, despite stdint.h * inttypes.h may not be available on some C99 compilers, despite stdint.h
* being available. */ * being available. */
/* coverity[misra_c_2012_directive_4_6_violation] */ /* coverity[misra_c_2012_directive_4_6_violation] */
@ -1054,16 +1032,14 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
if( _checkInit() == false ) if( _checkInit() == false )
{ {
status = IOT_MQTT_NOT_INITIALIZED; status = IOT_MQTT_NOT_INITIALIZED;
goto cleanup;
} }
/* Validate network interface and connect info. */ /* Validate network interface and connect info. */
if( _IotMqtt_ValidateConnect( pConnectInfo ) == false ) else if( _IotMqtt_ValidateConnect( pConnectInfo ) == false )
{ {
status = IOT_MQTT_BAD_PARAMETER; status = IOT_MQTT_BAD_PARAMETER;
goto cleanup;
} }
else
{
networkStatus = _createNetworkConnection( pNetworkInfo, networkStatus = _createNetworkConnection( pNetworkInfo,
&pNetworkConnection, &pNetworkConnection,
&ownNetworkConnection ); &ownNetworkConnection );
@ -1071,9 +1047,11 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
if( networkStatus != IOT_NETWORK_SUCCESS ) if( networkStatus != IOT_NETWORK_SUCCESS )
{ {
status = IOT_MQTT_NETWORK_ERROR; status = IOT_MQTT_NETWORK_ERROR;
goto cleanup; }
} }
if( status == IOT_MQTT_SUCCESS )
{
IotLogInfo( "Establishing new MQTT connection." ); IotLogInfo( "Establishing new MQTT connection." );
/* Initialize a new MQTT connection object. */ /* Initialize a new MQTT connection object. */
@ -1084,9 +1062,11 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
if( pNewMqttConnection == NULL ) if( pNewMqttConnection == NULL )
{ {
status = IOT_MQTT_NO_MEMORY; status = IOT_MQTT_NO_MEMORY;
goto cleanup;
} }
else }
/* Set the MQTT receive callback. */
if( status == IOT_MQTT_SUCCESS )
{ {
/* Set the network connection associated with the MQTT connection. */ /* Set the network connection associated with the MQTT connection. */
pNewMqttConnection->pNetworkConnection = pNetworkConnection; pNewMqttConnection->pNetworkConnection = pNetworkConnection;
@ -1098,9 +1078,7 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
#else #else
pNewMqttConnection->pSerializer = NULL; pNewMqttConnection->pSerializer = NULL;
#endif #endif
}
/* Set the MQTT receive callback. */
networkStatus = pNewMqttConnection->pNetworkInterface->setReceiveCallback( pNetworkConnection, networkStatus = pNewMqttConnection->pNetworkInterface->setReceiveCallback( pNetworkConnection,
IotMqtt_ReceiveCallback, IotMqtt_ReceiveCallback,
pNewMqttConnection ); pNewMqttConnection );
@ -1110,20 +1088,19 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
IotLogError( "Failed to set MQTT network receive callback." ); IotLogError( "Failed to set MQTT network receive callback." );
status = IOT_MQTT_NETWORK_ERROR; status = IOT_MQTT_NETWORK_ERROR;
goto cleanup;
} }
else
{
/* Create a CONNECT operation. */ /* Create a CONNECT operation. */
status = _IotMqtt_CreateOperation( pNewMqttConnection, status = _IotMqtt_CreateOperation( pNewMqttConnection,
IOT_MQTT_FLAG_WAITABLE, IOT_MQTT_FLAG_WAITABLE,
NULL, NULL,
&pOperation ); &pOperation );
}
if( status != IOT_MQTT_SUCCESS )
{
goto cleanup;
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Ensure the members set by operation creation and serialization /* Ensure the members set by operation creation and serialization
* are appropriate for a blocking CONNECT. */ * are appropriate for a blocking CONNECT. */
IotMqtt_Assert( pOperation->u.operation.status == IOT_MQTT_STATUS_PENDING ); IotMqtt_Assert( pOperation->u.operation.status == IOT_MQTT_STATUS_PENDING );
@ -1144,23 +1121,19 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
2, 2,
pConnectInfo->pPreviousSubscriptions, pConnectInfo->pPreviousSubscriptions,
pConnectInfo->previousSubscriptionCount ); pConnectInfo->previousSubscriptionCount );
}
}
if( status != IOT_MQTT_SUCCESS ) if( status == IOT_MQTT_SUCCESS )
{ {
goto cleanup;
}
}
/* Convert the connect info and will info objects to an MQTT CONNECT packet. */ /* Convert the connect info and will info objects to an MQTT CONNECT packet. */
status = _getMqttConnectSerializer( pNetworkInfo->pMqttSerializer )( pConnectInfo, status = _getMqttConnectSerializer( pNetworkInfo->pMqttSerializer )( pConnectInfo,
&( pOperation->u.operation.pMqttPacket ), &( pOperation->u.operation.pMqttPacket ),
&( pOperation->u.operation.packetSize ) ); &( pOperation->u.operation.packetSize ) );
if( status != IOT_MQTT_SUCCESS )
{
goto cleanup;
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Check the serialized MQTT packet. */ /* Check the serialized MQTT packet. */
IotMqtt_Assert( pOperation->u.operation.pMqttPacket != NULL ); IotMqtt_Assert( pOperation->u.operation.pMqttPacket != NULL );
IotMqtt_Assert( pOperation->u.operation.packetSize > 0 ); IotMqtt_Assert( pOperation->u.operation.packetSize > 0 );
@ -1174,6 +1147,7 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
/* The call to wait cleans up the CONNECT operation, so set the pointer /* The call to wait cleans up the CONNECT operation, so set the pointer
* to NULL. */ * to NULL. */
pOperation = NULL; pOperation = NULL;
}
/* When a connection is successfully established, schedule keep-alive job. */ /* When a connection is successfully established, schedule keep-alive job. */
if( status == IOT_MQTT_SUCCESS ) if( status == IOT_MQTT_SUCCESS )
@ -1190,13 +1164,10 @@ IotMqttError_t IotMqtt_Connect( const IotMqttNetworkInfo_t * pNetworkInfo,
if( taskPoolStatus != IOT_TASKPOOL_SUCCESS ) if( taskPoolStatus != IOT_TASKPOOL_SUCCESS )
{ {
status = IOT_MQTT_SCHEDULING_ERROR; status = IOT_MQTT_SCHEDULING_ERROR;
goto cleanup;
} }
} }
} }
cleanup:
if( status != IOT_MQTT_SUCCESS ) if( status != IOT_MQTT_SUCCESS )
{ {
IotLogError( "Failed to establish new MQTT connection, error %s.", IotLogError( "Failed to establish new MQTT connection, error %s.",
@ -1259,7 +1230,7 @@ void IotMqtt_Disconnect( IotMqttConnection_t mqttConnection,
uint32_t flags ) uint32_t flags )
{ {
bool disconnected = false, initCalled = false; bool disconnected = false, initCalled = false;
IotMqttError_t status = IOT_MQTT_STATUS_PENDING; IotMqttError_t status = IOT_MQTT_SUCCESS;
_mqttOperation_t * pOperation = NULL; _mqttOperation_t * pOperation = NULL;
/* Check that IotMqtt_Init was called. */ /* Check that IotMqtt_Init was called. */
@ -1267,16 +1238,20 @@ void IotMqtt_Disconnect( IotMqttConnection_t mqttConnection,
if( initCalled == false ) if( initCalled == false )
{ {
goto cleanup; status = IOT_MQTT_STATUS_PENDING;
} }
else
{
/* Only send a DISCONNECT packet if the connection is active and the "cleanup only" /* Only send a DISCONNECT packet if the connection is active and the "cleanup only"
* flag is not set. */ * flag is not set. */
if( ( flags & IOT_MQTT_FLAG_CLEANUP_ONLY ) == IOT_MQTT_FLAG_CLEANUP_ONLY ) if( ( flags & IOT_MQTT_FLAG_CLEANUP_ONLY ) == IOT_MQTT_FLAG_CLEANUP_ONLY )
{ {
goto cleanup; status = IOT_MQTT_STATUS_PENDING;
}
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Read the connection status. */ /* Read the connection status. */
IotMutex_Lock( &( mqttConnection->referencesMutex ) ); IotMutex_Lock( &( mqttConnection->referencesMutex ) );
disconnected = mqttConnection->disconnected; disconnected = mqttConnection->disconnected;
@ -1284,9 +1259,12 @@ void IotMqtt_Disconnect( IotMqttConnection_t mqttConnection,
if( disconnected == true ) if( disconnected == true )
{ {
goto cleanup; status = IOT_MQTT_STATUS_PENDING;
}
} }
if( status == IOT_MQTT_SUCCESS )
{
IotLogInfo( "(MQTT connection %p) Disconnecting connection.", mqttConnection ); IotLogInfo( "(MQTT connection %p) Disconnecting connection.", mqttConnection );
/* Create a DISCONNECT operation. This function blocks until the DISCONNECT /* Create a DISCONNECT operation. This function blocks until the DISCONNECT
@ -1312,6 +1290,7 @@ void IotMqtt_Disconnect( IotMqttConnection_t mqttConnection,
status = _getMqttDisconnectSerializer( mqttConnection->pSerializer )( &( pOperation->u.operation.pMqttPacket ), status = _getMqttDisconnectSerializer( mqttConnection->pSerializer )( &( pOperation->u.operation.pMqttPacket ),
&( pOperation->u.operation.packetSize ) ); &( pOperation->u.operation.packetSize ) );
} }
}
if( status == IOT_MQTT_SUCCESS ) if( status == IOT_MQTT_SUCCESS )
{ {
@ -1343,10 +1322,6 @@ void IotMqtt_Disconnect( IotMqttConnection_t mqttConnection,
} }
} }
/* This function has no return value and no cleanup, but uses the cleanup
* label to exit on error. */
cleanup:
if( initCalled == true ) if( initCalled == true )
{ {
/* Close the underlying network connection. This also cleans up keep-alive. /* Close the underlying network connection. This also cleans up keep-alive.
@ -1541,30 +1516,26 @@ IotMqttError_t IotMqtt_PublishAsync( IotMqttConnection_t mqttConnection,
if( _checkInit() == false ) if( _checkInit() == false )
{ {
status = IOT_MQTT_NOT_INITIALIZED; status = IOT_MQTT_NOT_INITIALIZED;
goto cleanup;
} }
else if( _IotMqtt_ValidatePublish( mqttConnection->awsIotMqttMode,
if( _IotMqtt_ValidatePublish( mqttConnection->awsIotMqttMode,
pPublishInfo, pPublishInfo,
flags, flags,
pCallbackInfo, pCallbackInfo,
pPublishOperation ) == false ) pPublishOperation ) == false )
{ {
status = IOT_MQTT_BAD_PARAMETER; status = IOT_MQTT_BAD_PARAMETER;
goto cleanup;
} }
else
{
/* Create a PUBLISH operation. */ /* Create a PUBLISH operation. */
status = _IotMqtt_CreateOperation( mqttConnection, status = _IotMqtt_CreateOperation( mqttConnection,
flags, flags,
pCallbackInfo, pCallbackInfo,
&pOperation ); &pOperation );
if( status != IOT_MQTT_SUCCESS )
{
goto cleanup;
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Check the PUBLISH operation data and set the operation type. */ /* Check the PUBLISH operation data and set the operation type. */
IotMqtt_Assert( pOperation->u.operation.status == IOT_MQTT_STATUS_PENDING ); IotMqtt_Assert( pOperation->u.operation.status == IOT_MQTT_STATUS_PENDING );
pOperation->u.operation.type = IOT_MQTT_PUBLISH_TO_SERVER; pOperation->u.operation.type = IOT_MQTT_PUBLISH_TO_SERVER;
@ -1581,12 +1552,10 @@ IotMqttError_t IotMqtt_PublishAsync( IotMqttConnection_t mqttConnection,
&( pOperation->u.operation.packetSize ), &( pOperation->u.operation.packetSize ),
&( pOperation->u.operation.packetIdentifier ), &( pOperation->u.operation.packetIdentifier ),
pPacketIdentifierHigh ); pPacketIdentifierHigh );
if( status != IOT_MQTT_SUCCESS )
{
goto cleanup;
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Check the serialized MQTT packet. */ /* Check the serialized MQTT packet. */
IotMqtt_Assert( pOperation->u.operation.pMqttPacket != NULL ); IotMqtt_Assert( pOperation->u.operation.pMqttPacket != NULL );
IotMqtt_Assert( pOperation->u.operation.packetSize > 0 ); IotMqtt_Assert( pOperation->u.operation.packetSize > 0 );
@ -1629,14 +1598,12 @@ IotMqttError_t IotMqtt_PublishAsync( IotMqttConnection_t mqttConnection,
{ {
_setOperationReference( pPublishOperation, IOT_MQTT_OPERATION_INITIALIZER ); _setOperationReference( pPublishOperation, IOT_MQTT_OPERATION_INITIALIZER );
} }
}
goto cleanup;
} }
} }
/* Clean up the PUBLISH operation if this function fails. Otherwise, set the /* Clean up the PUBLISH operation if this function fails. Otherwise, set the
* appropriate return code based on QoS. */ * appropriate return code based on QoS. */
cleanup:
if( status != IOT_MQTT_SUCCESS ) if( status != IOT_MQTT_SUCCESS )
{ {
@ -1711,18 +1678,17 @@ IotMqttError_t IotMqtt_Wait( IotMqttOperation_t operation,
if( _checkInit() == false ) if( _checkInit() == false )
{ {
status = IOT_MQTT_NOT_INITIALIZED; status = IOT_MQTT_NOT_INITIALIZED;
goto cleanup;
} }
/* Validate the given operation reference. */ /* Validate the given operation reference. */
if( _IotMqtt_ValidateOperation( operation ) == false ) else if( _IotMqtt_ValidateOperation( operation ) == false )
{ {
status = IOT_MQTT_BAD_PARAMETER; status = IOT_MQTT_BAD_PARAMETER;
goto cleanup;
} }
else
{
/* Check the MQTT connection status. */ /* Check the MQTT connection status. */
pMqttConnection = operation->pMqttConnection; pMqttConnection = operation->pMqttConnection;
}
if( status == IOT_MQTT_SUCCESS ) if( status == IOT_MQTT_SUCCESS )
{ {
@ -1761,7 +1727,6 @@ IotMqttError_t IotMqtt_Wait( IotMqttOperation_t operation,
} }
} }
cleanup:
return status; return status;
} }

View File

@ -454,10 +454,11 @@ IotMqttError_t _IotMqtt_CreateOperation( _mqttConnection_t * pMqttConnection,
IotLogError( "Callback should not be set for a waitable operation." ); IotLogError( "Callback should not be set for a waitable operation." );
status = IOT_MQTT_BAD_PARAMETER; status = IOT_MQTT_BAD_PARAMETER;
goto cleanup;
} }
} }
if( status == IOT_MQTT_SUCCESS )
{
IotLogDebug( "(MQTT connection %p) Creating new operation record.", IotLogDebug( "(MQTT connection %p) Creating new operation record.",
pMqttConnection ); pMqttConnection );
@ -470,14 +471,16 @@ IotMqttError_t _IotMqtt_CreateOperation( _mqttConnection_t * pMqttConnection,
pMqttConnection ); pMqttConnection );
status = IOT_MQTT_NETWORK_ERROR; status = IOT_MQTT_NETWORK_ERROR;
goto cleanup;
} }
else else
{ {
/* Reference count will need to be decremented on error. */ /* Reference count will need to be decremented on error. */
decrementOnError = true; decrementOnError = true;
} }
}
if( status == IOT_MQTT_SUCCESS )
{
/* Allocate memory for a new operation. */ /* Allocate memory for a new operation. */
pOperation = IotMqtt_MallocOperation( sizeof( _mqttOperation_t ) ); pOperation = IotMqtt_MallocOperation( sizeof( _mqttOperation_t ) );
@ -488,7 +491,6 @@ IotMqttError_t _IotMqtt_CreateOperation( _mqttConnection_t * pMqttConnection,
pMqttConnection ); pMqttConnection );
status = IOT_MQTT_NO_MEMORY; status = IOT_MQTT_NO_MEMORY;
goto cleanup;
} }
else else
{ {
@ -501,7 +503,10 @@ IotMqttError_t _IotMqtt_CreateOperation( _mqttConnection_t * pMqttConnection,
pOperation->u.operation.flags = flags; pOperation->u.operation.flags = flags;
pOperation->u.operation.status = IOT_MQTT_STATUS_PENDING; pOperation->u.operation.status = IOT_MQTT_STATUS_PENDING;
} }
}
if( status == IOT_MQTT_SUCCESS )
{
/* Check if the waitable flag is set. If it is, create a semaphore to /* Check if the waitable flag is set. If it is, create a semaphore to
* wait on. */ * wait on. */
if( waitable == true ) if( waitable == true )
@ -514,7 +519,6 @@ IotMqttError_t _IotMqtt_CreateOperation( _mqttConnection_t * pMqttConnection,
pMqttConnection ); pMqttConnection );
status = IOT_MQTT_NO_MEMORY; status = IOT_MQTT_NO_MEMORY;
goto cleanup;
} }
else else
{ {
@ -533,6 +537,8 @@ IotMqttError_t _IotMqtt_CreateOperation( _mqttConnection_t * pMqttConnection,
} }
} }
if( status == IOT_MQTT_SUCCESS )
{
/* Add this operation to the MQTT connection's operation list. */ /* Add this operation to the MQTT connection's operation list. */
IotMutex_Lock( &( pMqttConnection->referencesMutex ) ); IotMutex_Lock( &( pMqttConnection->referencesMutex ) );
IotListDouble_InsertHead( &( pMqttConnection->pendingProcessing ), IotListDouble_InsertHead( &( pMqttConnection->pendingProcessing ),
@ -541,9 +547,10 @@ IotMqttError_t _IotMqtt_CreateOperation( _mqttConnection_t * pMqttConnection,
/* Set the output parameter. */ /* Set the output parameter. */
*pNewOperation = pOperation; *pNewOperation = pOperation;
}
}
/* Clean up operation and decrement reference count if this function failed. */ /* Clean up operation and decrement reference count if this function failed. */
cleanup:
if( status != IOT_MQTT_SUCCESS ) if( status != IOT_MQTT_SUCCESS )
{ {

View File

@ -162,13 +162,10 @@ static bool _matchEndWildcards( const char * pTopicFilter,
{ {
/* Determine if the topic filter ends with the '#' wildcard. */ /* Determine if the topic filter ends with the '#' wildcard. */
status = ( pTopicFilter[ filterIndex + 1 ] == '/' ) && ( pTopicFilter[ filterIndex + 2 ] == '#' ); status = ( pTopicFilter[ filterIndex + 1 ] == '/' ) && ( pTopicFilter[ filterIndex + 2 ] == '#' );
}
if( status == true ) if( status == false )
{ {
goto cleanup;
}
}
/* Determine if the last character is reached for both topic name and topic /* Determine if the last character is reached for both topic name and topic
* filter for the '+' wildcard. */ * filter for the '+' wildcard. */
endChar = ( nameIndex == topicNameLength - 1 ) && ( filterIndex == topicFilterLength - 2 ); endChar = ( nameIndex == topicNameLength - 1 ) && ( filterIndex == topicFilterLength - 2 );
@ -178,8 +175,8 @@ static bool _matchEndWildcards( const char * pTopicFilter,
/* Filter "sport/+" also matches the "sport/" but not "sport". */ /* Filter "sport/+" also matches the "sport/" but not "sport". */
status = ( pTopicFilter[ filterIndex + 1 ] == '+' ); status = ( pTopicFilter[ filterIndex + 1 ] == '+' );
} }
}
cleanup:
*pMatch = status; *pMatch = status;
return status; return status;
@ -251,7 +248,7 @@ static bool _topicFilterMatch( const char * pTopicName,
filterIndex, filterIndex,
&status ) == true ) &status ) == true )
{ {
goto cleanup; break;
} }
} }
else else
@ -264,7 +261,7 @@ static bool _topicFilterMatch( const char * pTopicName,
&nameIndex, &nameIndex,
&status ) == true ) &status ) == true )
{ {
goto cleanup; break;
} }
} }
@ -273,14 +270,12 @@ static bool _topicFilterMatch( const char * pTopicName,
filterIndex++; filterIndex++;
} }
/* If the end of both strings has been reached, they match. */ if( status == false )
if( ( nameIndex == topicNameLength ) && ( filterIndex == topicFilterLength ) )
{ {
status = true; /* If the end of both strings has been reached, they match. */
status = ( ( nameIndex == topicNameLength ) && ( filterIndex == topicFilterLength ) );
} }
cleanup:
return status; return status;
} }