[hackers] [ubase] Handle cleanup properly in ttytostr() || sin

From: <git_AT_suckless.org>
Date: Fri, 16 Jan 2015 17:10:49 +0100 (CET)

commit a7a561ef008ea0842b54713434d5f3fd79e8ccec
Author: sin <sin_AT_2f30.org>
Date: Fri Jan 16 16:06:42 2015 +0000

    Handle cleanup properly in ttytostr()

diff --git a/libutil/tty.c b/libutil/tty.c
index f8159d9..0197f7e 100644
--- a/libutil/tty.c
+++ b/libutil/tty.c
_AT_@ -27,6 +27,7 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
         DIR *dirp;
         char path[PATH_MAX];
         int fd;
+ int r = 0;
 
         switch (tty_maj) {
         case 136:
_AT_@ -54,16 +55,19 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
 
                 if (strlcpy(path, "/dev/", sizeof(path)) >= sizeof(path)) {
                         weprintf("path too long\n");
- return -1;
+ r = -1;
+ goto err0;
                 }
                 if (strlcat(path, dp->d_name, sizeof(path)) >= sizeof(path)) {
                         weprintf("path too long\n");
- return -1;
+ r = -1;
+ goto err0;
                 }
 
                 if (stat(path, &sb) < 0) {
                         weprintf("stat %s:", path);
- return -1;
+ r = -1;
+ goto err0;
                 }
 
                 if ((int)major(sb.st_rdev) == tty_maj &&
_AT_@ -75,15 +79,19 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
                                 strlcpy(str, dp->d_name, n);
                                 close(fd);
                                 break;
+ } else {
+ close(fd);
+ r = -1;
+ goto err0;
                         }
- close(fd);
                 }
         }
 
+err0:
         if (closedir(dirp) < 0) {
                 weprintf("closedir /dev:");
- return -1;
+ r = -1;
         }
 
- return 0;
+ return r;
 }
Received on Fri Jan 16 2015 - 17:10:49 CET

This archive was generated by hypermail 2.3.0 : Fri Jan 16 2015 - 17:12:11 CET