summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-08-21 18:37:46 +0200
committerLennart Poettering <lennart@poettering.net>2023-08-22 10:53:25 +0200
commit90dab2b01ea19a8f161ad7f85a2fd309f13c2712 (patch)
treeaa23c9a88793d190b9fb534145ae38cf16663127
parentboot: explain why we refuse to edit kernel cmdline when we do so (diff)
downloadsystemd-90dab2b01ea19a8f161ad7f85a2fd309f13c2712.tar.xz
systemd-90dab2b01ea19a8f161ad7f85a2fd309f13c2712.zip
boot: modernize mangle_stub_cmdline() a bit + drop leading whitespace
Let's modernize the function a bit, and make it return the string passed in, as we usually do. Most importanly though: also drop leading whitespace, not just trailing whitespace.
-rw-r--r--src/boot/efi/util.c36
-rw-r--r--src/boot/efi/util.h2
2 files changed, 24 insertions, 14 deletions
diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c
index fdd5abac29..79fa525175 100644
--- a/src/boot/efi/util.c
+++ b/src/boot/efi/util.c
@@ -265,26 +265,36 @@ char16_t *xstr8_to_path(const char *str8) {
return path;
}
-void mangle_stub_cmdline(char16_t *cmdline) {
- char16_t *p = cmdline;
+static bool shall_be_whitespace(char16_t c) {
+ return c <= 0x20U || c == 0x7FU; /* All control characters + space */
+}
+
+char16_t* mangle_stub_cmdline(char16_t *cmdline) {
+ char16_t *p, *q, *e;
if (!cmdline)
- return;
+ return cmdline;
- for (; *cmdline != '\0'; cmdline++)
- /* Convert ASCII control characters to spaces. */
- if (*cmdline <= 0x1F)
- *cmdline = ' ';
+ p = q = cmdline;
- /* chomp the trailing whitespaces */
- while (cmdline != p) {
- --cmdline;
+ /* Skip initial whitespace */
+ while (shall_be_whitespace(*p))
+ p++;
- if (*cmdline != ' ')
- break;
+ /* Turn inner control characters into proper spaces */
+ for (e = p; *p != 0; p++) {
+ if (shall_be_whitespace(*p)) {
+ *(q++) = ' ';
+ continue;
+ }
- *cmdline = '\0';
+ *(q++) = *p;
+ e = q; /* remember last non-whitespace char */
}
+
+ /* Chop off trailing whitespace */
+ *e = 0;
+ return cmdline;
}
EFI_STATUS chunked_read(EFI_FILE *file, size_t *size, void *buf) {
diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h
index 8cb4ed0c1c..d190db9f3f 100644
--- a/src/boot/efi/util.h
+++ b/src/boot/efi/util.h
@@ -98,7 +98,7 @@ EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const char16_t *name, b
void convert_efi_path(char16_t *path);
char16_t *xstr8_to_path(const char *stra);
-void mangle_stub_cmdline(char16_t *cmdline);
+char16_t *mangle_stub_cmdline(char16_t *cmdline);
EFI_STATUS chunked_read(EFI_FILE *file, size_t *size, void *buf);
EFI_STATUS file_read(EFI_FILE *dir, const char16_t *name, size_t off, size_t size, char **content, size_t *content_size);