]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
vconsole-setup: simplify path allocation
authorLennart Poettering <lennart@poettering.net>
Tue, 8 Aug 2023 12:19:21 +0000 (14:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 9 Aug 2023 08:31:31 +0000 (10:31 +0200)
Let's code this straighforwadly, and just allocate the string as we need
it, instead of doing pre-allocation.

This is not performance sensitive, as this will almost certainly just
return /dev/tty1 after the first transition.

src/vconsole/vconsole-setup.c

index add0a00e0c05c337f76ab1e6583e981e4e3eb0af..5f595bf12413da9fc4231899da0bfe00b784be20 100644 (file)
@@ -503,12 +503,12 @@ static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) {
 static int find_source_vc(char **ret_path, unsigned *ret_idx) {
         int r, err = 0;
 
-        _cleanup_free_ char *path = new(char, sizeof("/dev/tty63"));
-        if (!path)
-                return log_oom();
+        assert(ret_path);
+        assert(ret_idx);
 
         for (unsigned i = 1; i <= 63; i++) {
                 _cleanup_close_ int fd = -EBADF;
+                _cleanup_free_ char *path = NULL;
 
                 r = verify_vc_allocation(i);
                 if (r < 0) {
@@ -517,7 +517,9 @@ static int find_source_vc(char **ret_path, unsigned *ret_idx) {
                         continue;
                 }
 
-                sprintf(path, "/dev/tty%u", i);
+                if (asprintf(&path, "/dev/tty%u", i) < 0)
+                        return log_oom();
+
                 fd = open_terminal(path, O_RDWR|O_CLOEXEC|O_NOCTTY);
                 if (fd < 0) {
                         if (!err)