diff --git a/loop_invariants.patch b/loop_invariants.patch index a9326956..1960cf24 100644 --- a/loop_invariants.patch +++ b/loop_invariants.patch @@ -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 ) @@ -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 ) @@ -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 ) diff --git a/source/core_json.c b/source/core_json.c index 8f00cc33..27755885 100644 --- a/source/core_json.c +++ b/source/core_json.c @@ -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++ ) { @@ -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 ) { @@ -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 ]; @@ -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 ) { @@ -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 */ @@ -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; @@ -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; @@ -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; @@ -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++ ) { @@ -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 ) ) ) { @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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 ); @@ -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; @@ -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; @@ -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; @@ -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 ); @@ -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 ); @@ -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; @@ -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 ) { @@ -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 ) { diff --git a/source/include/core_json.h b/source/include/core_json.h index da8f2354..aa3fa76d 100644 --- a/source/include/core_json.h +++ b/source/include/core_json.h @@ -30,6 +30,7 @@ #ifndef CORE_JSON_H_ #define CORE_JSON_H_ +#include #include #include @@ -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. diff --git a/test/unit-test/core_json_utest.c b/test/unit-test/core_json_utest.c index 22afdf86..dac4e6bd 100644 --- a/test/unit-test/core_json_utest.c +++ b/test/unit-test/core_json_utest.c @@ -1715,6 +1715,143 @@ void test_JSON_Max_Depth( void ) free( maxNestedObject ); } +/** + * @brief Trip all asserts in internal functions. + */ +void test_JSON_asserts( void ) +{ + char buf[] = "x", queryKey[] = "y"; + size_t start = 1, max = 1, length = 1, next = 0; + uint16_t u = 0; + size_t key, keyLength, value, valueLength; + int32_t queryIndex = 0; + + catch_assert( skipSpace( NULL, &start, max ) ); + catch_assert( skipSpace( buf, NULL, max ) ); + /* assert: max != 0 */ + catch_assert( skipSpace( buf, &start, 0 ) ); + + /* first argument is length; assert: 2 <= length <= 4 */ + catch_assert( shortestUTF8( 1, u ) ); + catch_assert( shortestUTF8( 5, u ) ); + + catch_assert( skipUTF8MultiByte( NULL, &start, max ) ); + catch_assert( skipUTF8MultiByte( buf, NULL, max ) ); + catch_assert( skipUTF8MultiByte( buf, &start, 0 ) ); + /* assert: start < max */ + catch_assert( skipUTF8MultiByte( buf, &start, max ) ); + /* assert: buf[0] < '\0' */ + catch_assert( skipUTF8MultiByte( buf, &start, ( start + 1 ) ) ); + + catch_assert( skipUTF8( NULL, &start, max ) ); + catch_assert( skipUTF8( buf, NULL, max ) ); + catch_assert( skipUTF8( buf, &start, 0 ) ); + + catch_assert( skipOneHexEscape( NULL, &start, max, &u ) ); + catch_assert( skipOneHexEscape( buf, NULL, max, &u ) ); + catch_assert( skipOneHexEscape( buf, &start, 0, &u ) ); + catch_assert( skipOneHexEscape( buf, &start, max, NULL ) ); + + catch_assert( skipHexEscape( NULL, &start, max ) ); + catch_assert( skipHexEscape( buf, NULL, max ) ); + catch_assert( skipHexEscape( buf, &start, 0 ) ); + + catch_assert( skipEscape( NULL, &start, max ) ); + catch_assert( skipEscape( buf, NULL, max ) ); + catch_assert( skipEscape( buf, &start, 0 ) ); + + catch_assert( skipString( NULL, &start, max ) ); + catch_assert( skipString( buf, NULL, max ) ); + catch_assert( skipString( buf, &start, 0 ) ); + + catch_assert( strnEq( NULL, buf, max ) ); + catch_assert( strnEq( buf, NULL, max ) ); + + catch_assert( skipLiteral( NULL, &start, max, "lit", length ) ); + catch_assert( skipLiteral( buf, NULL, max, "lit", length ) ); + catch_assert( skipLiteral( buf, &start, 0, "lit", length ) ); + catch_assert( skipLiteral( buf, &start, max, NULL, length ) ); + + catch_assert( skipDigits( NULL, &start, max, NULL ) ); + catch_assert( skipDigits( buf, NULL, max, NULL ) ); + catch_assert( skipDigits( buf, &start, 0, NULL ) ); + + catch_assert( skipDecimals( NULL, &start, max ) ); + catch_assert( skipDecimals( buf, NULL, max ) ); + catch_assert( skipDecimals( buf, &start, 0 ) ); + + catch_assert( skipExponent( NULL, &start, max ) ); + catch_assert( skipExponent( buf, NULL, max ) ); + catch_assert( skipExponent( buf, &start, 0 ) ); + + catch_assert( skipNumber( NULL, &start, max ) ); + catch_assert( skipNumber( buf, NULL, max ) ); + catch_assert( skipNumber( buf, &start, 0 ) ); + + catch_assert( skipSpaceAndComma( NULL, &start, max ) ); + catch_assert( skipSpaceAndComma( buf, NULL, max ) ); + catch_assert( skipSpaceAndComma( buf, &start, 0 ) ); + + catch_assert( skipArrayScalars( NULL, &start, max ) ); + catch_assert( skipArrayScalars( buf, NULL, max ) ); + catch_assert( skipArrayScalars( buf, &start, 0 ) ); + + catch_assert( skipObjectScalars( NULL, &start, max ) ); + catch_assert( skipObjectScalars( buf, NULL, max ) ); + catch_assert( skipObjectScalars( buf, &start, 0 ) ); + + /* assert: mode is '[' or '{' */ + catch_assert( skipScalars( buf, &start, max, '(' ) ); + + catch_assert( skipCollection( NULL, &start, max ) ); + catch_assert( skipCollection( buf, NULL, max ) ); + catch_assert( skipCollection( buf, &start, 0 ) ); + + catch_assert( nextValue( NULL, &start, max, &value, &valueLength ) ); + catch_assert( nextValue( buf, NULL, max, &value, &valueLength ) ); + catch_assert( nextValue( buf, &start, 0, &value, &valueLength ) ); + catch_assert( nextValue( buf, &start, max, NULL, &valueLength ) ); + catch_assert( nextValue( buf, &start, max, &value, NULL ) ); + + catch_assert( nextKeyValuePair( NULL, &start, max, &key, &keyLength, &value, &valueLength ) ); + catch_assert( nextKeyValuePair( buf, NULL, max, &key, &keyLength, &value, &valueLength ) ); + catch_assert( nextKeyValuePair( buf, &start, 0, &key, &keyLength, &value, &valueLength ) ); + catch_assert( nextKeyValuePair( buf, &start, max, NULL, &keyLength, &value, &valueLength ) ); + catch_assert( nextKeyValuePair( buf, &start, max, &key, NULL, &value, &valueLength ) ); + catch_assert( nextKeyValuePair( buf, &start, max, &key, &keyLength, NULL, &valueLength ) ); + catch_assert( nextKeyValuePair( buf, &start, max, &key, &keyLength, &value, NULL ) ); + + catch_assert( objectSearch( NULL, max, queryKey, keyLength, &value, &valueLength ) ); + catch_assert( objectSearch( buf, max, NULL, keyLength, &value, &valueLength ) ); + catch_assert( objectSearch( buf, max, queryKey, keyLength, NULL, &valueLength ) ); + catch_assert( objectSearch( buf, max, queryKey, keyLength, &value, NULL ) ); + + catch_assert( arraySearch( NULL, max, queryIndex, &value, &valueLength ) ); + catch_assert( arraySearch( buf, max, queryIndex, NULL, &valueLength ) ); + catch_assert( arraySearch( buf, max, queryIndex, &value, NULL ) ); + + catch_assert( skipQueryPart( NULL, &start, max, &valueLength ) ); + catch_assert( skipQueryPart( buf, NULL, max, &valueLength ) ); + catch_assert( skipQueryPart( buf, &start, 0, &valueLength ) ); + catch_assert( skipQueryPart( buf, &start, max, NULL ) ); + + catch_assert( multiSearch( NULL, max, queryKey, keyLength, &value, &valueLength ) ); + catch_assert( multiSearch( buf, 0, queryKey, keyLength, &value, &valueLength ) ); + catch_assert( multiSearch( buf, max, NULL, keyLength, &value, &valueLength ) ); + catch_assert( multiSearch( buf, max, queryKey, 0, &value, &valueLength ) ); + catch_assert( multiSearch( buf, max, queryKey, keyLength, NULL, &valueLength ) ); + catch_assert( multiSearch( buf, max, queryKey, keyLength, &value, NULL ) ); + + catch_assert( iterate( NULL, max, &start, &next, &key, &keyLength, &value, &valueLength ) ); + catch_assert( iterate( buf, 0, &start, &next, &key, &keyLength, &value, &valueLength ) ); + catch_assert( iterate( buf, max, NULL, &next, &key, &keyLength, &value, &valueLength ) ); + catch_assert( iterate( buf, max, &start, NULL, &key, &keyLength, &value, &valueLength ) ); + catch_assert( iterate( buf, max, &start, &next, NULL, &keyLength, &value, &valueLength ) ); + catch_assert( iterate( buf, max, &start, &next, &key, NULL, &value, &valueLength ) ); + catch_assert( iterate( buf, max, &start, &next, &key, &keyLength, NULL, &valueLength ) ); + catch_assert( iterate( buf, max, &start, &next, &key, &keyLength, &value, NULL ) ); +} + /** * @brief These checks are not otherwise reached. */