Skip to content

Commit

Permalink
jobs: more asserts and static_asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanTheGeek committed Dec 15, 2023
1 parent ec1ae74 commit b6d1214
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions source/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@
#endif
#define CONST_STRLEN( x ) ( sizeof( ( x ) ) - 1U )

/**
* @brief Get the length on an array.
*/
#ifdef ARRAY_LENGTH
#undef ARRAY_LENGTH
#endif
#define ARRAY_LENGTH( x ) ( sizeof( ( x ) ) / sizeof( ( x )[ 0 ] ) )

/**
* @brief use static_assert if available, otherwise use pre C11 workaround.
*/
#if defined( __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 201112L )
#include <assert.h>
#else
#define static_assert( expr, msg ) typedef char static_assertion_##msg[ ( expr ) ? 1 : -1 ]
#endif

/**
* @brief Table of topic API strings in JobsTopic_t order.
*/
Expand Down Expand Up @@ -817,7 +834,7 @@ size_t Jobs_UpdateMsg( JobCurrentStatus_t status,
char * buffer,
size_t bufferSize )
{
static const char * const jobStatusString[ 5U ] =
static const char * const jobStatusString[] =
{
"QUEUED",
"IN_PROGRESS",
Expand All @@ -826,7 +843,7 @@ size_t Jobs_UpdateMsg( JobCurrentStatus_t status,
"REJECTED"
};

static const size_t jobStatusStringLengths[ 5U ] =
static const size_t jobStatusStringLengths[] =
{
CONST_STRLEN( "QUEUED" ),
CONST_STRLEN( "IN_PROGRESS" ),
Expand All @@ -835,6 +852,11 @@ size_t Jobs_UpdateMsg( JobCurrentStatus_t status,
CONST_STRLEN( "REJECTED" )
};

static_assert( ARRAY_LENGTH( jobStatusString ) == ARRAY_LENGTH( jobStatusStringLengths ),
"jobStatusString and jobStatusStringLengths must have the same number of elements." );

assert( ( ( size_t ) status ) < ARRAY_LENGTH( jobStatusString ) );

size_t start = 0U;

if( ( expectedVersion != NULL ) && ( expectedVersionLength > 0U ) && ( bufferSize >=
Expand Down Expand Up @@ -867,18 +889,23 @@ bool Jobs_IsJobUpdateStatus( const char * topic,
const size_t thingNameLength,
JobUpdateStatus_t expectedStatus )
{
static const char * const jobUpdateStatusString[ 2U ] =
static const char * const jobUpdateStatusString[] =
{
"accepted",
"rejected"
};

static const size_t jobUpdateStatusStringLengths[ 2U ] =
static const size_t jobUpdateStatusStringLengths[] =
{
CONST_STRLEN( "accepted" ),
CONST_STRLEN( "rejected" )
};

static_assert( ARRAY_LENGTH( jobUpdateStatusString ) == ARRAY_LENGTH( jobUpdateStatusStringLengths ),
"jobUpdateStatusString and jobUpdateStatusStringLengths must have the same number of elements." );

assert( ( ( size_t ) expectedStatus ) < ARRAY_LENGTH( jobUpdateStatusString ) );

/* Max suffix size = max topic size - "$aws/<thingname>" prefix */
size_t suffixBufferLength = ( TOPIC_BUFFER_SIZE - CONST_STRLEN( "$aws/<thingname>" ) );
char suffixBuffer[ TOPIC_BUFFER_SIZE - CONST_STRLEN( "$aws/<thingname>" ) ] = { '\0' };
Expand Down

0 comments on commit b6d1214

Please sign in to comment.