diff --git a/source/ota.c b/source/ota.c index b252f3924..4bad18b2f 100644 --- a/source/ota.c +++ b/source/ota.c @@ -3094,9 +3094,6 @@ OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer, */ otaAgent.pOtaInterface = pOtaInterfaces; - /* Initialize application buffers. */ - initializeAppBuffers( pOtaBuffer ); - /* Initialize local buffers. */ initializeLocalBuffers(); @@ -3113,26 +3110,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" ) ); + } } } 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. */