#include "util/debug.h"
 #include "util/config.h"
 #include <linux/string.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 {
        int i, ret = -1;
        struct perf_config_set *set;
-       char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
+       char path[PATH_MAX];
+       char *user_config = mkpath(path, sizeof(path), "%s/.perfconfig", getenv("HOME"));
        const char *config_filename;
        bool changed = false;
 
 
 #include <linux/string.h>
 #include <linux/zalloc.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 {
        struct stat st;
        const char *html_path = system_path(PERF_HTML_PATH);
+       char path[PATH_MAX];
 
        /* Check that we have a perf documentation directory. */
-       if (stat(mkpath("%s/perf.html", html_path), &st)
+       if (stat(mkpath(path, sizeof(path), "%s/perf.html", html_path), &st)
            || !S_ISREG(st.st_mode)) {
                pr_err("'%s': not a documentation directory.", html_path);
                return -1;
 
        return path[0] == '/';
 }
 
-char *mkpath(const char *fmt, ...) __printf(1, 2);
+char *mkpath(char *path_buf, size_t sz, const char *fmt, ...) __printf(3, 4);
 
 #endif /* __PERF_CACHE_H */
 
        const char *home = NULL;
        char *config;
        struct stat st;
+       char path[PATH_MAX];
 
        home = getenv("HOME");
 
        if (!home || !*home || !perf_config_global())
                return NULL;
 
-       config = strdup(mkpath("%s/.perfconfig", home));
+       config = strdup(mkpath(path, sizeof(path), "%s/.perfconfig", home));
        if (config == NULL) {
                pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.\n", home);
                return NULL;
 
 // SPDX-License-Identifier: GPL-2.0
-/*
- * I'm tired of doing "vsnprintf()" etc just to open a
- * file, so here's a "return static buffer with printf"
- * interface for paths.
- *
- * It's obviously not thread-safe. Sue me. But it's quite
- * useful for doing things like
- *
- *   f = open(mkpath("%s/%s.perf", base, name), O_RDONLY);
- *
- * which is what it's designed for.
- */
 #include "path.h"
 #include "cache.h"
 #include <linux/kernel.h>
 #include <dirent.h>
 #include <unistd.h>
 
-static char bad_path[] = "/bad-path/";
-/*
- * One hack:
- */
-static char *get_pathname(void)
-{
-       static char pathname_array[4][PATH_MAX];
-       static int idx;
-
-       return pathname_array[3 & ++idx];
-}
-
 static char *cleanup_path(char *path)
 {
        /* Clean it up */
        return path;
 }
 
-char *mkpath(const char *fmt, ...)
+char *mkpath(char *path_buf, size_t sz, const char *fmt, ...)
 {
        va_list args;
        unsigned len;
-       char *pathname = get_pathname();
 
        va_start(args, fmt);
-       len = vsnprintf(pathname, PATH_MAX, fmt, args);
+       len = vsnprintf(path_buf, sz, fmt, args);
        va_end(args);
-       if (len >= PATH_MAX)
-               return bad_path;
-       return cleanup_path(pathname);
+       if (len >= sz)
+               strncpy(path_buf, "/bad-path/", sz);
+       return cleanup_path(path_buf);
 }
 
 int path__join(char *bf, size_t size, const char *path1, const char *path2)