Handle cleanup properly in ttytostr()

This commit is contained in:
sin 2015-01-16 16:06:42 +00:00
parent 8a95422ff1
commit a7a561ef00

View File

@ -27,6 +27,7 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
DIR *dirp; DIR *dirp;
char path[PATH_MAX]; char path[PATH_MAX];
int fd; int fd;
int r = 0;
switch (tty_maj) { switch (tty_maj) {
case 136: case 136:
@ -54,16 +55,19 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
if (strlcpy(path, "/dev/", sizeof(path)) >= sizeof(path)) { if (strlcpy(path, "/dev/", sizeof(path)) >= sizeof(path)) {
weprintf("path too long\n"); weprintf("path too long\n");
return -1; r = -1;
goto err0;
} }
if (strlcat(path, dp->d_name, sizeof(path)) >= sizeof(path)) { if (strlcat(path, dp->d_name, sizeof(path)) >= sizeof(path)) {
weprintf("path too long\n"); weprintf("path too long\n");
return -1; r = -1;
goto err0;
} }
if (stat(path, &sb) < 0) { if (stat(path, &sb) < 0) {
weprintf("stat %s:", path); weprintf("stat %s:", path);
return -1; r = -1;
goto err0;
} }
if ((int)major(sb.st_rdev) == tty_maj && if ((int)major(sb.st_rdev) == tty_maj &&
@ -75,15 +79,19 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
strlcpy(str, dp->d_name, n); strlcpy(str, dp->d_name, n);
close(fd); close(fd);
break; break;
} } else {
close(fd); close(fd);
r = -1;
goto err0;
}
} }
} }
err0:
if (closedir(dirp) < 0) { if (closedir(dirp) < 0) {
weprintf("closedir /dev:"); weprintf("closedir /dev:");
return -1; r = -1;
} }
return 0; return r;
} }