From: Sven Panne Date: Mon, 22 Mar 2021 11:55:26 +0000 (+0100) Subject: Avoid segfault: Don't read and write into the same buffer. X-Git-Tag: v1.8.0~27^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1111%2Fhead;p=thirdparty%2Frrdtool-1.x.git Avoid segfault: Don't read and write into the same buffer. 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. --- diff --git a/src/rrd_flushcached.c b/src/rrd_flushcached.c index 6d92c940..090bca74 100644 --- a/src/rrd_flushcached.c +++ b/src/rrd_flushcached.c @@ -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; } }