Skip to content

Commit e12ac90

Browse files
Update assert definition
1 parent 9f6a926 commit e12ac90

File tree

4 files changed

+188
-61
lines changed

4 files changed

+188
-61
lines changed

loop_invariants.patch

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ index 901b2e1..8bdd89c 100644
2525
* @brief Advance buffer index beyond whitespace.
2626
*
2727
@@ -78,6 +93,9 @@ static void skipSpace( const char * buf,
28-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
28+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
2929

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

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

188188
while( i < queryLength )
189189
+ assigns( i, start, queryStart, value, length )

source/core_json.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static void skipSpace( const char * buf,
7474
{
7575
size_t i = 0U;
7676

77-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
77+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
7878

7979
for( i = *start; i < max; i++ )
8080
{
@@ -132,7 +132,7 @@ static bool shortestUTF8( size_t length,
132132
bool ret = false;
133133
uint32_t min = 0U, max = 0U;
134134

135-
assert_param( ( length >= 2U ) && ( length <= 4U ) );
135+
coreJSON_ASSERT( ( length >= 2U ) && ( length <= 4U ) );
136136

137137
switch( length )
138138
{
@@ -193,11 +193,11 @@ static bool skipUTF8MultiByte( const char * buf,
193193
uint32_t value = 0U;
194194
char_ c;
195195

196-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
196+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
197197

198198
i = *start;
199-
assert_param( i < max );
200-
assert_param( !isascii_( buf[ i ] ) );
199+
coreJSON_ASSERT( i < max );
200+
coreJSON_ASSERT( !isascii_( buf[ i ] ) );
201201

202202
c.c = buf[ i ];
203203

@@ -254,7 +254,7 @@ static bool skipUTF8( const char * buf,
254254
{
255255
bool ret = false;
256256

257-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
257+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
258258

259259
if( *start < max )
260260
{
@@ -331,8 +331,8 @@ static bool skipOneHexEscape( const char * buf,
331331
size_t i = 0U, end = 0U;
332332
uint16_t value = 0U;
333333

334-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
335-
assert_param( outValue != NULL );
334+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
335+
coreJSON_ASSERT( outValue != NULL );
336336

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

397-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
397+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
398398

399399
i = *start;
400400

@@ -445,7 +445,7 @@ static bool skipEscape( const char * buf,
445445
bool ret = false;
446446
size_t i = 0U;
447447

448-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
448+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
449449

450450
i = *start;
451451

@@ -512,7 +512,7 @@ static bool skipString( const char * buf,
512512
bool ret = false;
513513
size_t i = 0;
514514

515-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
515+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
516516

517517
i = *start;
518518

@@ -576,7 +576,7 @@ static bool strnEq( const char * a,
576576
{
577577
size_t i = 0U;
578578

579-
assert_param( ( a != NULL ) && ( b != NULL ) );
579+
coreJSON_ASSERT( ( a != NULL ) && ( b != NULL ) );
580580

581581
for( i = 0; i < n; i++ )
582582
{
@@ -609,8 +609,8 @@ static bool skipLiteral( const char * buf,
609609
{
610610
bool ret = false;
611611

612-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
613-
assert_param( literal != NULL );
612+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
613+
coreJSON_ASSERT( literal != NULL );
614614

615615
if( ( *start < max ) && ( length <= ( max - *start ) ) )
616616
{
@@ -689,7 +689,7 @@ static bool skipDigits( const char * buf,
689689
size_t i = 0U, saveStart = 0U;
690690
int32_t value = 0;
691691

692-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
692+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
693693

694694
saveStart = *start;
695695

@@ -742,7 +742,7 @@ static void skipDecimals( const char * buf,
742742
{
743743
size_t i = 0U;
744744

745-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
745+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
746746

747747
i = *start;
748748

@@ -770,7 +770,7 @@ static void skipExponent( const char * buf,
770770
{
771771
size_t i = 0U;
772772

773-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
773+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
774774

775775
i = *start;
776776

@@ -807,7 +807,7 @@ static bool skipNumber( const char * buf,
807807
bool ret = false;
808808
size_t i = 0U;
809809

810-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
810+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
811811

812812
i = *start;
813813

@@ -903,7 +903,7 @@ static bool skipSpaceAndComma( const char * buf,
903903
bool ret = false;
904904
size_t i = 0U;
905905

906-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
906+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
907907

908908
skipSpace( buf, start, max );
909909
i = *start;
@@ -938,7 +938,7 @@ static void skipArrayScalars( const char * buf,
938938
{
939939
size_t i = 0U;
940940

941-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
941+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
942942

943943
i = *start;
944944

@@ -980,7 +980,7 @@ static void skipObjectScalars( const char * buf,
980980
size_t i = 0U;
981981
bool comma = false;
982982

983-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
983+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
984984

985985
i = *start;
986986

@@ -1035,7 +1035,7 @@ static void skipScalars( const char * buf,
10351035
size_t max,
10361036
char mode )
10371037
{
1038-
assert_param( isOpenBracket_( mode ) );
1038+
coreJSON_ASSERT( isOpenBracket_( mode ) );
10391039

10401040
skipSpace( buf, start, max );
10411041

@@ -1076,7 +1076,7 @@ static JSONStatus_t skipCollection( const char * buf,
10761076
int16_t depth = -1;
10771077
size_t i = 0U;
10781078

1079-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
1079+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
10801080

10811081
i = *start;
10821082

@@ -1221,8 +1221,8 @@ static bool nextValue( const char * buf,
12211221
bool ret = true;
12221222
size_t i = 0U, valueStart = 0U;
12231223

1224-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
1225-
assert_param( ( value != NULL ) && ( valueLength != NULL ) );
1224+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
1225+
coreJSON_ASSERT( ( value != NULL ) && ( valueLength != NULL ) );
12261226

12271227
i = *start;
12281228
valueStart = i;
@@ -1278,9 +1278,9 @@ static bool nextKeyValuePair( const char * buf,
12781278
bool ret = true;
12791279
size_t i = 0U, keyStart = 0U;
12801280

1281-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
1282-
assert_param( ( key != NULL ) && ( keyLength != NULL ) );
1283-
assert_param( ( value != NULL ) && ( valueLength != NULL ) );
1281+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
1282+
coreJSON_ASSERT( ( key != NULL ) && ( keyLength != NULL ) );
1283+
coreJSON_ASSERT( ( value != NULL ) && ( valueLength != NULL ) );
12841284

12851285
i = *start;
12861286
keyStart = i;
@@ -1351,8 +1351,8 @@ static bool objectSearch( const char * buf,
13511351

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

1354-
assert_param( ( buf != NULL ) && ( query != NULL ) );
1355-
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
1354+
coreJSON_ASSERT( ( buf != NULL ) && ( query != NULL ) );
1355+
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );
13561356

13571357
skipSpace( buf, &i, max );
13581358

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

1421-
assert_param( buf != NULL );
1422-
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
1421+
coreJSON_ASSERT( buf != NULL );
1422+
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );
14231423

14241424
skipSpace( buf, &i, max );
14251425

@@ -1486,8 +1486,8 @@ static bool skipQueryPart( const char * buf,
14861486
bool ret = false;
14871487
size_t i = 0U;
14881488

1489-
assert_param( ( buf != NULL ) && ( start != NULL ) && ( outLength != NULL ) );
1490-
assert_param( max > 0U );
1489+
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( outLength != NULL ) );
1490+
coreJSON_ASSERT( max > 0U );
14911491

14921492
i = *start;
14931493

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

1538-
assert_param( ( buf != NULL ) && ( query != NULL ) );
1539-
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
1540-
assert_param( ( max > 0U ) && ( queryLength > 0U ) );
1538+
coreJSON_ASSERT( ( buf != NULL ) && ( query != NULL ) );
1539+
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );
1540+
coreJSON_ASSERT( ( max > 0U ) && ( queryLength > 0U ) );
15411541

15421542
while( i < queryLength )
15431543
{
@@ -1747,10 +1747,10 @@ static JSONStatus_t iterate( const char * buf,
17471747
JSONStatus_t ret = JSONNotFound;
17481748
bool found = false;
17491749

1750-
assert_param( ( buf != NULL ) && ( max > 0U ) );
1751-
assert_param( ( start != NULL ) && ( next != NULL ) );
1752-
assert_param( ( outKey != NULL ) && ( outKeyLength != NULL ) );
1753-
assert_param( ( outValue != NULL ) && ( outValueLength != NULL ) );
1750+
coreJSON_ASSERT( ( buf != NULL ) && ( max > 0U ) );
1751+
coreJSON_ASSERT( ( start != NULL ) && ( next != NULL ) );
1752+
coreJSON_ASSERT( ( outKey != NULL ) && ( outKeyLength != NULL ) );
1753+
coreJSON_ASSERT( ( outValue != NULL ) && ( outValueLength != NULL ) );
17541754

17551755
if( *start < max )
17561756
{

source/include/core_json.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#ifndef CORE_JSON_H_
3131
#define CORE_JSON_H_
3232

33+
#include <assert.h>
3334
#include <stdbool.h>
3435
#include <stddef.h>
3536

@@ -40,25 +41,14 @@
4041
/* *INDENT-ON* */
4142

4243
/**
43-
* @brief When set to 1, config_assert is defined to sit in a loop if an
44-
* assertion fails. When set to 0, config_assert() is defined as a NOP.
45-
* It is useful to set this to 1 while developing your application for
46-
* debugging purposes.
47-
*/
48-
#define coreJSON_ASSERT_DEFINED ( 0 )
49-
50-
#if ( coreJSON_ASSERT_DEFINED == 1 )
51-
52-
/**
53-
* @brief Define assert_param() to sit in a loop if an assertion fails */
54-
#define assert_param( expr ) if( ( expr ) == ( bool ) 0 ) { for( ; ; ) {} }
55-
#else
56-
57-
/**
58-
* @brief Define assert_param() as a NOP */
59-
#define assert_param( expr ) ( ( void ) 0 )
44+
* @brief By default, has the stand behavior of assert() for
45+
* parameter checking. To swap out the assert(), define this
46+
* macro with the desired behavior. */
47+
#ifndef coreJSON_ASSERT
48+
#define coreJSON_ASSERT(expr) assert(expr)
6049
#endif
6150

51+
6252
/**
6353
* @ingroup json_enum_types
6454
* @brief Return codes from coreJSON library functions.

0 commit comments

Comments
 (0)