From: Lennart Poettering Date: Wed, 12 Jul 2023 20:29:17 +0000 (+0200) Subject: loop-write: do strlen() implicitly if size is specified as SIZE_MAX X-Git-Tag: v254-rc2~31 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d89457a1454eb179b3630c38d91e661976a60997;p=thirdparty%2Fsystemd.git loop-write: do strlen() implicitly if size is specified as SIZE_MAX This reduces repetition in the function calls, since quite often we write out strings with loop_write(). Noticed while reviewing #28077. --- diff --git a/src/basic/io-util.c b/src/basic/io-util.c index bf1aab9e388..25e551604a7 100644 --- a/src/basic/io-util.c +++ b/src/basic/io-util.c @@ -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; diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 6b2388e2c1a..ca6ba80cbd2 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -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) { diff --git a/src/shared/machine-id-setup.c b/src/shared/machine-id-setup.c index e059c711051..6090231374e 100644 --- a/src/shared/machine-id-setup.c +++ b/src/shared/machine-id-setup.c @@ -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); diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c index 4d9915cbbaa..0c63f73bcf8 100644 --- a/src/vconsole/vconsole-setup.c +++ b/src/vconsole/vconsole-setup.c @@ -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);