Skip to content

Commit 39f0fbc

Browse files
authored
Fix overflow in skipOneHexEscape() (#100)
1 parent 5c678b2 commit 39f0fbc

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

source/core_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ static bool skipOneHexEscape( const char * buf,
335335

336336
i = *start;
337337
#define HEX_ESCAPE_LENGTH ( 6U ) /* e.g., \u1234 */
338-
end = i + HEX_ESCAPE_LENGTH;
338+
end = ( i <= ( SIZE_MAX - HEX_ESCAPE_LENGTH ) ) ? ( i + HEX_ESCAPE_LENGTH ) : SIZE_MAX;
339339

340340
if( ( end < max ) && ( buf[ i ] == '\\' ) && ( buf[ i + 1U ] == 'u' ) )
341341
{

source/include/stdint.readme

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ typedef unsigned long uint32_t;
2525
typedef long long int64_t;
2626
typedef unsigned long long uint64_t;
2727

28-
#define INT8_MAX ( ( signed char ) 127 )
29-
#define UINT8_MAX ( ( unsigned char ) 255 )
30-
#define INT16_MAX ( ( short ) 32767 )
31-
#define UINT16_MAX ( ( unsigned short ) 65535 )
32-
#define INT32_MAX 2147483647L
33-
#define UINT32_MAX 4294967295UL
34-
#define INT64_MAX 9223372036854775807LL
35-
#define UINT64_MAX 18446744073709551615ULL
28+
#define INT8_MAX ( ( signed char ) 127 )
29+
#define UINT8_MAX ( ( unsigned char ) 255 )
30+
#define INT16_MAX ( ( short ) 32767 )
31+
#define UINT16_MAX ( ( unsigned short ) 65535 )
32+
#define INT32_MAX 2147483647L
33+
#define UINT32_MAX 4294967295UL
34+
#define INT64_MAX 9223372036854775807LL
35+
#define UINT64_MAX 18446744073709551615ULL
36+
37+
#ifndef SIZE_MAX
38+
#define SIZE_MAX ( ( size_t ) -1 )
39+
#endif
3640

3741
#endif /* _STDINT_H */

test/unit-test/core_json_utest.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,3 +1871,16 @@ void test_JSON_unreached( void )
18711871
iterate( buf, max, &start, &next, &key, &keyLength, &value, &valueLength ) );
18721872
}
18731873
}
1874+
1875+
/**
1876+
* @brief Test overflows.
1877+
*/
1878+
void test_JSON_overflows( void )
1879+
{
1880+
char buf[] = UNICODE_ESCAPE_SEQUENCES_BMP;
1881+
size_t start;
1882+
uint16_t u;
1883+
1884+
start = SIZE_MAX;
1885+
TEST_ASSERT_EQUAL( false, skipOneHexEscape( buf, &start, SIZE_MAX, &u ) );
1886+
}

0 commit comments

Comments
 (0)