Skip to content

Commit

Permalink
lib: Fixed json-parser to correctly parse numbers at EOF.
Browse files Browse the repository at this point in the history
Numbers are a bit special compared to others, because they don't have any
clear character indicating that the number ends. So we can only assume that
the number is finished when EOF is reached, although even that isn't
necessarily correct in case the stream is terminated unexpectedly.

This change is in prepartion for the next change. With current JSON parser
this issue could never happen because "}" was expected just before EOF.
  • Loading branch information
sirainen committed Feb 1, 2016
1 parent 736b180 commit 4bfa47e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/lib/json-parser.c
Expand Up @@ -37,6 +37,7 @@ struct json_parser {
ARRAY(enum json_state) nesting;
unsigned int nested_skip_count;
bool skipping;
bool seen_eof;
};

static int json_parser_read_more(struct json_parser *parser)
Expand All @@ -57,13 +58,19 @@ static int json_parser_read_more(struct json_parser *parser)
parser->error = "Token too large";
return -1;
}
if (ret <= 0)
if (ret < 0 && !parser->seen_eof &&
i_stream_get_data_size(parser->input) > 0 &&
parser->input->stream_errno == 0) {
/* call it once more to finish any pending number */
parser->seen_eof = TRUE;
} else if (ret <= 0) {
return ret;

cur_highwater = parser->input->v_offset +
i_stream_get_data_size(parser->input);
i_assert(parser->highwater_offset < cur_highwater);
parser->highwater_offset = cur_highwater;
} else {
cur_highwater = parser->input->v_offset +
i_stream_get_data_size(parser->input);
i_assert(parser->highwater_offset < cur_highwater);
parser->highwater_offset = cur_highwater;
}
}

parser->start = parser->data = i_stream_get_data(parser->input, &size);
Expand Down

0 comments on commit 4bfa47e

Please sign in to comment.