Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Check for trailing garbage inside json (rework #80) #149

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

Closed
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
58 changes: 57 additions & 1 deletion json.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ static int new_value (json_state * state,
return 1;
}

static int trailing_garbage (const json_char * ptr);

#define whitespace \
case '\n': ++ state.cur_line; state.cur_col = 0; /* FALLTHRU */ \
case ' ': /* FALLTHRU */ case '\t': /* FALLTHRU */ case '\r'
Expand Down Expand Up @@ -301,6 +303,7 @@ json_value * json_parse_ex (json_settings * settings,
for (state.ptr = json ;; ++ state.ptr)
{
json_char b = (state.ptr == end ? 0 : *state.ptr);
int garbage_check;

if (flags & flag_string)
{
Expand Down Expand Up @@ -547,8 +550,15 @@ json_value * json_parse_ex (json_settings * settings,

case ']':

if (top && top->type == json_array)
if (top && top->type == json_array) {
garbage_check = trailing_garbage(state.ptr);
if (garbage_check) {
sprintf (error, "Trailing garbage before %d:%d",
state.cur_line, state.cur_col);
goto e_failed;
}
flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next;
}
else
{ sprintf (error, "%u:%u: Unexpected `]`", line_and_col);
goto e_failed;
Expand Down Expand Up @@ -740,6 +750,13 @@ json_value * json_parse_ex (json_settings * settings,

case '}':

garbage_check = trailing_garbage(state.ptr);
if (garbage_check) {
sprintf (error, "Trailing garbage before %d:%d",
state.cur_line, state.cur_col);
goto e_failed;
}

flags = (flags & ~ flag_need_comma) | flag_next;
break;

Expand Down Expand Up @@ -1053,3 +1070,42 @@ void json_value_free (json_value * value)
settings.mem_free = default_free;
json_value_free_ex (&settings, value);
}

static int trailing_garbage (const json_char * ptr)
{
json_char marker;

do {
ptr--;
} while (isspace(*ptr));

switch (*ptr)
{
case '}':
case '{':
case ']':
case '[':
case '"':
return 0;

case 'e':
marker = *(--ptr);
if (marker == 's')
if (*(--ptr) == 'l' && *(--ptr) == 'a' && *(--ptr) == 'f')
return 0;
if (marker == 'u')
if (*(--ptr) == 'r' && *(--ptr) == 't')
return 0;
return 1;

case 'l':
if (*(--ptr) == 'l' && *(--ptr) == 'u' && *(--ptr) == 'n')
return 0;
return 1;

default:
if (isdigit(*ptr))
return 0;
return 1;
}
}