From 37762ff488752c1686b359816aec75831b49c55f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20Mi=C5=82ecki?= Date: Thu, 4 Jan 2018 14:58:18 +0100 Subject: [PATCH] libfstools: support file paths longer than 255 chars MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Alloc globdir buffer dynamically and simply use realloc when needed. This fixes e.g. segmentation fault in jffs2reset due to an infinite recurrency when dealing with longs paths. Signed-off-by: Rafał Miłecki --- libfstools/overlay.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/libfstools/overlay.c b/libfstools/overlay.c index 5a49da2..6024586 100644 --- a/libfstools/overlay.c +++ b/libfstools/overlay.c @@ -67,15 +67,29 @@ handle_rmdir(const char *dir) void foreachdir(const char *dir, int (*cb)(const char*)) { + static char *globdir = NULL; + static size_t globdirlen = 0; struct stat s = { 0 }; - char globdir[256]; + size_t dirlen = strlen(dir); glob_t gl; int j; - if (dir[strlen(dir) - 1] == '/') - snprintf(globdir, 256, "%s*", dir); + if (dirlen + sizeof("/*") > globdirlen) { + /* Alloc extra 256 B to avoid too many reallocs */ + size_t len = dirlen + sizeof("/*") + 256; + char *tmp; + + tmp = realloc(globdir, len); + if (!tmp) + return; + globdir = tmp; + globdirlen = len; + } + + if (dir[dirlen - 1] == '/') + sprintf(globdir, "%s*", dir); else - snprintf(globdir, 256, "%s/*", dir); /**/ + sprintf(globdir, "%s/*", dir); if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl)) for (j = 0; j < gl.gl_pathc; j++) { -- 2.25.1