]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Avoid segfault: Don't read and write into the same buffer. 1111/head
authorSven Panne <svenpanne@gmail.com>
Mon, 22 Mar 2021 11:55:26 +0000 (12:55 +0100)
committerSven Panne <svenpanne@gmail.com>
Mon, 22 Mar 2021 11:55:27 +0000 (12:55 +0100)
Using CTX->rrd_error as a source *and* destination buffer at the same time
provokes undefined behavior.  In real life you get funny error messages
and/or segfaults within vs(n)printf().

Simple solution: Use a temporary copy of the error message while writing
into it.

src/rrd_flushcached.c

index 6d92c940611d3620a5145f9c66c135a443e74c79..090bca7496e38923ffd63b151e68d869adaca311 100644 (file)
@@ -92,13 +92,14 @@ int rrd_flushcached (int argc, char **argv)
             char *error;
             int   remaining;
 
-            error     = rrd_get_error();
+            error     = strdup(rrd_get_error());
             remaining = options.argc - options.optind - 1;
 
             rrd_set_error("Flushing of file \"%s\" failed: %s. Skipping "
                     "remaining %i file%s.", options.argv[i],
-                    (*error == '\0') ? "unknown error" : error,
+                    (error == NULL || *error == '\0') ? "unknown error" : error,
                     remaining, (remaining == 1) ? "" : "s");
+            free(error);
             break;
         }
     }