]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Add escapeJSON() to legend entries
authorWolfgang Stöggl <c72578@yahoo.de>
Tue, 13 Jul 2021 18:23:54 +0000 (20:23 +0200)
committerWolfgang Stöggl <c72578@yahoo.de>
Tue, 13 Jul 2021 18:23:54 +0000 (20:23 +0200)
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

index e69a807010175159a6cf232144af406b84136c44..cc401feb9cbed8246cc41de9816a72c9e4f38f5e 100644 (file)
@@ -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), "        <line>%s</line>\n",
                          entry);