Skip to content

Commit

Permalink
Update assert definition
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleysmith23 committed Dec 11, 2023
1 parent 9f6a926 commit e12ac90
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 61 deletions.
6 changes: 3 additions & 3 deletions loop_invariants.patch
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ index 901b2e1..8bdd89c 100644
* @brief Advance buffer index beyond whitespace.
*
@@ -78,6 +93,9 @@ static void skipSpace( const char * buf,
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

for( i = *start; i < max; i++ )
+ assigns( i )
Expand Down Expand Up @@ -86,7 +86,7 @@ index 901b2e1..8bdd89c 100644
if( buf[ i ] == '"' )
{
@@ -580,6 +621,9 @@ static bool strnEq( const char * a,
assert_param( ( a != NULL ) && ( b != NULL ) );
coreJSON_ASSERT( ( a != NULL ) && ( b != NULL ) );

for( i = 0; i < n; i++ )
+ assigns( i )
Expand Down Expand Up @@ -183,7 +183,7 @@ index 901b2e1..8bdd89c 100644
i++;
}
@@ -1541,6 +1616,17 @@ static JSONStatus_t multiSearch( const char * buf,
assert_param( ( max > 0U ) && ( queryLength > 0U ) );
coreJSON_ASSERT( ( max > 0U ) && ( queryLength > 0U ) );

while( i < queryLength )
+ assigns( i, start, queryStart, value, length )
Expand Down
82 changes: 41 additions & 41 deletions source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void skipSpace( const char * buf,
{
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

for( i = *start; i < max; i++ )
{
Expand Down Expand Up @@ -132,7 +132,7 @@ static bool shortestUTF8( size_t length,
bool ret = false;
uint32_t min = 0U, max = 0U;

assert_param( ( length >= 2U ) && ( length <= 4U ) );
coreJSON_ASSERT( ( length >= 2U ) && ( length <= 4U ) );

switch( length )
{
Expand Down Expand Up @@ -193,11 +193,11 @@ static bool skipUTF8MultiByte( const char * buf,
uint32_t value = 0U;
char_ c;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;
assert_param( i < max );
assert_param( !isascii_( buf[ i ] ) );
coreJSON_ASSERT( i < max );
coreJSON_ASSERT( !isascii_( buf[ i ] ) );

c.c = buf[ i ];

Expand Down Expand Up @@ -254,7 +254,7 @@ static bool skipUTF8( const char * buf,
{
bool ret = false;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

if( *start < max )
{
Expand Down Expand Up @@ -331,8 +331,8 @@ static bool skipOneHexEscape( const char * buf,
size_t i = 0U, end = 0U;
uint16_t value = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert_param( outValue != NULL );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( outValue != NULL );

i = *start;
#define HEX_ESCAPE_LENGTH ( 6U ) /* e.g., \u1234 */
Expand Down Expand Up @@ -394,7 +394,7 @@ static bool skipHexEscape( const char * buf,
size_t i = 0U;
uint16_t value = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -445,7 +445,7 @@ static bool skipEscape( const char * buf,
bool ret = false;
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -512,7 +512,7 @@ static bool skipString( const char * buf,
bool ret = false;
size_t i = 0;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -576,7 +576,7 @@ static bool strnEq( const char * a,
{
size_t i = 0U;

assert_param( ( a != NULL ) && ( b != NULL ) );
coreJSON_ASSERT( ( a != NULL ) && ( b != NULL ) );

for( i = 0; i < n; i++ )
{
Expand Down Expand Up @@ -609,8 +609,8 @@ static bool skipLiteral( const char * buf,
{
bool ret = false;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert_param( literal != NULL );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( literal != NULL );

if( ( *start < max ) && ( length <= ( max - *start ) ) )
{
Expand Down Expand Up @@ -689,7 +689,7 @@ static bool skipDigits( const char * buf,
size_t i = 0U, saveStart = 0U;
int32_t value = 0;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

saveStart = *start;

Expand Down Expand Up @@ -742,7 +742,7 @@ static void skipDecimals( const char * buf,
{
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -770,7 +770,7 @@ static void skipExponent( const char * buf,
{
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -807,7 +807,7 @@ static bool skipNumber( const char * buf,
bool ret = false;
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -903,7 +903,7 @@ static bool skipSpaceAndComma( const char * buf,
bool ret = false;
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

skipSpace( buf, start, max );
i = *start;
Expand Down Expand Up @@ -938,7 +938,7 @@ static void skipArrayScalars( const char * buf,
{
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -980,7 +980,7 @@ static void skipObjectScalars( const char * buf,
size_t i = 0U;
bool comma = false;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -1035,7 +1035,7 @@ static void skipScalars( const char * buf,
size_t max,
char mode )
{
assert_param( isOpenBracket_( mode ) );
coreJSON_ASSERT( isOpenBracket_( mode ) );

skipSpace( buf, start, max );

Expand Down Expand Up @@ -1076,7 +1076,7 @@ static JSONStatus_t skipCollection( const char * buf,
int16_t depth = -1;
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;

Expand Down Expand Up @@ -1221,8 +1221,8 @@ static bool nextValue( const char * buf,
bool ret = true;
size_t i = 0U, valueStart = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert_param( ( value != NULL ) && ( valueLength != NULL ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( value != NULL ) && ( valueLength != NULL ) );

i = *start;
valueStart = i;
Expand Down Expand Up @@ -1278,9 +1278,9 @@ static bool nextKeyValuePair( const char * buf,
bool ret = true;
size_t i = 0U, keyStart = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert_param( ( key != NULL ) && ( keyLength != NULL ) );
assert_param( ( value != NULL ) && ( valueLength != NULL ) );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( key != NULL ) && ( keyLength != NULL ) );
coreJSON_ASSERT( ( value != NULL ) && ( valueLength != NULL ) );

i = *start;
keyStart = i;
Expand Down Expand Up @@ -1351,8 +1351,8 @@ static bool objectSearch( const char * buf,

size_t i = 0U, key = 0U, keyLength = 0U, value = 0U, valueLength = 0U;

assert_param( ( buf != NULL ) && ( query != NULL ) );
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
coreJSON_ASSERT( ( buf != NULL ) && ( query != NULL ) );
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );

skipSpace( buf, &i, max );

Expand Down Expand Up @@ -1418,8 +1418,8 @@ static bool arraySearch( const char * buf,
size_t i = 0U, value = 0U, valueLength = 0U;
uint32_t currentIndex = 0U;

assert_param( buf != NULL );
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
coreJSON_ASSERT( buf != NULL );
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );

skipSpace( buf, &i, max );

Expand Down Expand Up @@ -1486,8 +1486,8 @@ static bool skipQueryPart( const char * buf,
bool ret = false;
size_t i = 0U;

assert_param( ( buf != NULL ) && ( start != NULL ) && ( outLength != NULL ) );
assert_param( max > 0U );
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( outLength != NULL ) );
coreJSON_ASSERT( max > 0U );

i = *start;

Expand Down Expand Up @@ -1535,9 +1535,9 @@ static JSONStatus_t multiSearch( const char * buf,
JSONStatus_t ret = JSONSuccess;
size_t i = 0U, start = 0U, queryStart = 0U, value = 0U, length = max;

assert_param( ( buf != NULL ) && ( query != NULL ) );
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
assert_param( ( max > 0U ) && ( queryLength > 0U ) );
coreJSON_ASSERT( ( buf != NULL ) && ( query != NULL ) );
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );
coreJSON_ASSERT( ( max > 0U ) && ( queryLength > 0U ) );

while( i < queryLength )
{
Expand Down Expand Up @@ -1747,10 +1747,10 @@ static JSONStatus_t iterate( const char * buf,
JSONStatus_t ret = JSONNotFound;
bool found = false;

assert_param( ( buf != NULL ) && ( max > 0U ) );
assert_param( ( start != NULL ) && ( next != NULL ) );
assert_param( ( outKey != NULL ) && ( outKeyLength != NULL ) );
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
coreJSON_ASSERT( ( buf != NULL ) && ( max > 0U ) );
coreJSON_ASSERT( ( start != NULL ) && ( next != NULL ) );
coreJSON_ASSERT( ( outKey != NULL ) && ( outKeyLength != NULL ) );
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );

if( *start < max )
{
Expand Down
24 changes: 7 additions & 17 deletions source/include/core_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#ifndef CORE_JSON_H_
#define CORE_JSON_H_

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>

Expand All @@ -40,25 +41,14 @@
/* *INDENT-ON* */

/**
* @brief When set to 1, config_assert is defined to sit in a loop if an
* assertion fails. When set to 0, config_assert() is defined as a NOP.
* It is useful to set this to 1 while developing your application for
* debugging purposes.
*/
#define coreJSON_ASSERT_DEFINED ( 0 )

#if ( coreJSON_ASSERT_DEFINED == 1 )

/**
* @brief Define assert_param() to sit in a loop if an assertion fails */
#define assert_param( expr ) if( ( expr ) == ( bool ) 0 ) { for( ; ; ) {} }
#else

/**
* @brief Define assert_param() as a NOP */
#define assert_param( expr ) ( ( void ) 0 )
* @brief By default, has the stand behavior of assert() for
* parameter checking. To swap out the assert(), define this
* macro with the desired behavior. */
#ifndef coreJSON_ASSERT
#define coreJSON_ASSERT(expr) assert(expr)
#endif


/**
* @ingroup json_enum_types
* @brief Return codes from coreJSON library functions.
Expand Down
Loading

0 comments on commit e12ac90

Please sign in to comment.