]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Use strlen8/16
authorJan Janssen <medhefgo@web.de>
Mon, 23 May 2022 10:32:50 +0000 (12:32 +0200)
committerJan Janssen <medhefgo@web.de>
Tue, 31 May 2022 12:18:43 +0000 (14:18 +0200)
The casts in this and the next few commits are curently necessary
because CHAR8 is defined as uint8_t in gnu-efi, while char is signed.
Once we switch from gnu-efi typedefs to stdint types, the casts
will be dropped.

src/boot/efi/bcd.c
src/boot/efi/boot.c
src/boot/efi/cpio.c
src/boot/efi/meson.build
src/boot/efi/pe.c
src/boot/efi/random-seed.c
src/boot/efi/util.c
src/boot/efi/util.h
src/fundamental/string-util-fundamental.h

index 3eaabdd5387b7083edaf5aad46517edc69787fb7..14d74ae38725707ae0f21d0103be3d0524658ff6 100644 (file)
@@ -7,9 +7,7 @@
 #  define TEST_STATIC
 #else
 /* Provide our own "EFI API" if we are running as a unit test. */
-#  include <stddef.h>
-#  include <stdint.h>
-#  include <uchar.h>
+#  include "efi-string.h"
 #  include "string-util-fundamental.h"
 
 #  define CHAR8 char
@@ -19,7 +17,6 @@
 #  define UINT32 uint32_t
 #  define UINT64 uint64_t
 #  define UINTN size_t
-#  define strlena(s) strlen(s)
 #  define strncaseeqa(a, b, n) strncaseeq((a), (b), (n))
 #  define TEST_STATIC static
 #endif
