]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: use CMP() macro for safety 23583/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 31 May 2022 21:36:55 +0000 (06:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 2 Jun 2022 19:20:46 +0000 (04:20 +0900)
src/boot/efi/efi-string.c
src/boot/efi/test-efi-string.c

index 22367ed73858b0c687a5f180e01a00b0c07b9ddc..4b405155a03975097bc8425925dfa27c8f88da04 100644 (file)
@@ -60,14 +60,13 @@ DEFINE_STRTOLOWER(char16_t, strtolower16);
                         return CMP(s1, s2);                  \
                                                              \
                 while (n > 0) {                              \
-                        int c1 = *s1;                        \
-                        int c2 = *s2;                        \
+                        type c1 = *s1, c2 = *s2;             \
                         if (tolower) {                       \
                                 c1 = TOLOWER(c1);            \
                                 c2 = TOLOWER(c2);            \
                         }                                    \
                         if (!c1 || c1 != c2)                 \
-                                return c1 - c2;              \
+                                return CMP(c1, c2);          \
                                                              \
                         s1++;                                \
                         s2++;                                \
@@ -141,13 +140,16 @@ DEFINE_STRNDUP(char, xstrndup8, strnlen8);
 DEFINE_STRNDUP(char16_t, xstrndup16, strnlen16);
 
 int efi_memcmp(const void *p1, const void *p2, size_t n) {
+        const uint8_t *up1 = p1, *up2 = p2;
+        int r;
+
         if (!p1 || !p2)
                 return CMP(p1, p2);
 
-        const uint8_t *up1 = p1, *up2 = p2;
         while (n > 0) {
-                if (*up1 != *up2)
-                        return *up1 - *up2;
+                r = CMP(*up1, *up2);
+                if (r != 0)
+                        return r;
 
                 up1++;
                 up2++;
index d5044d801c719006940f4025e07c89ce89fdcd1f..b688c6ae4101168f54ddf728ca6db7c53eaa8cc0 100644 (file)
@@ -335,6 +335,7 @@ TEST(efi_memcmp) {
         assert_se(efi_memcmp("a", "A", 1) > 0);
         assert_se(efi_memcmp("abc", "ab", 2) == 0);
         assert_se(efi_memcmp("ab", "abc", 3) < 0);
+        assert_se(efi_memcmp("abc", "ab", 3) > 0);
         assert_se(efi_memcmp("ab\000bd", "ab\000bd", 6) == 0);
         assert_se(efi_memcmp("ab\000b\0", "ab\000bd", 6) < 0);
 }