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

Fix JSON validation for mismatched brackets #115

Merged
merged 5 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,8 @@ static JSONStatus_t skipCollection( const char * buf,
break;
}

ret = ( depth == 0 ) ? JSONSuccess : JSONIllegalDocument;
ret = ( ( depth == 0 ) && isMatchingBracket_( stack[ depth ], c ) ) ?
JSONSuccess : JSONIllegalDocument;
break;

default:
Expand Down
18 changes: 16 additions & 2 deletions test/unit-test/core_json_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@
#define MISMATCHED_BRACKETS_LENGTH ( sizeof( MISMATCHED_BRACKETS ) - 1 )

#define MISMATCHED_BRACKETS2 "{\"foo\":[\"bar\",\"xyz\"}}"
#define MISMATCHED_BRACKETS2_LENGTH ( sizeof( MISMATCHED_BRACKETS ) - 1 )
#define MISMATCHED_BRACKETS2_LENGTH ( sizeof( MISMATCHED_BRACKETS2 ) - 1 )

#define MISMATCHED_BRACKETS3 "{\"foo\":[\"bar\",\"xyz\"]]"
Copy link
Contributor

@paulbartell paulbartell Jun 13, 2022

Choose a reason for hiding this comment

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

This is only used once.. Does it really need to be a #defined constant?

#define MISMATCHED_BRACKETS3_LENGTH ( sizeof( MISMATCHED_BRACKETS3 ) - 1 )

#define MISMATCHED_BRACKETS4 "[\"foo\",\"bar\",\"xyz\"}"
#define MISMATCHED_BRACKETS4_LENGTH ( sizeof( MISMATCHED_BRACKETS4 ) - 1 )

#define INCORRECT_OBJECT_SEPARATOR "{\"foo\": \"bar\"; \"bar\": \"foo\"}"
#define INCORRECT_OBJECT_SEPARATOR_LENGTH ( sizeof( INCORRECT_OBJECT_SEPARATOR ) - 1 )
Expand Down Expand Up @@ -620,6 +626,14 @@ void test_JSON_Validate_Illegal_Documents( void )
MISMATCHED_BRACKETS2_LENGTH );
TEST_ASSERT_EQUAL( JSONIllegalDocument, jsonStatus );

jsonStatus = JSON_Validate( MISMATCHED_BRACKETS3,
MISMATCHED_BRACKETS3_LENGTH );
TEST_ASSERT_EQUAL( JSONIllegalDocument, jsonStatus );

jsonStatus = JSON_Validate( MISMATCHED_BRACKETS4,
MISMATCHED_BRACKETS4_LENGTH );
TEST_ASSERT_EQUAL( JSONIllegalDocument, jsonStatus );

jsonStatus = JSON_Validate( NUL_ESCAPE, NUL_ESCAPE_LENGTH );
TEST_ASSERT_EQUAL( JSONIllegalDocument, jsonStatus );

Expand Down Expand Up @@ -1245,7 +1259,7 @@ void test_JSON_Search_Illegal_Documents( void )
COMPLETE_QUERY_KEY_LENGTH,
&outValue,
&outValueLength );
TEST_ASSERT_EQUAL( JSONSuccess, jsonStatus );
TEST_ASSERT_EQUAL( JSONNotFound, jsonStatus );

jsonStatus = JSON_Search( LETTER_AS_EXPONENT,
LETTER_AS_EXPONENT_LENGTH,
Expand Down