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 <rafal@milecki.pl>
void
foreachdir(const char *dir, int (*cb)(const char*))
{
void
foreachdir(const char *dir, int (*cb)(const char*))
{
+ static char *globdir = NULL;
+ static size_t globdirlen = 0;
+ size_t dirlen = strlen(dir);
- 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);
- 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++) {
if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
for (j = 0; j < gl.gl_pathc; j++) {