Skip to content

Commit

Permalink
Parse no deeper than MAX_PARSING_DEPTH
Browse files Browse the repository at this point in the history
    while true; do printf '{"deeper": '; done | jq .
  • Loading branch information
mark-kubacki authored and nicowilliams committed Jan 27, 2017
1 parent 83e2cf6 commit fd4ae83
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

typedef const char* presult;

#ifndef MAX_PARSING_DEPTH
#define MAX_PARSING_DEPTH (256)
#endif

#define TRY(x) do {presult msg__ = (x); if (msg__) return msg__; } while(0)
#ifdef __GNUC__
#define pfunc __attribute__((warn_unused_result)) presult
Expand Down Expand Up @@ -147,11 +151,13 @@ static void push(struct jv_parser* p, jv v) {
static pfunc parse_token(struct jv_parser* p, char ch) {
switch (ch) {
case '[':
if (p->stackpos >= MAX_PARSING_DEPTH) return "Exceeds depth limit for parsing";
if (jv_is_valid(p->next)) return "Expected separator between values";
push(p, jv_array());
break;

case '{':
if (p->stackpos >= MAX_PARSING_DEPTH) return "Exceeds depth limit for parsing";
if (jv_is_valid(p->next)) return "Expected separator between values";
push(p, jv_object());
break;
Expand Down

0 comments on commit fd4ae83

Please sign in to comment.