Skip to content

Commit

Permalink
Check for failed buf allocations yajl_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Apr 4, 2022
1 parent 09bc860 commit bef7072
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ext/yajl/api/yajl_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ extern "C" {
yajl_status_insufficient_data,
/** An error occured during the parse. Call yajl_get_error for
* more information about the encountered error */
yajl_status_error
yajl_status_error,
/** an allocation failed */
yajl_status_alloc_failed,
} yajl_status;

/** attain a human readable, english, string for an error */
Expand Down
3 changes: 3 additions & 0 deletions ext/yajl/yajl.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ yajl_status_to_string(yajl_status stat)
case yajl_status_error:
statStr = "parse error";
break;
case yajl_status_alloc_failed:
statStr = "allocation failed";
break;
}
return statStr;
}
Expand Down
8 changes: 7 additions & 1 deletion ext/yajl/yajl_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,17 @@ void yajl_parse_chunk(const unsigned char * chunk, unsigned int len, yajl_handle

stat = yajl_parse(parser, chunk, len);

if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
if (stat == yajl_status_ok || stat == yajl_status_insufficient_data) {
// success
} else if (stat == yajl_status_error) {
unsigned char * str = yajl_get_error(parser, 1, chunk, len);
VALUE errobj = rb_exc_new2(cParseError, (const char*) str);
yajl_free_error(parser, str);
rb_exc_raise(errobj);
} else {
const char * str = yajl_status_to_string(stat);
VALUE errobj = rb_exc_new2(cParseError, (const char*) str);
rb_exc_raise(errobj);
}
}

Expand Down
2 changes: 2 additions & 0 deletions ext/yajl/yajl_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ yajl_do_parse(yajl_handle hand, const unsigned char * jsonText,
if (hand->callbacks && hand->callbacks->yajl_string) {
yajl_buf_clear(hand->decodeBuf);
yajl_string_decode(hand->decodeBuf, buf, bufLen);
if (yajl_buf_err(hand->decodeBuf))
return yajl_status_alloc_failed;
_CC_CHK(hand->callbacks->yajl_string(
hand->ctx, yajl_buf_data(hand->decodeBuf),
yajl_buf_len(hand->decodeBuf)));
Expand Down

0 comments on commit bef7072

Please sign in to comment.