@@ -168,7 +165,7 @@ static const Key *get_key(const UINT8 *bcd, UINT32 bcd_len, UINT32 offset, const
                 return NULL;
 
         if (*name) {
-                if (strncaseeqa(name, key->key_name, key->key_name_len) && strlena(name) == key->key_name_len)
+                if (strncaseeqa(name, key->key_name, key->key_name_len) && strlen8((const char *) name) == key->key_name_len)
                         name += key->key_name_len;
                 else
                         return NULL;
@@ -213,7 +210,7 @@ static const KeyValue *get_key_value(const UINT8 *bcd, UINT32 bcd_len, const Key
                 if (BAD_OFFSET(kv->data_offset, kv->data_size, bcd_len))
                         continue;
 
-                if (strncaseeqa(name, kv->name, kv->name_len) && strlena(name) == kv->name_len)
+                if (strncaseeqa(name, kv->name, kv->name_len) && strlen8((const char *) name) == kv->name_len)
                         return kv;
         }
 
index c9ee403c674737872d518605565262e99c8390c4..52be68289670a998cc25d8086015b066c30c64d5 100644 (file)
@@ -149,7 +149,7 @@ static BOOLEAN line_edit(
 
         assert(line_in);
 
-        len = strlen_ptr(*line_in);
+        len = strlen16(*line_in);
         size = len + 1024;
         line = xnew(CHAR16, size);
         print = xnew(CHAR16, x_max + 1);
@@ -662,7 +662,7 @@ static BOOLEAN menu_run(
                         /* length of the longest entry */
                         line_width = 0;
                         for (UINTN i = 0; i < config->entry_count; i++)
-                                line_width = MAX(line_width, StrLen(config->entries[i]->title_show));
+                                line_width = MAX(line_width, strlen16(config->entries[i]->title_show));
                         line_width = MIN(line_width + 2 * entry_padding, x_max);
 
                         /* offsets to center the entries on the screen */
@@ -686,7 +686,7 @@ static BOOLEAN menu_run(
                                 UINTN j, padding;
 
                                 lines[i] = xnew(CHAR16, line_width + 1);
-                                padding = (line_width - MIN(StrLen(config->entries[i]->title_show), line_width)) / 2;
+                                padding = (line_width - MIN(strlen16(config->entries[i]->title_show), line_width)) / 2;
 
                                 for (j = 0; j < padding; j++)
                                         lines[i][j] = ' ';
@@ -750,7 +750,7 @@ static BOOLEAN menu_run(
                          * input. Therefore, draw one less character then we could for the status message.
                          * Note that the same does not apply for the separator line as it will never be drawn
                          * on the last line. */
-                        UINTN len = StrnLen(status, x_max - 1);
+                        UINTN len = strnlen16(status, x_max - 1);
                         UINTN x = (x_max - len) / 2;
                         status[len] = '\0';
                         print_at(0, y_status, COLOR_NORMAL, clearline + x_max - x);
@@ -1277,13 +1277,13 @@ static void config_entry_parse_tries(
          * foobar+4-0.efi → foobar+3-1.efi → foobar+2-2.efi → foobar+1-3.efi → foobar+0-4.efi → STOP!
          */
 
-        i = StrLen(file);
+        i = strlen16(file);
 
         /* Chop off any suffix such as ".conf" or ".efi" */
         if (suffix) {
                 UINTN suffix_length;
 
-                suffix_length = StrLen(suffix);
+                suffix_length = strlen16(suffix);
                 if (i < suffix_length)
                         return;
 
@@ -1829,7 +1829,7 @@ static void config_title_generate(Config *config) {
                 config->entries[i]->title_show = xpool_print(
                         L"%s (%.*s)",
                         t,
-                        StrnLen(config->entries[i]->machine_id, 8),
+                        strnlen16(config->entries[i]->machine_id, 8),
                         config->entries[i]->machine_id);
         }
 
index 14957130be00bc9ba8bfec918b6610f906c53138..5de124f113786010bd70fef4afcb861da053d9a6 100644 (file)
@@ -81,12 +81,12 @@ static EFI_STATUS pack_cpio_one(
 
         l = 6 + 13*8 + 1 + 1; /* Fixed CPIO header size, slash separator, and NUL byte after the file name*/
 
-        target_dir_prefix_size = strlena(target_dir_prefix);
+        target_dir_prefix_size = strlen8((const char *) target_dir_prefix);
         if (l > UINTN_MAX - target_dir_prefix_size)
                 return EFI_OUT_OF_RESOURCES;
         l += target_dir_prefix_size;
 
-        fname_size = StrLen(fname);
+        fname_size = strlen16(fname);
         if (l > UINTN_MAX - fname_size)
                 return EFI_OUT_OF_RESOURCES;
         l += fname_size; /* append space for file name */
@@ -182,7 +182,7 @@ static EFI_STATUS pack_cpio_dir(
 
         l = 6 + 13*8 + 1; /* Fixed CPIO header size, and NUL byte after the file name*/
 
-        path_size = strlena(path);
+        path_size = strlen8((const char *) path);
         if (l > UINTN_MAX - path_size)
                 return EFI_OUT_OF_RESOURCES;
         l += path_size;
@@ -388,7 +388,7 @@ EFI_STATUS pack_cpio(
                         continue;
                 if (!is_ascii(dirent->FileName))
                         continue;
-                if (StrLen(dirent->FileName) > 255) /* Max filename size on Linux */
+                if (strlen16(dirent->FileName) > 255) /* Max filename size on Linux */
                         continue;
 
                 d = xstrdup(dirent->FileName);
index f6c19fea33749995654d0066a270ec118ed016cb..1e42f8b08e0c297ab24a6bccf89777d647684b90 100644 (file)
@@ -194,6 +194,7 @@ efi_cflags = cc.get_supported_arguments(
         '-isystem', efi_incdir,
         '-isystem', efi_incdir / efi_arch[1],
         '-I', fundamental_path,
+        '-I', meson.current_source_dir(),
         '-DSD_BOOT',
         '-DGNU_EFI_USE_MS_ABI',
         '-include', efi_config_h,
@@ -390,14 +391,14 @@ tests += [
 if efi_arch[1] in ['ia32', 'x86_64', 'arm', 'aarch64']
         systemd_boot_sources += files('bcd.c')
         tests += [
-                [files('test-bcd.c'),
+                [files('test-bcd.c', 'efi-string.c'),
                  [],
                  [libzstd],
                  [],
                  'HAVE_ZSTD'],
         ]
         fuzzers += [
-                [files('fuzz-bcd.c')],
+                [files('fuzz-bcd.c', 'efi-string.c')],
         ]
 endif
 
index 568dcf40124f92e40aacb2d21b40b8b6a7a93943..daa76388627fd5d862232ecba96cf1a2a51da31e 100644 (file)
@@ -154,7 +154,7 @@ static void locate_sections(
                 const struct PeSectionHeader *sect = section_table + i;
 
                 for (UINTN j = 0; sections[j]; j++) {
-                        if (CompareMem(sect->Name, sections[j], strlena(sections[j])) != 0)
+                        if (CompareMem(sect->Name, sections[j], strlen8((const char *) sections[j])) != 0)
                                 continue;
 
                         if (addrs)
index b01e232f63637e5ce77ebb544578d6e21716d04f..2ff8299fa65d1f55a94003411bba6dbe38fb512d 100644 (file)
@@ -220,7 +220,7 @@ static void validate_sha256(void) {
                 uint8_t result[HASH_VALUE_SIZE];
 
                 sha256_init_ctx(&hash);
-                sha256_process_bytes(array[i].string, strlena((const CHAR8*) array[i].string), &hash);
+                sha256_process_bytes(array[i].string, strlen8(array[i].string), &hash);
                 sha256_finish_ctx(&hash, result);
 
                 assert(CompareMem(result, array[i].hash, HASH_VALUE_SIZE) == 0);
index 169add8680e9d7120468dd888c590254c1905a34..80d83f25b43d9c93427ad00f34466f87affa4b7c 100644 (file)
@@ -306,7 +306,7 @@ CHAR16 *xstra_to_str(const CHAR8 *stra) {
 
         assert(stra);
 
-        len = strlena(stra);
+        len = strlen8((const char *) stra);
         str = xnew(CHAR16, len + 1);
 
         strlen = 0;
@@ -336,7 +336,7 @@ CHAR16 *xstra_to_path(const CHAR8 *stra) {
 
         assert(stra);
 
-        len = strlena(stra);
+        len = strlen8((const char *) stra);
         str = xnew(CHAR16, len + 2);
 
         str[0] = '\\';
@@ -572,19 +572,6 @@ EFI_STATUS readdir_harder(
         return EFI_SUCCESS;
 }
 
-UINTN strnlena(const CHAR8 *p, UINTN maxlen) {
-        UINTN c;
-
-        if (!p)
-                return 0;
-
-        for (c = 0; c < maxlen; c++)
-                if (p[c] == 0)
-                        break;
-
-        return c;
-}
-
 INTN strncasecmpa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen) {
         if (!a || !b)
                 return CMP(a, b);
@@ -615,7 +602,7 @@ CHAR8 *xstrndup8(const CHAR8 *p, UINTN sz) {
 
         assert(p || sz == 0);
 
-        sz = strnlena(p, sz);
+        sz = strnlen8((const char *) p, sz);
 
         n = xallocate_pool(sz + 1);
 
index aca6937ac713b28b972986acbf1fe081dd69ecf9..b92dc18ed481627ecc1d9d4a5d1c75fa384c95e3 100644 (file)
@@ -120,7 +120,6 @@ EFI_STATUS get_file_info_harder(EFI_FILE *handle, EFI_FILE_INFO **ret, UINTN *re
 
 EFI_STATUS readdir_harder(EFI_FILE *handle, EFI_FILE_INFO **buffer, UINTN *buffer_size);
 
-UINTN strnlena(const CHAR8 *p, UINTN maxlen);
 CHAR8 *xstrndup8(const CHAR8 *p, UINTN sz);
 INTN strncasecmpa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen);
 static inline BOOLEAN strncaseeqa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen) {
index 01df4ce17ae76cb9b229cd948380bf32738af039..c9f712463c17b56685167d5e34c493c9faeca8d6 100644 (file)
@@ -2,23 +2,24 @@
 #pragma once
 
 #ifdef SD_BOOT
-#include <efi.h>
-#include <efilib.h>
+#  include <efi.h>
+#  include <efilib.h>
+#  include "efi-string.h"
 #else
-#include <string.h>
+#  include <string.h>
 #endif
 
 #include "macro-fundamental.h"
 
 #ifdef SD_BOOT
-#define strlen(a)        StrLen((a))
-#define strcmp(a, b)     StrCmp((a), (b))
-#define strncmp(a, b, n) StrnCmp((a), (b), (n))
-#define strcasecmp(a, b) StriCmp((a), (b))
-#define STR_C(str)       (L ## str)
-#define memcmp(a, b, n)  CompareMem(a, b, n)
+#  define strlen(a)        strlen16((a))
+#  define strcmp(a, b)     StrCmp((a), (b))
+#  define strncmp(a, b, n) StrnCmp((a), (b), (n))
+#  define strcasecmp(a, b) StriCmp((a), (b))
+#  define STR_C(str)       (L ## str)
+#  define memcmp(a, b, n)  CompareMem(a, b, n)
 #else
-#define STR_C(str)       (str)
+#  define STR_C(str)       (str)
 #endif
 
 #define streq(a,b) (strcmp((a),(b)) == 0)