util-lib: use a fixed buffer size for terminal path

The loop around ttyname_r() makes it look like we use unbounded stack
allocations. We know that that paths have a maximum size, so let's simplify
the whole thing.

Replaces #12043.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2019-03-20 09:36:45 +01:00
parent 17b70256f2
commit 30222f4b2c
+11 -20
View File
@@ -891,33 +891,24 @@ bool on_tty(void) {
}
int getttyname_malloc(int fd, char **ret) {
size_t l = 100;
char path[PATH_MAX], *c; /* PATH_MAX is counted *with* the trailing NUL byte */
int r;
assert(fd >= 0);
assert(ret);
for (;;) {
char path[l];
r = ttyname_r(fd, path, sizeof path); /* positive error */
assert(r >= 0);
if (r == ERANGE)
return -ENAMETOOLONG;
if (r > 0)
return -r;
r = ttyname_r(fd, path, sizeof(path));
if (r == 0) {
char *c;
c = strdup(skip_dev_prefix(path));
if (!c)
return -ENOMEM;
*ret = c;
return 0;
}
if (r != ERANGE)
return -r;
l *= 2;
}
c = strdup(skip_dev_prefix(path));
if (!c)
return -ENOMEM;
*ret = c;
return 0;
}