From 3f4bc28c5ab0a10c380d45439f590bc39fec7dec Mon Sep 17 00:00:00 2001 From: Attila Kovacs Date: Sun, 29 Sep 2024 11:27:28 +0200 Subject: [PATCH] xjson: More robust field parsing --- src/xjson.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/xjson.c b/src/xjson.c index dd7e8e6..9b85e25 100644 --- a/src/xjson.c +++ b/src/xjson.c @@ -413,11 +413,18 @@ static XStructure *ParseObject(char **pos, int *lineNumber) { XField *f; next = SkipSpaces(next, lineNumber); + if(*next == '}') { next++; break; } + if(*next == ',') { + Warning("[L.%d] Empty field.\n", *lineNumber); + next++; + continue; + } + f = calloc(1, sizeof(XField)); x_check_alloc(f); @@ -466,11 +473,19 @@ static XStructure *ParseObject(char **pos, int *lineNumber) { f->value = ParsePrimitive(&next, &f->type, lineNumber); } - if(*next == ',') - next++; - f = xSetField(s, f); if(f) xDestroyField(f); // If duplicate field, destroy the prior one. + + // Spaces after field... + next = SkipSpaces(next, lineNumber); + + // There should be either a comma or a closing bracket after the field... + if(*next == ',') next++; + else if(*next == '}') { + next++; + break; + } + else Warning("[L.%d] Missing comma or closing bracket after field.\n", *lineNumber); } *pos = next;