11diff --git a/source/core_json.c b/source/core_json.c
2- index 901b2e1..8bdd89c 100644
2+ index d8694f0..761c44b 100644
33--- a/source/core_json.c
44+++ b/source/core_json.c
5- @@ -62 ,6 +62 ,21 @@ typedef union
6- #define isSquareOpen_ ( x ) ( ( x ) == '[ ' )
7- #define isSquareClose_ ( x ) ( ( x ) == '] ' )
8-
5+ @@ -63 ,6 +63 ,21 @@ typedef union
6+ #define isCurlyOpen_ ( x ) ( ( x ) == '{ ' )
7+ #define isCurlyClose_ ( x ) ( ( x ) == '} ' )
8+
99+ /**
1010+ * Renaming all loop-contract clauses from CBMC for readability.
1111+ * For more information about loop contracts in CBMC, see
1212+ * https://diffblue.github.io/cbmc/contracts-user.html.
1313+ */
1414+ #ifdef CBMC
15- + #define loopInvariant(...) __CPROVER_loop_invariant(__VA_ARGS__)
16- + #define decreases(...) __CPROVER_decreases(__VA_ARGS__)
17- + #define assigns(...) __CPROVER_assigns(__VA_ARGS__)
15+ + #define loopInvariant(...) __CPROVER_loop_invariant(__VA_ARGS__)
16+ + #define decreases(...) __CPROVER_decreases(__VA_ARGS__)
17+ + #define assigns(...) __CPROVER_assigns(__VA_ARGS__)
1818+ #else
19- + #define loopInvariant(...)
20- + #define decreases(...)
21- + #define assigns(...)
19+ + #define loopInvariant(...)
20+ + #define decreases(...)
21+ + #define assigns(...)
2222+ #endif
2323+
2424 /**
2525 * @brief Advance buffer index beyond whitespace.
2626 *
27- @@ -78 ,6 +93 ,9 @@ static void skipSpace( const char * buf,
27+ @@ -79 ,6 +94 ,9 @@ static void skipSpace( const char * buf,
2828 coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
29-
29+
3030 for( i = *start; i < max; i++ )
3131+ assigns( i )
3232+ loopInvariant( *start <= i && i <= max )
3333+ decreases( max - i )
3434 {
3535 if( !isspace_( buf[ i ] ) )
3636 {
37- @@ -102 ,6 +120 ,13 @@ static size_t countHighBits( uint8_t c )
37+ @@ -103 ,6 +121 ,13 @@ static size_t countHighBits( uint8_t c )
3838 size_t i = 0;
39-
39+
4040 while( ( n & 0x80U ) != 0U )
4141+ assigns( i, n )
4242+ loopInvariant (
@@ -48,7 +48,7 @@ index 901b2e1..8bdd89c 100644
4848 {
4949 i++;
5050 n = ( n & 0x7FU ) << 1U;
51- @@ -210 ,6 +235 ,13 @@ static bool skipUTF8MultiByte( const char * buf,
51+ @@ -211 ,6 +236 ,13 @@ static bool skipUTF8MultiByte( const char * buf,
5252 /* The bit count is 1 greater than the number of bytes,
5353 * e.g., when j is 2, we skip one more byte. */
5454 for( j = bitCount - 1U; j > 0U; j-- )
@@ -61,8 +61,8 @@ index 901b2e1..8bdd89c 100644
6161+ decreases( j )
6262 {
6363 i++;
64-
65- @@ -345 ,6 +377 ,12 @@ static bool skipOneHexEscape( const char * buf,
64+
65+ @@ -346 ,6 +378 ,12 @@ static bool skipOneHexEscape( const char * buf,
6666 if( ( end < max ) && ( buf[ i ] == '\\' ) && ( buf[ i + 1U ] == 'u' ) )
6767 {
6868 for( i += 2U; i < end; i++ )
@@ -74,58 +74,50 @@ index 901b2e1..8bdd89c 100644
7474+ decreases( end - i )
7575 {
7676 uint8_t n = hexToInt( buf[ i ] );
77-
78- @@ -522 ,6 +560 ,9 @@ static bool skipString( const char * buf,
77+
78+ @@ -523 ,6 +561 ,9 @@ static bool skipString( const char * buf,
7979 i++;
80-
80+
8181 while( i < max )
8282+ assigns( i )
8383+ loopInvariant( *start + 1U <= i && i <= max )
8484+ decreases( max - i )
8585 {
8686 if( buf[ i ] == '"' )
8787 {
88- @@ -580 ,6 +621 ,9 @@ static bool strnEq( const char * a,
88+ @@ -581 ,6 +622 ,9 @@ static bool strnEq( const char * a,
8989 coreJSON_ASSERT( ( a != NULL ) && ( b != NULL ) );
90-
90+
9191 for( i = 0; i < n; i++ )
9292+ assigns( i )
9393+ loopInvariant( i <= n )
9494+ decreases( n - i )
9595 {
9696 if( a[ i ] != b[ i ] )
9797 {
98- @@ -681,6 +725,7 @@ static bool skipAnyLiteral( const char * buf,
99- * false otherwise.
100- */
101- #define MAX_FACTOR ( MAX_INDEX_VALUE / 10 )
102- +
103- static bool skipDigits( const char * buf,
104- size_t * start,
105- size_t max,
106- @@ -695,6 +740,9 @@ static bool skipDigits( const char * buf,
98+ @@ -696,6 +740,9 @@ static bool skipDigits( const char * buf,
10799 saveStart = *start;
108-
100+
109101 for( i = *start; i < max; i++ )
110102+ assigns( value, i )
111103+ loopInvariant( *start <= i && i <= max )
112104+ decreases( max - i )
113105 {
114106 if( !isdigit_( buf[ i ] ) )
115107 {
116- @@ -944 ,6 +992,9 @@ static void skipArrayScalars( const char * buf,
108+ @@ -945 ,6 +992,9 @@ static void skipArrayScalars( const char * buf,
117109 i = *start;
118-
110+
119111 while( i < max )
120112+ assigns( i )
121113+ loopInvariant( *start <= i && i <= max )
122114+ decreases( max - i )
123115 {
124116 if( skipAnyScalar( buf, &i, max ) != true )
125117 {
126- @@ -986 ,6 +1037 ,13 @@ static bool skipObjectScalars( const char * buf,
118+ @@ -991 ,6 +1041 ,13 @@ static bool skipObjectScalars( const char * buf,
127119 i = *start;
128-
120+
129121 while( i < max )
130122+ assigns( i, *start, comma )
131123+ loopInvariant(
@@ -137,9 +129,9 @@ index 901b2e1..8bdd89c 100644
137129 {
138130 if( skipString( buf, &i, max ) != true )
139131 {
140- @@ -1082 ,6 +1140 ,14 @@ static JSONStatus_t skipCollection( const char * buf,
132+ @@ -1118 ,6 +1175 ,14 @@ static JSONStatus_t skipCollection( const char * buf,
141133 i = *start;
142-
134+
143135 while( i < max )
144136+ assigns( i, depth, c, __CPROVER_object_whole( stack ), ret )
145137+ loopInvariant(
@@ -152,27 +144,27 @@ index 901b2e1..8bdd89c 100644
152144 {
153145 c = buf[ i ];
154146 i++;
155- @@ -1363 ,6 +1429 ,9 @@ static bool objectSearch( const char * buf,
147+ @@ -1407 ,6 +1472 ,9 @@ static bool objectSearch( const char * buf,
156148 skipSpace( buf, &i, max );
157-
149+
158150 while( i < max )
159151+ assigns( i, key, keyLength, value, valueLength )
160152+ loopInvariant( __CPROVER_loop_entry( i ) <= i && i <= max )
161153+ decreases( max - i )
162154 {
163155 if( nextKeyValuePair( buf, &i, max, &key, &keyLength,
164156 &value, &valueLength ) != true )
165- @@ -1430 ,6 +1499 ,9 @@ static bool arraySearch( const char * buf,
157+ @@ -1474 ,6 +1542 ,9 @@ static bool arraySearch( const char * buf,
166158 skipSpace( buf, &i, max );
167-
159+
168160 while( i < max )
169161+ assigns( i, currentIndex, value, valueLength )
170162+ loopInvariant( __CPROVER_loop_entry( i ) <= i && i <= max && currentIndex < i )
171163+ decreases( max - i )
172164 {
173165 if( nextValue( buf, &i, max, &value, &valueLength ) != true )
174166 {
175- @@ -1495 ,6 +1567 ,9 @@ static bool skipQueryPart( const char * buf,
167+ @@ -1539 ,6 +1610 ,9 @@ static bool skipQueryPart( const char * buf,
176168 while( ( i < max ) &&
177169 !isSeparator_( buf[ i ] ) &&
178170 !isSquareOpen_( buf[ i ] ) )
@@ -182,9 +174,9 @@ index 901b2e1..8bdd89c 100644
182174 {
183175 i++;
184176 }
185- @@ -1541 ,6 +1616 ,17 @@ static JSONStatus_t multiSearch( const char * buf,
177+ @@ -1585 ,6 +1659 ,17 @@ static JSONStatus_t multiSearch( const char * buf,
186178 coreJSON_ASSERT( ( max > 0U ) && ( queryLength > 0U ) );
187-
179+
188180 while( i < queryLength )
189181+ assigns( i, start, queryStart, value, length )
190182+ loopInvariant(
@@ -199,4 +191,4 @@ index 901b2e1..8bdd89c 100644
199191+ decreases( queryLength - i )
200192 {
201193 bool found = false;
202-
194+
0 commit comments