]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
terminal-util: modernize vtnr_from_tty() a bit
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Nov 2024 19:42:27 +0000 (20:42 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 3 Jan 2025 20:05:58 +0000 (21:05 +0100)
src/basic/terminal-util.c
src/basic/terminal-util.h
src/login/logind-dbus.c

index 3831543a6abc5cc425685914b5d78285021151b2..725ccc2b87184938eda6a7073b707866550141d8 100644 (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") )
-                return -EINVAL;
-
-        if (!ascii_isdigit(tty[3]))
+        const char *e = startswith(tty, "tty");
+        if (!e)
                 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) {
index 833c6617de7e1434985d4dca684de07c9416bb42..75b32190976a2f43d1bb3a8a05bb81ea1a591f5d 100644 (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;
+}
index dce596eeb370e97a0e321648b93ed113ef5bbe4c..4b14b9b690a46142f1c6e292db7113397e2266e1 100644 (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 {