Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MISRA fixes #153

Merged
merged 12 commits into from
Mar 13, 2024
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 @@ -1036,7 +1035,7 @@ static void skipScalars( const char * buf,
size_t max,
char mode )
{
assert( isOpenBracket_( mode ) );
coreJSON_ASSERT( isOpenBracket_( mode ) );

skipSpace( buf, start, max );

Expand Down Expand Up @@ -1077,7 +1076,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 @@ -1222,8 +1221,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 @@ -1279,9 +1278,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 @@ -1352,8 +1351,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 @@ -1419,8 +1418,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 @@ -1487,8 +1486,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 @@ -1536,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( ( 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 @@ -1747,10 +1746,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
Loading