Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
20032376 f50b8357

+20 -15
+20 -15
tools/lib/subcmd/exec-cmd.c
··· 36 36 return path[0] == '/'; 37 37 } 38 38 39 - static const char *get_pwd_cwd(void) 39 + static const char *get_pwd_cwd(char *buf, size_t sz) 40 40 { 41 - static char cwd[PATH_MAX + 1]; 42 41 char *pwd; 43 42 struct stat cwd_stat, pwd_stat; 44 - if (getcwd(cwd, PATH_MAX) == NULL) 43 + if (getcwd(buf, sz) == NULL) 45 44 return NULL; 46 45 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); 49 48 if (!stat(pwd, &pwd_stat) && 50 49 pwd_stat.st_dev == cwd_stat.st_dev && 51 50 pwd_stat.st_ino == cwd_stat.st_ino) { 52 - strlcpy(cwd, pwd, PATH_MAX); 51 + strlcpy(buf, pwd, sz); 53 52 } 54 53 } 55 - return cwd; 54 + return buf; 56 55 } 57 56 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) 59 58 { 60 - static char buf[PATH_MAX + 1]; 61 - 62 59 if (is_absolute_path(path)) { 63 - if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) 60 + if (strlcpy(buf, path, sz) >= sz) 64 61 die("Too long path: %.*s", 60, path); 65 62 } else { 66 - const char *cwd = get_pwd_cwd(); 63 + const char *cwd = get_pwd_cwd(buf, sz); 64 + 67 65 if (!cwd) 68 66 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) 70 69 die("Too long path: %.*s", 60, path); 70 + 71 + strcat(buf, "/"); 72 + strcat(buf, path); 71 73 } 72 74 return buf; 73 75 } ··· 135 133 if (path && *path) { 136 134 if (is_absolute_path(path)) 137 135 astrcat(out, path); 138 - else 139 - astrcat(out, make_nonrelative_path(path)); 136 + else { 137 + char buf[PATH_MAX]; 138 + 139 + astrcat(out, make_nonrelative_path(buf, sizeof(buf), path)); 140 + } 140 141 141 142 astrcat(out, ":"); 142 143 }