/* Set current node pointer. */
@@ -679,7 +679,7 @@
level = cur->fts_level + 1;
/* Read the directory, attaching each entry to the `link' pointer. */
- adjaddr = NULL;
+ doadjust = 0;
for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
continue;
@@ -687,6 +687,7 @@
if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
goto mem1;
if (dp->d_namlen > maxlen) {
+ oldaddr = sp->fts_path;
if (fts_palloc(sp, (size_t)dp->d_namlen)) {
/*
* No more memory for path or structures. Save
@@ -703,7 +704,9 @@
SET(FTS_STOP);
return (NULL);
}
- adjaddr = sp->fts_path;
+ /* Did realloc() change the pointer? */
+ if (oldaddr != sp->fts_path)
+ doadjust = 1;
maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
}
@@ -762,11 +765,11 @@
(void)closedir(dirp);
/*
- * If had to realloc the path, adjust the addresses for the rest
- * of the tree.
+ * If realloc() changed the address of the path, adjust the
+ * addresses for the rest of the tree and the dir list.
*/
- if (adjaddr)
- fts_padjust(sp, adjaddr);
+ if (doadjust)
+ fts_padjust(sp, head);
/*
* If not changing directories, reset the path back to original
@@ -950,7 +953,7 @@
if ((p = malloc(len)) == NULL)
return (NULL);
- /* Copy the name and guarantee NULL termination. */
+ /* Copy the name and guarantee NUL termination. */
memmove(p->fts_name, name, namelen);
p->fts_name[namelen] = '\0';