Add missing calls to yajl_complete_parse()

Fixes leak when link parses OK but JSON errors follow.
Includes regression test.
This commit is contained in:
Andrew Johnson
2017-09-30 11:30:50 -05:00
parent 6f4e466989
commit cd14e2ee9f
4 changed files with 14 additions and 4 deletions

View File

@@ -267,7 +267,7 @@ static long chf_parse(dbChannel *chan, const char **pjson)
{ chan, NULL, 0 };
yajl_handle yh = yajl_alloc(&chf_callbacks, &chf_alloc, &parser);
const char *json = *pjson;
size_t jlen = strlen(json);
size_t jlen = strlen(json), ylen;
yajl_status ys;
long status;
@@ -275,11 +275,15 @@ static long chf_parse(dbChannel *chan, const char **pjson)
return S_db_noMemory;
ys = yajl_parse(yh, (const unsigned char *) json, jlen);
ylen = yajl_get_bytes_consumed(yh);
if (ys == yajl_status_ok)
ys = yajl_complete_parse(yh);
switch (ys) {
case yajl_status_ok:
*pjson += ylen;
status = 0;
*pjson += yajl_get_bytes_consumed(yh);
break;
case yajl_status_error: {

View File

@@ -171,6 +171,8 @@ long dbPutConvertJSON(const char *json, short dbrType,
return S_db_noMemory;
ys = yajl_parse(yh, (const unsigned char *) json, jlen);
if (ys == yajl_status_ok)
ys = yajl_complete_parse(yh);
switch (ys) {
case yajl_status_ok:

View File

@@ -365,6 +365,8 @@ long dbJLinkParse(const char *json, size_t jlen, short dbfType,
return S_db_noMemory;
ys = yajl_parse(yh, (const unsigned char *) json, jlen);
if (ys == yajl_status_ok)
ys = yajl_complete_parse(yh);
switch (ys) {
unsigned char *err;
@@ -380,6 +382,7 @@ long dbJLinkParse(const char *json, size_t jlen, short dbfType,
errlogPrintf("dbJLinkInit: %s\n", err);
yajl_free_error(yh, err);
dbJLinkFree(parser->pjlink);
dbJLinkFree(parser->product);
/* fall through */
default:
status = S_db_badField;

View File

@@ -585,9 +585,10 @@ void testJLink(void)
testNumZ(3);
testdbPutFieldFail(S_dbLib_badField, "j1.INP", DBF_STRING, "{\"z\":{\"fail\":5}}");
testdbPutFieldFail(S_dbLib_badField, "j1.INP", DBF_STRING, "{\"z\":{\"good\":6}");
testdbPutFieldOk("j1.PROC", DBF_LONG, 1);
testdbGetFieldEqual("j1.VAL", DBF_LONG, 4);
/* put failure in parsing stage doesn't modify link */
/* put failures in parsing stage don't modify link */
testdbGetFieldEqual("j1.INP", DBF_STRING, "{\"z\":{\"good\":4}}");
testNumZ(3);
@@ -601,7 +602,7 @@ void testJLink(void)
MAIN(dbPutLinkTest)
{
testPlan(301);
testPlan(302);
testLinkParse();
testLinkFailParse();
testCADBSet();