Skip to content

Commit 2003237

Browse files
captain5050acmel
authored andcommitted
libsubcmd: Avoid two path statics, removing 8192 bytes from .bss
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>
1 parent f50b835 commit 2003237

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

tools/lib/subcmd/exec-cmd.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,40 @@ static int is_absolute_path(const char *path)
3636
return path[0] == '/';
3737
}
3838

39-
static const char *get_pwd_cwd(void)
39+
static const char *get_pwd_cwd(char *buf, size_t sz)
4040
{
41-
static char cwd[PATH_MAX + 1];
4241
char *pwd;
4342
struct stat cwd_stat, pwd_stat;
44-
if (getcwd(cwd, PATH_MAX) == NULL)
43+
if (getcwd(buf, sz) == NULL)
4544
return NULL;
4645
pwd = getenv("PWD");
47-
if (pwd && strcmp(pwd, cwd)) {
48-
stat(cwd, &cwd_stat);
46+
if (pwd && strcmp(pwd, buf)) {
47+
stat(buf, &cwd_stat);
4948
if (!stat(pwd, &pwd_stat) &&
5049
pwd_stat.st_dev == cwd_stat.st_dev &&
5150
pwd_stat.st_ino == cwd_stat.st_ino) {
52-
strlcpy(cwd, pwd, PATH_MAX);
51+
strlcpy(buf, pwd, sz);
5352
}
5453
}
55-
return cwd;
54+
return buf;
5655
}
5756

58-
static const char *make_nonrelative_path(const char *path)
57+
static const char *make_nonrelative_path(char *buf, size_t sz, const char *path)
5958
{
60-
static char buf[PATH_MAX + 1];
61-
6259
if (is_absolute_path(path)) {
63-
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
60+
if (strlcpy(buf, path, sz) >= sz)
6461
die("Too long path: %.*s", 60, path);
6562
} else {
66-
const char *cwd = get_pwd_cwd();
63+
const char *cwd = get_pwd_cwd(buf, sz);
64+
6765
if (!cwd)
6866
die("Cannot determine the current working directory");
69-
if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
67+
68+
if (strlen(cwd) + strlen(path) + 2 >= sz)
7069
die("Too long path: %.*s", 60, path);
70+
71+
strcat(buf, "/");
72+
strcat(buf, path);
7173
}
7274
return buf;
7375
}
@@ -133,8 +135,11 @@ static void add_path(char **out, const char *path)
133135
if (path && *path) {
134136
if (is_absolute_path(path))
135137
astrcat(out, path);
136-
else
137-
astrcat(out, make_nonrelative_path(path));
138+
else {
139+
char buf[PATH_MAX];
140+
141+
astrcat(out, make_nonrelative_path(buf, sizeof(buf), path));
142+
}
138143

139144
astrcat(out, ":");
140145
}

0 commit comments

Comments
 (0)