+2018-05-20 Pádraig Brady <P@draigBrady.com>
+
+ fts: avoid a memory leak edge case
+ * lib/fts.c (fts_open): Set an appropriate fts_level
+ so that an immediate fts_close() will free the allocation.
+ * tests/test-fts.c (fts_dealloc): Add a test case which
+ will trigger under valgrind or address sanitizer.
+ Fixes https://bugs.gnu.org/31439
+
2018-05-20 Bruno Haible <bruno@clisp.org>
wcwidth tests: Fix link error.
exit (status);
}
+/* alloc/dealloc to ensure structures initialized appropriately. */
+static void
+fts_dealloc (void)
+{
+ static char dir[] = "./";
+ static char *const curr_dir[2] = { dir, 0 };
+ FTSENT *e;
+ FTS *ftsp = fts_open (curr_dir, FTS_NOSTAT | FTS_PHYSICAL | FTS_CWDFD, 0);
+ if (ftsp)
+ {
+ if (fts_close (ftsp) != 0)
+ perror_exit ("fts_close", 9);
+ }
+ else
+ perror_exit (base, 10);
+}
+
/* Remove BASE and all files under it. */
static void
remove_tree (void)
perror_exit (base, 6);
while ((e = fts_read (ftsp)))
needles_seen += strcmp (e->fts_name, "needle") == 0;
+ int fts_read_errno = errno;
fflush (stdout);
- if (errno)
- perror_exit ("fts_read", 7);
+ if (fts_read_errno)
+ {
+ errno = fts_read_errno;
+ perror_exit ("fts_read", 7);
+ }
+ if (fts_close (ftsp) != 0)
+ perror_exit (base, 8);
/* Report an error if we did not find the needles. */
if (needles_seen != needles)
fprintf (stderr, "fts could not remove directory\n");
return 1;
}
+
+ fts_dealloc ();
+
return 0;
}