Skip to content

Commit d89457a

Browse files
poetteringyuwata
authored andcommitted
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.
1 parent fc4a7f1 commit d89457a

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

src/basic/io-util.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,24 @@ int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
106106
}
107107

108108
int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
109-
const uint8_t *p = ASSERT_PTR(buf);
109+
const uint8_t *p;
110110

111111
assert(fd >= 0);
112112

113-
if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
114-
return -EINVAL;
113+
if (nbytes == 0) {
114+
static const dummy_t dummy[0];
115+
assert_cc(sizeof(dummy) == 0);
116+
p = (const void*) dummy; /* Some valid pointer, in case NULL was specified */
117+
} else {
118+
assert(buf);
119+
120+
if (nbytes == SIZE_MAX)
121+
nbytes = strlen(buf);
122+
else if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
123+
return -EINVAL;
124+
125+
p = buf;
126+
}
115127

116128
do {
117129
ssize_t k;

src/shared/ask-password-api.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,20 +431,20 @@ int ask_password_tty(
431431
use_color = colors_enabled();
432432

433433
if (use_color)
434-
(void) loop_write(ttyfd, ANSI_HIGHLIGHT, STRLEN(ANSI_HIGHLIGHT), false);
434+
(void) loop_write(ttyfd, ANSI_HIGHLIGHT, SIZE_MAX, false);
435435

436-
(void) loop_write(ttyfd, message, strlen(message), false);
436+
(void) loop_write(ttyfd, message, SIZE_MAX, false);
437437
(void) loop_write(ttyfd, " ", 1, false);
438438

439439
if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT) && !FLAGS_SET(flags, ASK_PASSWORD_ECHO)) {
440440
if (use_color)
441-
(void) loop_write(ttyfd, ansi_grey(), strlen(ansi_grey()), false);
442-
(void) loop_write(ttyfd, PRESS_TAB, strlen(PRESS_TAB), false);
441+
(void) loop_write(ttyfd, ansi_grey(), SIZE_MAX, false);
442+
(void) loop_write(ttyfd, PRESS_TAB, SIZE_MAX, false);
443443
press_tab_visible = true;
444444
}
445445

446446
if (use_color)
447-
(void) loop_write(ttyfd, ANSI_NORMAL, STRLEN(ANSI_NORMAL), false);
447+
(void) loop_write(ttyfd, ANSI_NORMAL, SIZE_MAX, false);
448448

449449
new_termios = old_termios;
450450
new_termios.c_lflag &= ~(ICANON|ECHO);
@@ -529,7 +529,7 @@ int ask_password_tty(
529529

530530
if (c == 4) { /* C-d also known as EOT */
531531
if (ttyfd >= 0)
532-
(void) loop_write(ttyfd, SKIPPED, strlen(SKIPPED), false);
532+
(void) loop_write(ttyfd, SKIPPED, SIZE_MAX, false);
533533

534534
goto skipped;
535535
}
@@ -579,7 +579,7 @@ int ask_password_tty(
579579
* first key (and only as first key), or ... */
580580

581581
if (ttyfd >= 0)
582-
(void) loop_write(ttyfd, NO_ECHO, strlen(NO_ECHO), false);
582+
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX, false);
583583

584584
} else if (ttyfd >= 0)
585585
(void) loop_write(ttyfd, "\a", 1, false);
@@ -592,7 +592,7 @@ int ask_password_tty(
592592
/* ... or by pressing TAB at any time. */
593593

594594
if (ttyfd >= 0)
595-
(void) loop_write(ttyfd, NO_ECHO, strlen(NO_ECHO), false);
595+
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX, false);
596596

597597
} else if (p >= sizeof(passphrase)-1) {
598598

src/shared/machine-id-setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_
162162
*
163163
* Otherwise write the machine-id directly to disk. */
164164
if (force_transient) {
165-
r = loop_write(fd, "uninitialized\n", strlen("uninitialized\n"), false);
165+
r = loop_write(fd, "uninitialized\n", SIZE_MAX, false);
166166
if (r < 0)
167167
return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
168168

src/vconsole/vconsole-setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static int toggle_utf8_vc(const char *name, int fd, bool utf8) {
239239
if (r < 0)
240240
return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode on %s: %m", enable_disable(utf8), name);
241241

242-
r = loop_write(fd, utf8 ? "\033%G" : "\033%@", 3, false);
242+
r = loop_write(fd, utf8 ? "\033%G" : "\033%@", SIZE_MAX, false);
243243
if (r < 0)
244244
return log_warning_errno(r, "Failed to %s UTF-8 term processing on %s: %m", enable_disable(utf8), name);
245245

0 commit comments

Comments
 (0)