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.
This commit is contained in:
Lennart Poettering 2023-07-12 22:29:17 +02:00 committed by Yu Watanabe
parent fc4a7f13bb
commit d89457a145
4 changed files with 25 additions and 13 deletions

View 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;

View 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) {

View 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);

View 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);