terminal-util: modernize vtnr_from_tty() a bit

This commit is contained in:
Lennart Poettering
2024-11-19 20:42:27 +01:00
parent 496639d02c
commit 57e55f93bb
3 changed files with 13 additions and 11 deletions

View File

@@ -777,26 +777,24 @@ bool tty_is_console(const char *tty) {
}
int vtnr_from_tty(const char *tty) {
int i, r;
int r;
assert(tty);
tty = skip_dev_prefix(tty);
if (!startswith(tty, "tty") )
const char *e = startswith(tty, "tty");
if (!e)
return -EINVAL;
if (!ascii_isdigit(tty[3]))
return -EINVAL;
r = safe_atoi(tty+3, &i);
unsigned u;
r = safe_atou(e, &u);
if (r < 0)
return r;
if (!vtnr_is_valid(u))
return -ERANGE;
if (i < 0 || i > 63)
return -EINVAL;
return i;
return (int) u;
}
int resolve_dev_console(char **ret) {

View File

@@ -172,3 +172,7 @@ static inline bool osc_char_is_valid(char c) {
* ECMA-48 5th edition, section 8.3.89 */
return (unsigned char) c >= 32U && (unsigned char) c < 127;
}
static inline bool vtnr_is_valid(unsigned n) {
return n >= 1 && n <= 63;
}

View File

@@ -952,7 +952,7 @@ static int create_session(
if (seat) {
if (seat_has_vts(seat)) {
if (vtnr <= 0 || vtnr > 63)
if (!vtnr_is_valid(vtnr))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"VT number out of range");
} else {