From 951d91ecb7ae7a1bdcb9b35bc5713716fef6c9fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wolfgang=20St=C3=B6ggl?= Date: Tue, 13 Jul 2021 20:23:54 +0200 Subject: [PATCH] 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. --- src/rrd_xport.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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); -- 2.47.2