Skip to content

Commit 89df62c

Browse files
captain5050acmel
authored andcommitted
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>
1 parent 1fc88e5 commit 89df62c

File tree

1 file changed

+18
-7
lines changed
  • tools/lib/api/fs

1 file changed

+18
-7
lines changed

tools/lib/api/fs/fs.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static const char * const bpf_fs__known_mountpoints[] = {
8888
struct fs {
8989
const char *name;
9090
const char * const *mounts;
91-
char path[PATH_MAX];
91+
char *path;
9292
bool found;
9393
bool checked;
9494
long magic;
@@ -151,17 +151,23 @@ static bool fs__read_mounts(struct fs *fs)
151151
bool found = false;
152152
char type[100];
153153
FILE *fp;
154+
char path[PATH_MAX + 1];
154155

155156
fp = fopen("/proc/mounts", "r");
156157
if (fp == NULL)
157-
return NULL;
158+
return false;
158159

159160
while (!found &&
160161
fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
161-
fs->path, type) == 2) {
162+
path, type) == 2) {
162163

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;
164169
found = true;
170+
}
165171
}
166172

167173
fclose(fp);
@@ -188,8 +194,11 @@ static bool fs__check_mounts(struct fs *fs)
188194
ptr = fs->mounts;
189195
while (*ptr) {
190196
if (fs__valid_mount(*ptr, fs->magic) == 0) {
197+
free(fs->path);
198+
fs->path = strdup(*ptr);
199+
if (!fs->path)
200+
return false;
191201
fs->found = true;
192-
strcpy(fs->path, *ptr);
193202
return true;
194203
}
195204
ptr++;
@@ -227,10 +236,12 @@ static bool fs__env_override(struct fs *fs)
227236
if (!override_path)
228237
return false;
229238

239+
free(fs->path);
240+
fs->path = strdup(override_path);
241+
if (!fs->path)
242+
return false;
230243
fs->found = true;
231244
fs->checked = true;
232-
strncpy(fs->path, override_path, sizeof(fs->path) - 1);
233-
fs->path[sizeof(fs->path) - 1] = '\0';
234245
return true;
235246
}
236247

0 commit comments

Comments
 (0)