From 246b8d8553b6880146d6c489a28cf4bacea8a199 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Fri, 30 Dec 2022 17:13:17 +0100 Subject: [PATCH] core bugfix: template system may generate invalid json When - a list template - is created with option.jsonf="on" - and the last list element is a property with onEmpty="skip" - and that property is actually empty invalid JSON is generated. The JSON string in this case ends with ", " instead of "}\n". This patch fixes the issue. closes https://github.com/rsyslog/rsyslog/issues/5050 --- Conflict:NA Reference:https://github.com/rsyslog/rsyslog/commit/246b8d8553b6880146d6c489a28cf4bacea8a199 --- template.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/template.c b/template.c index 21d8b8d..18bcda7 100644 --- a/template.c +++ b/template.c @@ -163,6 +163,7 @@ tplToString(struct template *__restrict__ const pTpl, unsigned short bMustBeFreed = 0; uchar *pVal; rs_size_t iLenVal = 0; + int need_comma = 0; if(pTpl->pStrgen != NULL) { CHKiRet(pTpl->pStrgen(pMsg, iparam)); @@ -230,15 +231,24 @@ tplToString(struct template *__restrict__ const pTpl, if(iBuf + iLenVal + extra_space >= iparam->lenBuf) /* we reserve one char for the final \0! */ CHKiRet(ExtendBuf(iparam, iBuf + iLenVal + 1)); + if(need_comma) { + memcpy(iparam->param + iBuf, ", ", 2); + iBuf += 2; + } memcpy(iparam->param + iBuf, pVal, iLenVal); iBuf += iLenVal; if(pTpl->optFormatEscape == JSONF) { - memcpy(iparam->param + iBuf, - (pTpe->pNext == NULL) ? "}\n" : ", ", 2); - iBuf += 2; + need_comma = 1; } } + if((pTpl->optFormatEscape == JSONF) && (pTpe->pNext == NULL)) { + /* space was reserved while processing field above + (via extra_space in ExtendBuf() new size formula. */ + memcpy(iparam->param + iBuf, "}\n", 2); + iBuf += 2; + } + if(bMustBeFreed) { free(pVal); bMustBeFreed = 0; -- 2.27.0