From c6ac8ff01c4d74019ff58c224583fa379964bb52 Mon Sep 17 00:00:00 2001 From: bujain Date: Thu, 9 Dec 2021 23:13:42 +0000 Subject: [PATCH 1/2] add checks for pOtaBuffer to be non_null --- source/ota.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/source/ota.c b/source/ota.c index b252f3924..d9fd7c54f 100644 --- a/source/ota.c +++ b/source/ota.c @@ -3072,6 +3072,8 @@ OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer, ( void ) pOtaEventStrings; /* For suppressing compiler-warning: unused variable. */ ( void ) pOtaAgentStateStrings; /* For suppressing compiler-warning: unused variable. */ + assert( pOtaBuffer != NULL ); + /* If OTA agent is stopped then start running. */ if( otaAgent.state == OtaAgentStateStopped ) { @@ -3094,9 +3096,6 @@ OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer, */ otaAgent.pOtaInterface = pOtaInterfaces; - /* Initialize application buffers. */ - initializeAppBuffers( pOtaBuffer ); - /* Initialize local buffers. */ initializeLocalBuffers(); @@ -3113,26 +3112,36 @@ OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer, */ ( void ) otaAgent.pOtaInterface->os.event.init( NULL ); - if( pThingName == NULL ) + if( pOtaBuffer == NULL ) { - LogError( ( "Error: Thing name is NULL.\r\n" ) ); + LogError( ( "Error: pOtaBuffer is NULL.\r\n" ) ); } else { - uint32_t strLength = ( uint32_t ) ( strlen( ( const char * ) pThingName ) ); + /* Initialize application buffers. */ + initializeAppBuffers( pOtaBuffer ); - if( strLength <= otaconfigMAX_THINGNAME_LEN ) + if( pThingName == NULL ) { - /* - * Store the Thing name to be used for topics later. Include zero terminator - * when saving the Thing name. - */ - ( void ) memcpy( otaAgent.pThingName, pThingName, strLength + 1UL ); - returnStatus = OtaErrNone; + LogError( ( "Error: Thing name is NULL.\r\n" ) ); } else { - LogError( ( "Error: Thing name is too long.\r\n" ) ); + uint32_t strLength = ( uint32_t ) ( strlen( ( const char * ) pThingName ) ); + + if( strLength <= otaconfigMAX_THINGNAME_LEN ) + { + /* + * Store the Thing name to be used for topics later. Include zero terminator + * when saving the Thing name. + */ + ( void ) memcpy( otaAgent.pThingName, pThingName, strLength + 1UL ); + returnStatus = OtaErrNone; + } + else + { + LogError( ( "Error: Thing name is too long.\r\n" ) ); + } } } From d5d686bb7e7ff07bd8fef9787f0d068db99eb596 Mon Sep 17 00:00:00 2001 From: bujain Date: Thu, 9 Dec 2021 23:33:43 +0000 Subject: [PATCH 2/2] add unit test --- source/ota.c | 2 -- test/unit-test/ota_utest.c | 11 +++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/source/ota.c b/source/ota.c index d9fd7c54f..4bad18b2f 100644 --- a/source/ota.c +++ b/source/ota.c @@ -3072,8 +3072,6 @@ OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer, ( void ) pOtaEventStrings; /* For suppressing compiler-warning: unused variable. */ ( void ) pOtaAgentStateStrings; /* For suppressing compiler-warning: unused variable. */ - assert( pOtaBuffer != NULL ); - /* If OTA agent is stopped then start running. */ if( otaAgent.state == OtaAgentStateStopped ) { diff --git a/test/unit-test/ota_utest.c b/test/unit-test/ota_utest.c index d59e433e5..b0dbdd656 100644 --- a/test/unit-test/ota_utest.c +++ b/test/unit-test/ota_utest.c @@ -972,6 +972,17 @@ void test_OTA_InitNullAppBuffers() TEST_ASSERT_EQUAL( OtaAgentStateInit, OTA_GetState() ); } +void test_OTA_InitNullOtaAppPointer() +{ + OtaAppBuffer_t * otaAppBuffer = NULL; + + OTA_Init( otaAppBuffer, + &otaInterfaces, + ( const uint8_t * ) pOtaDefaultClientId, + mockAppCallback ); + TEST_ASSERT_EQUAL( OtaAgentStateStopped, OTA_GetState() ); +} + void test_OTA_InitZeroAppBufferSizes() { /* Test for having valid pointers with zero sizes. */