Skip to content

Commit

Permalink
Merge branch 'main' into fix-assert-misra
Browse files Browse the repository at this point in the history
  • Loading branch information
chinglee-iot authored Mar 14, 2024
2 parents 70a0beb + 8401d38 commit ddd30bb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 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( ( 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( ( 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( ( max > 0U ) && ( queryLength > 0U ) );
coreJSON_ASSERT( ( max > 0U ) && ( queryLength > 0U ) );

while( i < queryLength )
+ assigns( i, start, queryStart, value, length )
Expand Down
87 changes: 43 additions & 44 deletions source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* @brief The source file that implements the user-facing functions in core_json.h.
*/

#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
Expand Down Expand Up @@ -75,7 +74,7 @@ static void skipSpace( const char * buf,
{
size_t i = 0U;

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

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

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

switch( length )
{
Expand Down Expand Up @@ -192,13 +191,13 @@ static bool skipUTF8MultiByte( const char * buf,
bool ret = false;
size_t i = 0U, bitCount = 0U, j = 0U;
uint32_t value = 0U;
char_ c = { 0 };
char_ c;

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

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

c.c = buf[ i ];

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

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

if( *start < max )
{
Expand Down Expand Up @@ -283,7 +282,7 @@ static bool skipUTF8( const char * buf,
#define NOT_A_HEX_CHAR ( 0x10U )
static uint8_t hexToInt( char c )
{
char_ n = { 0 };
char_ n;

n.c = c;

Expand Down Expand Up @@ -332,8 +331,8 @@ static bool skipOneHexEscape( const char * buf,
size_t i = 0U, end = 0U;
uint16_t value = 0U;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert( 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 @@ -395,7 +394,7 @@ static bool skipHexEscape( const char * buf,
size_t i = 0U;
uint16_t value = 0U;

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

i = *start;

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

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

i = *start;

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

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

i = *start;

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

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

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

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

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

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

saveStart = *start;

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

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

i = *start;

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

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

i = *start;

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

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

i = *start;

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

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

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

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

i = *start;

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

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

i = *start;

Expand Down Expand Up @@ -1041,7 +1040,7 @@ static void skipScalars( const char * buf,
/* assert function may be implemented in macro using a # or ## operator.
* Using a local variable here to prevent macro replacement is subjected
* to macro itself. */
assert( modeIsOpenBracket != false );
coreJSON_ASSERT( modeIsOpenBracket != false );

/* Adding this line to avoid unused variable warning in release mode. */
( void ) modeIsOpenBracket;
Expand Down Expand Up @@ -1085,7 +1084,7 @@ static JSONStatus_t skipCollection( const char * buf,
int16_t depth = -1;
size_t i = 0U;

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

i = *start;

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

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert( ( 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 @@ -1287,9 +1286,9 @@ static bool nextKeyValuePair( const char * buf,
bool ret = true;
size_t i = 0U, keyStart = 0U;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert( ( key != NULL ) && ( keyLength != NULL ) );
assert( ( 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 @@ -1360,8 +1359,8 @@ static bool objectSearch( const char * buf,

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

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

skipSpace( buf, &i, max );

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

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

skipSpace( buf, &i, max );

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

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

i = *start;

Expand Down Expand Up @@ -1544,9 +1543,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( ( buf != NULL ) && ( query != NULL ) );
assert( ( outValue != NULL ) && ( outValueLength != NULL ) );
assert( ( 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 @@ -1755,10 +1754,10 @@ static JSONStatus_t iterate( const char * buf,
JSONStatus_t ret = JSONNotFound;
bool found = false;

assert( ( buf != NULL ) && ( max > 0U ) );
assert( ( start != NULL ) && ( next != NULL ) );
assert( ( outKey != NULL ) && ( outKeyLength != NULL ) );
assert( ( 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
10 changes: 10 additions & 0 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 @@ -39,6 +40,15 @@
#endif
/* *INDENT-ON* */

/**
* @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

0 comments on commit ddd30bb

Please sign in to comment.