From e441c38462facfa48ab7d7bb40c343d9ce6500db Mon Sep 17 00:00:00 2001 From: Icarus Sparry Date: Wed, 21 Mar 2018 18:40:54 -0700 Subject: [PATCH] Fix garbage from list-bundles The directory reading routines do not promise that the entries remain valid whilst the directory is open. In particular if the directory is more than 4k (one stdio buffer) in size then the names will be invalid. Signed-off-by: Icarus Sparry --- src/bundle.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/bundle.c b/src/bundle.c index 8fca605c..6589cae2 100644 --- a/src/bundle.c +++ b/src/bundle.c @@ -932,21 +932,27 @@ int list_local_bundles() (strcmp(ent->d_name, "..") == 0)) { continue; } - - bundles = list_append_data(bundles, ent->d_name); + /* Need to dup the strings as the directory + * may be bigger than the size of the I/O buffer */ + char * name = strdup(ent->d_name); + if (!name) { + abort(); + } + bundles = list_append_data(bundles, name); } + closedir(dir); + item = bundles = list_sort(bundles, lex_sort); while (item) { printf("%s\n", (char *)item->data); + free(item->data); item = item->next; } list_free_list(bundles); - /* closedir only after we use the strings from readdir. */ - closedir(dir); free_string(&path); return 0;