Skip to content
44 changes: 22 additions & 22 deletions source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ static uint8_t hexToInt( char c )
*
* @return true if a valid escape sequence was present;
* false otherwise.
*
* @note For the sake of security, \u0000 is disallowed.
*/
static bool skipOneHexEscape( const char * buf,
size_t * start,
Expand Down Expand Up @@ -358,7 +356,7 @@ static bool skipOneHexEscape( const char * buf,
}
}

if( ( i == end ) && ( value > 0U ) )
if( i == end )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow value of 0 (\u0000) per JSON spec.

{
ret = true;
*outValue = value;
Expand All @@ -382,8 +380,6 @@ static bool skipOneHexEscape( const char * buf,
*
* @return true if a valid escape sequence was present;
* false otherwise.
*
* @note For the sake of security, \u0000 is disallowed.
*/
#define isHighSurrogate( x ) ( ( ( x ) >= 0xD800U ) && ( ( x ) <= 0xDBFFU ) )
#define isLowSurrogate( x ) ( ( ( x ) >= 0xDC00U ) && ( ( x ) <= 0xDFFFU ) )
Expand Down Expand Up @@ -437,8 +433,6 @@ static bool skipHexEscape( const char * buf,
*
* @return true if a valid escape sequence was present;
* false otherwise.
*
* @note For the sake of security, \NUL is disallowed.
*/
static bool skipEscape( const char * buf,
size_t * start,
Expand All @@ -457,9 +451,6 @@ static bool skipEscape( const char * buf,

switch( c )
{
case '\0':
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break from the default case - no need for this.

break;

case 'u':
ret = skipHexEscape( buf, &i, max );
break;
Expand All @@ -477,14 +468,6 @@ static bool skipEscape( const char * buf,
break;

default:

/* a control character: (NUL,SPACE) */
Copy link
Member Author

@jasonpcarroll jasonpcarroll Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is invalid... escaped literal control characters should fail validation per spec, thus I have removed it.

if( iscntrl_( c ) )
{
i += 2U;
ret = true;
}

break;
}
}
Expand Down Expand Up @@ -934,11 +917,12 @@ static bool skipSpaceAndComma( const char * buf,
*
* @note Stops advance if a value is an object or array.
*/
static void skipArrayScalars( const char * buf,
static bool skipArrayScalars( const char * buf,
size_t * start,
size_t max )
{
size_t i = 0U;
bool ret = true;

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

Expand All @@ -953,11 +937,19 @@ static void skipArrayScalars( const char * buf,

if( skipSpaceAndComma( buf, &i, max ) != true )
{
/* After parsing a scalar, we must either have a comma (followed by more content)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes cases such as [3[4]] from being parsed as valid.

* or be at a closing bracket. If neither, the array is malformed. */
if( ( i >= max ) || !isSquareClose_( buf[ i ] ) )
{
ret = false;
}

break;
}
}

*start = i;
return ret;
}

/**
Expand Down Expand Up @@ -1071,7 +1063,7 @@ static bool skipScalars( const char * buf,
{
if( !isSquareClose_( buf[ i ] ) )
{
skipArrayScalars( buf, start, max );
ret = skipArrayScalars( buf, start, max );
}
}
else
Expand Down Expand Up @@ -1151,14 +1143,22 @@ static JSONStatus_t skipCollection( const char * buf,
{
depth--;

if( ( skipSpaceAndComma( buf, &i, max ) == true ) &&
isOpenBracket_( stack[ depth ] ) )
Copy link
Member Author

@jasonpcarroll jasonpcarroll Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this isOpenBracket_ check as there is no point... stack[ depth ] will always contain an open bracket when depth >= 0.

if( skipSpaceAndComma( buf, &i, max ) == true )
{
if( skipScalars( buf, &i, max, stack[ depth ] ) != true )
{
ret = JSONIllegalDocument;
}
}
else
{
/* After closing a nested collection, if there is no comma found when calling
* skipSpaceAndComma, then we must be at the end of the parent collection. */
if( ( i < max ) && !isMatchingBracket_( stack[ depth ], buf[ i ] ) )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes cases where missing commas between collections was considered valid. (e.g. [ { "key1" : "value1"} {"key2":"value2"}])

{
ret = JSONIllegalDocument;
}
}

break;
}
Expand Down
Loading