From: Wolfgang Stöggl Date: Tue, 13 Jul 2021 18:23:54 +0000 (+0200) Subject: Add escapeJSON() to legend entries X-Git-Tag: v1.8.0~19^2~1 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=951d91ecb7ae7a1bdcb9b35bc5713716fef6c9fd;p=thirdparty%2Frrdtool-1.x.git Add escapeJSON() to legend entries Double quotes in legends are currently not escaped, if JSON imgformat is used for rrdtool graph. This produces invalid JSON files. See comment in #409 for further details. --- diff --git a/src/rrd_xport.c b/src/rrd_xport.c index e69a8070..cc401feb 100644 --- a/src/rrd_xport.c +++ b/src/rrd_xport.c @@ -724,6 +724,9 @@ static int rrd_xport_format_xmljson( char buf[256]; char dbuf[1024]; + /* avoid calling escapeJSON() with garbage */ + memset(dbuf, 0, sizeof(dbuf)); + rrd_value_t *ptr = data; if (json == 0) { @@ -820,7 +823,10 @@ static int rrd_xport_format_xmljson( } /* now output it */ if (json) { - snprintf(buf, sizeof(buf), " \"%s\"", entry); + strncpy(dbuf, entry, sizeof(dbuf)); + dbuf[sizeof(dbuf) - 1] = 0; + escapeJSON(dbuf, sizeof(dbuf)); + snprintf(buf, sizeof(buf), " \"%s\"", dbuf); addToBuffer(buffer, buf, 0); if (j < col_cnt - 1) { addToBuffer(buffer, ",", 1); @@ -1164,8 +1170,11 @@ static int rrd_xport_format_addprints( entry++; } if (json) { + strncpy(dbuf, entry, sizeof(dbuf)); + dbuf[sizeof(dbuf) - 1] = 0; + escapeJSON(dbuf, sizeof(dbuf)); snprintf(buf, sizeof(buf), ",\n { \"line\": \"%s\" }", - entry); + dbuf); } else { snprintf(buf, sizeof(buf), " %s\n", entry);