From b8e206429b7e94bf9b3bef158755ac1f0a383dd9 Mon Sep 17 00:00:00 2001 From: Sven Panne Date: Mon, 22 Mar 2021 12:55:26 +0100 Subject: [PATCH] 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. --- src/rrd_flushcached.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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; } } -- 2.47.2