bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

minor fts.c fixes


From: Jim Meyering
Subject: minor fts.c fixes
Date: Fri, 14 Apr 2006 22:40:21 +0200

I've just installed these changes for coreutils.
I plan to make bigger changes to the copy of fts.c in coreutils
before merging back to gnulib.

2006-04-14  Jim Meyering  <address@hidden>

        Remove unreachable code and plug leaks in code that handles degenerate
        input and some failure cases.
        * fts.c (fts_build): #if-0-out a block of unused code. Patch from glibc.
        (fts_build): Remove just-#if-0'd code and the code that
        declares and sets cderrno.
        (fts_build): Free `head' before returning NULL, in two places,
        to avoid leaks.  Patch from glibc.
        (fts_open): Don't allocate parent if *argv==NULL.  Patch from glibc.

Index: lib/fts.c
===================================================================
RCS file: /fetish/cu/lib/fts.c,v
retrieving revision 1.44
retrieving revision 1.49
diff -u -p -r1.44 -r1.49
--- lib/fts.c   17 Jan 2006 17:24:14 -0000      1.44
+++ lib/fts.c   14 Apr 2006 16:36:34 -0000      1.49
@@ -273,7 +273,8 @@ fts_open (char * const *argv,
        register FTS *sp;
        register FTSENT *p, *root;
        register size_t nitems;
-       FTSENT *parent, *tmp = NULL;    /* pacify gcc */
+       FTSENT *parent = NULL;
+       FTSENT *tmp = NULL;     /* pacify gcc */
        size_t len;
 
        /* Options check. */
@@ -341,9 +342,11 @@ fts_open (char * const *argv,
        }
 
        /* Allocate/initialize root's parent. */
-       if ((parent = fts_alloc(sp, "", 0)) == NULL)
-               goto mem2;
-       parent->fts_level = FTS_ROOTPARENTLEVEL;
+       if (*argv != NULL) {
+               if ((parent = fts_alloc(sp, "", 0)) == NULL)
+                       goto mem2;
+               parent->fts_level = FTS_ROOTPARENTLEVEL;
+         }
 
        /* Allocate/initialize root(s). */
        for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
@@ -786,7 +789,6 @@ fts_build (register FTS *sp, int type)
        FTSENT *cur, *tail;
        DIR *dirp;
        void *oldaddr;
-       int cderrno;
        int saved_errno;
        bool descend;
        bool doadjust;
@@ -855,7 +857,6 @@ fts_build (register FTS *sp, int type)
         * needed sorted entries or stat information, they had better be
         * checking FTS_NS on the returned nodes.
         */
-       cderrno = 0;
        if (nlinks || type == BREAD) {
                int dir_fd = dup (dirfd(dirp));
                if (dir_fd < 0 || fts_safe_changedir(sp, cur, dir_fd, NULL)) {
@@ -863,7 +864,6 @@ fts_build (register FTS *sp, int type)
                                cur->fts_errno = errno;
                        cur->fts_flags |= FTS_DONTCHDIR;
                        descend = false;
-                       cderrno = errno;
                        closedir(dirp);
                        if (0 <= dir_fd)
                                close (dir_fd);
@@ -956,37 +956,19 @@ mem1:                             saved_errno = errno;
                        p->fts_flags |= FTS_ISW;
 #endif
 
-               if (cderrno) {
-                       if (nlinks) {
-                               p->fts_info = FTS_NS;
-                               p->fts_errno = cderrno;
-                       } else
-                               p->fts_info = FTS_NSOK;
-                       p->fts_accpath = cur->fts_accpath;
-               } else if (nlinks == 0
-#if HAVE_STRUCT_DIRENT_D_TYPE
-                          || (nostat &&
-                              dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
-#endif
-                   ) {
-                       p->fts_accpath =
-                           ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
-                       p->fts_info = FTS_NSOK;
-               } else {
-                       /* Build a file name for fts_stat to stat. */
-                       if (ISSET(FTS_NOCHDIR)) {
-                               p->fts_accpath = p->fts_path;
-                               memmove(cp, p->fts_name, p->fts_namelen + 1);
-                       } else
-                               p->fts_accpath = p->fts_name;
-                       /* Stat it. */
-                       p->fts_info = fts_stat(sp, p, false);
-
-                       /* Decrement link count if applicable. */
-                       if (nlinks > 0 && (p->fts_info == FTS_D ||
-                           p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
-                               nlinks -= nostat;
-               }
+               /* Build a file name for fts_stat to stat. */
+               if (ISSET(FTS_NOCHDIR)) {
+                       p->fts_accpath = p->fts_path;
+                       memmove(cp, p->fts_name, p->fts_namelen + 1);
+               } else
+                       p->fts_accpath = p->fts_name;
+               /* Stat it. */
+               p->fts_info = fts_stat(sp, p, false);
+
+               /* Decrement link count if applicable. */
+               if (nlinks > 0 && (p->fts_info == FTS_D ||
+                   p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
+                       nlinks -= nostat;
 
                /* We walk in directory order so "ls -f" doesn't get upset. */
                p->fts_link = NULL;
@@ -1031,6 +1013,7 @@ mem1:                             saved_errno = errno;
             fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
                cur->fts_info = FTS_ERR;
                SET(FTS_STOP);
+               fts_lfree(head);
                return (NULL);
        }
 
@@ -1038,6 +1021,7 @@ mem1:                             saved_errno = errno;
        if (!nitems) {
                if (type == BREAD)
                        cur->fts_info = FTS_DP;
+               fts_lfree(head);
                return (NULL);
        }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]