Skip to content

Commit

Permalink
Fix stringBuilderUInt32Decimal to handle 0 value (#475)
Browse files Browse the repository at this point in the history
* Fix stringBuilderUInt32Decimal to handle 0 value

Previously the stringBuilder method would return
and empty string when the integer value of '0' was
supplied. With this change, the string representation
"0" is returned.
  • Loading branch information
kstribrnAmzn authored Feb 21, 2023
1 parent 3488f6b commit e5c5fb4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
20 changes: 9 additions & 11 deletions source/ota_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ static size_t stringBuilder( char * pBuffer,
curLen += thisLength;
}

pBuffer[ curLen ] = '\0';

return curLen;
}

Expand All @@ -295,17 +297,16 @@ static size_t stringBuilderUInt32Decimal( char * pBuffer,
uint32_t valueCopy = value;
size_t size = 0;

/* Assert if there is not enough buffer space. */

assert( bufferSizeBytes >= U32_MAX_LEN );
( void ) bufferSizeBytes;
/* Assert if there is not enough buffer space. */
assert( bufferSizeBytes >= U32_MAX_LEN + 1 );

while( valueCopy > 0U )
do
{
*pCur = asciiDigits[ ( valueCopy % 10U ) ];
pCur++;
valueCopy /= 10U;
}
} while( valueCopy > 0U );

while( pCur > workBuf )
{
Expand All @@ -315,7 +316,6 @@ static size_t stringBuilderUInt32Decimal( char * pBuffer,
}

pDest[ size ] = '\0';
size++;
return size;
}

Expand All @@ -330,10 +330,9 @@ static size_t stringBuilderUInt32Hex( char * pBuffer,
uint32_t valueCopy = value;
size_t i;

/* Assert if there is not enough buffer space. */

assert( bufferSizeBytes >= U32_MAX_LEN );
( void ) bufferSizeBytes;
/* Assert if there is not enough buffer space. */
assert( bufferSizeBytes >= U32_MAX_LEN + 1 );

/* Render all 8 digits, including leading zeros. */
for( i = 0U; i < 8U; i++ )
Expand All @@ -351,7 +350,6 @@ static size_t stringBuilderUInt32Hex( char * pBuffer,
}

pDest[ size ] = '\0';
size++;
return size;
}

Expand Down Expand Up @@ -870,6 +868,7 @@ OtaErr_t requestJob_Mqtt( const OtaAgentContext_t * pAgentCtx )
uint32_t msgSize = 0;
uint16_t topicLen = 0;
uint32_t xThingNameLength = 0;
char reqCounterString[ U32_MAX_LEN + 1 ];
uint32_t reqCounterStringLength = 0;

/* NULL-terminated list of topic string parts. */
Expand All @@ -880,7 +879,6 @@ OtaErr_t requestJob_Mqtt( const OtaAgentContext_t * pAgentCtx )
MQTT_API_JOBS_NEXT_GET,
NULL
};
char reqCounterString[ U32_MAX_LEN + 1 ];

/* NULL-terminated list of payload parts */

Expand Down
40 changes: 40 additions & 0 deletions test/unit-test/ota_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,28 @@ static OtaMqttStatus_t stubMqttPublishOnlySuccedsIfTruncatedValue( const char *
return OtaMqttSuccess;
}

static OtaMqttStatus_t stubMqttPublishZeroBlocksReceived( const char * const unused_1,
uint16_t unused_2,
const char * msg,
uint32_t msgSize,
uint8_t unused_3 )
{
( void ) unused_1;
( void ) unused_2;
( void ) unused_3;

/* Maximum message size is 77 characters */
TEST_ASSERT_LESS_OR_EQUAL( 77U, msgSize );

char actual[ 19 ] = { 0 };

memcpy( actual, msg, 18U );

TEST_ASSERT_EQUAL_STRING( "{\"status\":\"IN_PROGRESS\",\"statusDetails\":{\"receive\":\"0/2\"}}", actual );

return OtaMqttSuccess;
}

OtaErr_t mockControlInterfaceRequestJobAlwaysFail( const OtaAgentContext_t * unused )
{
( void ) unused;
Expand Down Expand Up @@ -2646,6 +2668,24 @@ void test_OTA_ReceiveFileBlockCompleteMqttCountUpdateJobCalledTime()
TEST_ASSERT_EQUAL( OTA_TEST_FILE_NUM_BLOCKS, otaReceivedFileBlockNumber );
}

void test_OTA_UpdateJobStatus()
{
OtaErr_t err = OtaErrNone;

otaInitDefault();

/* Verify the conversion of the value 0U and 2U to a string */
otaInterfaces.mqtt.publish = stubMqttPublishZeroBlocksReceived;

/* Set the file size to an arbitrary value < block size to ensure 2 blocks for the OTA */
otaAgent.fileContext.fileSize = 100U;
/* With 2 blocks remaining, status message will say '0/2' blocks received */
otaAgent.fileContext.blocksRemaining = 2U;

err = updateJobStatus_Mqtt( &otaAgent, JobStatusInProgress, 0, 0 );
TEST_ASSERT_EQUAL( OtaErrNone, err );
}

void test_OTA_EventProcessingTask_ExitOnAbort()
{
OtaEventMsg_t otaEvent = { 0 };
Expand Down

0 comments on commit e5c5fb4

Please sign in to comment.