]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loop-write: do strlen() implicitly if size is specified as SIZE_MAX
authorLennart Poettering <lennart@poettering.net>
Wed, 12 Jul 2023 20:29:17 +0000 (22:29 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 13 Jul 2023 00:59:28 +0000 (09:59 +0900)
This reduces repetition in the function calls, since quite often we
write out strings with loop_write().

Noticed while reviewing #28077.

src/basic/io-util.c
src/shared/ask-password-api.c
src/shared/machine-id-setup.c
src/vconsole/vconsole-setup.c

index bf1aab9e3884a8757bb9f9b5aa9483186ce98eeb..25e551604a7ad52bac97e1d17c20c92852922481 100644 (file)
@@ -106,12 +106,24 @@ int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
 }
 
 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
-        const uint8_t *p = ASSERT_PTR(buf);
+        const uint8_t *p;
 
         assert(fd >= 0);
 
-        if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
-                return -EINVAL;
+        if (nbytes == 0) {
+                static const dummy_t dummy[0];
+                assert_cc(sizeof(dummy) == 0);
+                p = (const void*) dummy; /* Some valid pointer, in case NULL was specified */
+        } else {
+                assert(buf);
+
+                if (nbytes == SIZE_MAX)
+                        nbytes = strlen(buf);
+                else if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
+                        return -EINVAL;
+
+                p = buf;
+        }
 
         do {
                 ssize_t k;
index 6b2388e2c1ac3d69e44607d3993bfd34e25e4eb3..ca6ba80cbd27d756e3bf62ba68f781205650ecf6 100644 (file)
@@ -431,20 +431,20 @@ int ask_password_tty(
                         use_color = colors_enabled();
 
                 if (use_color)
-                        (void) loop_write(ttyfd, ANSI_HIGHLIGHT, STRLEN(ANSI_HIGHLIGHT), false);
+                        (void) loop_write(ttyfd, ANSI_HIGHLIGHT, SIZE_MAX, false);
 
-                (void) loop_write(ttyfd, message, strlen(message), false);
+                (void) loop_write(ttyfd, message, SIZE_MAX, false);
                 (void) loop_write(ttyfd, " ", 1, false);
 
                 if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT) && !FLAGS_SET(flags, ASK_PASSWORD_ECHO)) {
                         if (use_color)
-                                (void) loop_write(ttyfd, ansi_grey(), strlen(ansi_grey()), false);
-                        (void) loop_write(ttyfd, PRESS_TAB, strlen(PRESS_TAB), false);
+                                (void) loop_write(ttyfd, ansi_grey(), SIZE_MAX, false);
+                        (void) loop_write(ttyfd, PRESS_TAB, SIZE_MAX, false);
                         press_tab_visible = true;
                 }
 
                 if (use_color)
-                        (void) loop_write(ttyfd, ANSI_NORMAL, STRLEN(ANSI_NORMAL), false);
+                        (void) loop_write(ttyfd, ANSI_NORMAL, SIZE_MAX, false);
 
                 new_termios = old_termios;
                 new_termios.c_lflag &= ~(ICANON|ECHO);
@@ -529,7 +529,7 @@ int ask_password_tty(
 
                 if (c == 4) { /* C-d also known as EOT */
                         if (ttyfd >= 0)
-                                (void) loop_write(ttyfd, SKIPPED, strlen(SKIPPED), false);
+                                (void) loop_write(ttyfd, SKIPPED, SIZE_MAX, false);
 
                         goto skipped;
                 }
@@ -579,7 +579,7 @@ int ask_password_tty(
                                  * first key (and only as first key), or ... */
 
                                 if (ttyfd >= 0)
-                                        (void) loop_write(ttyfd, NO_ECHO, strlen(NO_ECHO), false);
+                                        (void) loop_write(ttyfd, NO_ECHO, SIZE_MAX, false);
 
                         } else if (ttyfd >= 0)
                                 (void) loop_write(ttyfd, "\a", 1, false);
@@ -592,7 +592,7 @@ int ask_password_tty(
                         /* ... or by pressing TAB at any time. */
 
                         if (ttyfd >= 0)
-                                (void) loop_write(ttyfd, NO_ECHO, strlen(NO_ECHO), false);
+                                (void) loop_write(ttyfd, NO_ECHO, SIZE_MAX, false);
 
                 } else if (p >= sizeof(passphrase)-1) {
 
index e059c71105157ccf594161ead0563c659b5dc629..6090231374ec39d1ad6f3c803776e445df48977f 100644 (file)
@@ -162,7 +162,7 @@ int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_
                  *
                  * Otherwise write the machine-id directly to disk. */
                 if (force_transient) {
-                        r = loop_write(fd, "uninitialized\n", strlen("uninitialized\n"), false);
+                        r = loop_write(fd, "uninitialized\n", SIZE_MAX, false);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
 
index 4d9915cbbaaab8b4578113a670400665e77407fa..0c63f73bcf858c0ed5d274490095c809e8ee8473 100644 (file)
@@ -239,7 +239,7 @@ static int toggle_utf8_vc(const char *name, int fd, bool utf8) {
         if (r < 0)
                 return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode on %s: %m", enable_disable(utf8), name);
 
-        r = loop_write(fd, utf8 ? "\033%G" : "\033%@", 3, false);
+        r = loop_write(fd, utf8 ? "\033%G" : "\033%@", SIZE_MAX, false);
         if (r < 0)
                 return log_warning_errno(r, "Failed to %s UTF-8 term processing on %s: %m", enable_disable(utf8), name);