From 20e20cbf2b57cd6f6e36e6a705ef4313d787e233 Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Wed, 27 Jul 2022 15:07:13 +0200 Subject: [PATCH] improved JSON parser error messages --- modules/database/src/ioc/db/dbConstLink.c | 8 ++-- modules/database/src/ioc/db/dbConvertJSON.c | 47 ++++++++++++--------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/modules/database/src/ioc/db/dbConstLink.c b/modules/database/src/ioc/db/dbConstLink.c index 7215beb01..ec2d3184f 100644 --- a/modules/database/src/ioc/db/dbConstLink.c +++ b/modules/database/src/ioc/db/dbConstLink.c @@ -186,8 +186,8 @@ static long dbConstLoadLS(struct link *plink, char *pbuffer, epicsUInt32 size, status = dbLSConvertJSON(pstr, pbuffer, size, plen); if (status) - errlogPrintf("... while parsing link %s.%s %s\n", - plink->precord->name, dbLinkFieldName(plink), pstr); + errlogPrintf("... while parsing link %s.%s\n", + plink->precord->name, dbLinkFieldName(plink)); return status; } @@ -206,8 +206,8 @@ static long dbConstLoadArray(struct link *plink, short dbrType, void *pbuffer, status = dbPutConvertJSON(pstr, dbrType, pbuffer, pnReq); if (status) - errlogPrintf("... while parsing link %s.%s %s\n", - plink->precord->name, dbLinkFieldName(plink), pstr); + errlogPrintf("... while parsing link %s.%s\n", + plink->precord->name, dbLinkFieldName(plink)); return status; } diff --git a/modules/database/src/ioc/db/dbConvertJSON.c b/modules/database/src/ioc/db/dbConvertJSON.c index 36a439262..ba7c42691 100644 --- a/modules/database/src/ioc/db/dbConvertJSON.c +++ b/modules/database/src/ioc/db/dbConvertJSON.c @@ -30,10 +30,12 @@ typedef struct parseContext { } parseContext; static int dbcj_null(void *ctx) { + errlogPrintf("dbConvertJSON: Null objects not supported\n"); return 0; /* Illegal */ } static int dbcj_boolean(void *ctx, int val) { + errlogPrintf("dbConvertJSON: Boolean not supported\n"); return 0; /* Illegal */ } @@ -51,6 +53,7 @@ static int dbcj_integer(void *ctx, long long num) { } static int dblsj_integer(void *ctx, long long num) { + errlogPrintf("dbLSConvertJSON: Numeric value %lli provided, string expected\n", num); return 0; /* Illegal */ } @@ -67,6 +70,7 @@ static int dbcj_double(void *ctx, double num) { } static int dblsj_double(void *ctx, double num) { + errlogPrintf("dbLSConvertJSON: Numeric value %g provided, string expected\n", num); return 0; /* Illegal */ } @@ -78,7 +82,8 @@ static int dbcj_string(void *ctx, const unsigned char *val, size_t len) { * metadata about the field than we have available at the moment. */ if (parser->dbrType != DBF_STRING) { - errlogPrintf("dbConvertJSON: String provided, numeric value(s) expected\n"); + errlogPrintf("dbConvertJSON: String \"%.*s\" provided, numeric value expected\n", + (int)len, val); return 0; /* Illegal */ } @@ -159,8 +164,10 @@ long dbPutConvertJSON(const char *json, short dbrType, size_t jlen = strlen(json); long status; - if(INVALID_DB_REQ(dbrType)) + if (INVALID_DB_REQ(dbrType)) { + errlogPrintf("dbConvertJSON: Invalid dbrType %d\n", dbrType); return S_db_badDbrtype; + } if (!jlen) { *pnRequest = 0; @@ -175,8 +182,10 @@ long dbPutConvertJSON(const char *json, short dbrType, yajl_set_default_alloc_funcs(&dbcj_alloc); yh = yajl_alloc(&dbcj_callbacks, &dbcj_alloc, parser); - if (!yh) + if (!yh) { + errlogPrintf("dbConvertJSON: out of memory\n"); return S_db_noMemory; + } ys = yajl_parse(yh, (const unsigned char *) json, jlen); if (ys == yajl_status_ok) @@ -188,15 +197,13 @@ long dbPutConvertJSON(const char *json, short dbrType, status = 0; break; - case yajl_status_error: { - unsigned char *err = yajl_get_error(yh, 1, - (const unsigned char *) json, jlen); - errlogPrintf("dbConvertJSON: %s\n", err); - yajl_free_error(yh, err); + default: { + unsigned char *err = yajl_get_error(yh, 1, + (const unsigned char *) json, jlen); + errlogPrintf("dbConvertJSON: %s", err); + yajl_free_error(yh, err); + status = S_db_badField; } - /* fall through */ - default: - status = S_db_badField; } yajl_free(yh); @@ -233,8 +240,10 @@ long dbLSConvertJSON(const char *json, char *pdest, epicsUInt32 size, yajl_set_default_alloc_funcs(&dbcj_alloc); yh = yajl_alloc(&dblsj_callbacks, &dbcj_alloc, parser); - if (!yh) + if (!yh) { + errlogPrintf("dbLSConvertJSON: out of memory\n"); return S_db_noMemory; + } ys = yajl_parse(yh, (const unsigned char *) json, jlen); @@ -244,15 +253,13 @@ long dbLSConvertJSON(const char *json, char *pdest, epicsUInt32 size, status = 0; break; - case yajl_status_error: { - unsigned char *err = yajl_get_error(yh, 1, - (const unsigned char *) json, jlen); - errlogPrintf("dbLoadLS_JSON: %s\n", err); - yajl_free_error(yh, err); + default: { + unsigned char *err = yajl_get_error(yh, 1, + (const unsigned char *) json, jlen); + errlogPrintf("dbLSConvertJSON: %s", err); + yajl_free_error(yh, err); + status = S_db_badField; } - /* fall through */ - default: - status = S_db_badField; } yajl_free(yh);