libsubcmd: Avoid two path statics, removing 8192 bytes from .bss
authorIan Rogers <irogers@google.com>
Fri, 26 May 2023 18:34:01 +0000 (11:34 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sun, 28 May 2023 13:25:55 +0000 (10:25 -0300)
Use a single stack allocated buffer and avoid 8,192 bytes in .bss.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Ross Zwisler <zwisler@chromium.org>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20230526183401.2326121-17-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/subcmd/exec-cmd.c

index 5dbea456973e1e21fb59c09874e8fb1623d31719..7739b5217cf65698c73f6b111b7785ebcdb52c57 100644 (file)
@@ -36,38 +36,40 @@ static int is_absolute_path(const char *path)
        return path[0] == '/';
 }
 
-static const char *get_pwd_cwd(void)
+static const char *get_pwd_cwd(char *buf, size_t sz)
 {
-       static char cwd[PATH_MAX + 1];
        char *pwd;
        struct stat cwd_stat, pwd_stat;
-       if (getcwd(cwd, PATH_MAX) == NULL)
+       if (getcwd(buf, sz) == NULL)
                return NULL;
        pwd = getenv("PWD");
-       if (pwd && strcmp(pwd, cwd)) {
-               stat(cwd, &cwd_stat);
+       if (pwd && strcmp(pwd, buf)) {
+               stat(buf, &cwd_stat);
                if (!stat(pwd, &pwd_stat) &&
                    pwd_stat.st_dev == cwd_stat.st_dev &&
                    pwd_stat.st_ino == cwd_stat.st_ino) {
-                       strlcpy(cwd, pwd, PATH_MAX);
+                       strlcpy(buf, pwd, sz);
                }
        }
-       return cwd;
+       return buf;
 }
 
-static const char *make_nonrelative_path(const char *path)
+static const char *make_nonrelative_path(char *buf, size_t sz, const char *path)
 {
-       static char buf[PATH_MAX + 1];
-
        if (is_absolute_path(path)) {
-               if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
+               if (strlcpy(buf, path, sz) >= sz)
                        die("Too long path: %.*s", 60, path);
        } else {
-               const char *cwd = get_pwd_cwd();
+               const char *cwd = get_pwd_cwd(buf, sz);
+
                if (!cwd)
                        die("Cannot determine the current working directory");
-               if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
+
+               if (strlen(cwd) + strlen(path) + 2 >= sz)
                        die("Too long path: %.*s", 60, path);
+
+               strcat(buf, "/");
+               strcat(buf, path);
        }
        return buf;
 }
@@ -133,8 +135,11 @@ static void add_path(char **out, const char *path)
        if (path && *path) {
                if (is_absolute_path(path))
                        astrcat(out, path);
-               else
-                       astrcat(out, make_nonrelative_path(path));
+               else {
+                       char buf[PATH_MAX];
+
+                       astrcat(out, make_nonrelative_path(buf, sizeof(buf), path));
+               }
 
                astrcat(out, ":");
        }