make proper use of fstatat, as suggested by guenther@
authorespie <espie@openbsd.org>
Mon, 27 Jun 2016 16:52:30 +0000 (16:52 +0000)
committerespie <espie@openbsd.org>
Mon, 27 Jun 2016 16:52:30 +0000 (16:52 +0000)
okay millert@

lib/libc/gen/ttyname.c

index 456fe79..5efbc0d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: ttyname.c,v 1.17 2016/06/27 06:10:04 espie Exp $ */
+/*     $OpenBSD: ttyname.c,v 1.18 2016/06/27 16:52:30 espie Exp $ */
 /*
  * Copyright (c) 1988, 1993
  *     The Regents of the University of California.  All rights reserved.
@@ -122,16 +122,14 @@ oldttyname(struct stat *sb, char *buf, size_t len)
        while ((dirp = readdir(dp))) {
                if (dirp->d_type != DT_CHR && dirp->d_type != DT_UNKNOWN)
                        continue;
-               if (dirp->d_namlen > len - sizeof(_PATH_DEV)) {
-                       (void)closedir(dp);
+               if (fstatat(dirfd(dp), dirp->d_name, &dsb, AT_SYMLINK_NOFOLLOW) 
+                   || !S_ISCHR(dsb.st_mode) || sb->st_rdev != dsb.st_rdev)
+                       continue;
+               (void)closedir(dp);
+               if (dirp->d_namlen > len - sizeof(_PATH_DEV))
                        return (ERANGE);
-               }
                memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name,
                    dirp->d_namlen + 1);
-               if (lstat(buf, &dsb) || !S_ISCHR(dsb.st_mode) ||
-                   sb->st_rdev != dsb.st_rdev)
-                       continue;
-               (void)closedir(dp);
                return (0);
        }
        (void)closedir(dp);