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

tools api fs: Avoid large static PATH_MAX arrays

Change struct fs to have a pointer to a dynamically allocated array
rather than an array. This reduces the size of fs__entries from 24,768
bytes to 240 bytes. Read paths into a stack allocated array and
strdup. Fix off-by-1 fscanf %<num>s in fs__read_mounts caught by
address sanitizer.

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

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
89df62c3 1fc88e5a

+18 -7
+18 -7
tools/lib/api/fs/fs.c
··· 88 88 struct fs { 89 89 const char *name; 90 90 const char * const *mounts; 91 - char path[PATH_MAX]; 91 + char *path; 92 92 bool found; 93 93 bool checked; 94 94 long magic; ··· 151 151 bool found = false; 152 152 char type[100]; 153 153 FILE *fp; 154 + char path[PATH_MAX + 1]; 154 155 155 156 fp = fopen("/proc/mounts", "r"); 156 157 if (fp == NULL) 157 - return NULL; 158 + return false; 158 159 159 160 while (!found && 160 161 fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", 161 - fs->path, type) == 2) { 162 + path, type) == 2) { 162 163 163 - if (strcmp(type, fs->name) == 0) 164 + if (strcmp(type, fs->name) == 0) { 165 + free(fs->path); 166 + fs->path = strdup(path); 167 + if (!fs->path) 168 + return false; 164 169 found = true; 170 + } 165 171 } 166 172 167 173 fclose(fp); ··· 194 188 ptr = fs->mounts; 195 189 while (*ptr) { 196 190 if (fs__valid_mount(*ptr, fs->magic) == 0) { 191 + free(fs->path); 192 + fs->path = strdup(*ptr); 193 + if (!fs->path) 194 + return false; 197 195 fs->found = true; 198 - strcpy(fs->path, *ptr); 199 196 return true; 200 197 } 201 198 ptr++; ··· 236 227 if (!override_path) 237 228 return false; 238 229 230 + free(fs->path); 231 + fs->path = strdup(override_path); 232 + if (!fs->path) 233 + return false; 239 234 fs->found = true; 240 235 fs->checked = true; 241 - strncpy(fs->path, override_path, sizeof(fs->path) - 1); 242 - fs->path[sizeof(fs->path) - 1] = '\0'; 243 236 return true; 244 237 } 245 238