ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
4bff0a
From 6bb1d52f0554f687ef27de46c0b9daac9d256d60 Mon Sep 17 00:00:00 2001
4bff0a
From: Lennart Poettering <lennart@poettering.net>
4bff0a
Date: Wed, 10 Oct 2018 11:34:30 +0200
4bff0a
Subject: [PATCH] tree-wide: various ubsan zero size memory fixes
4bff0a
4bff0a
Fixes: #10346
4bff0a
(cherry picked from commit 65f95765d05ddcd9e5849b68c379afa7e87d1248)
4bff0a
4bff0a
Resolves: #1683319
4bff0a
---
4bff0a
 src/basic/bitmap.c        | 2 +-
4bff0a
 src/basic/util.h          | 8 +++++++-
4bff0a
 src/test/test-hexdecoct.c | 2 +-
4bff0a
 3 files changed, 9 insertions(+), 3 deletions(-)
4bff0a
4bff0a
diff --git a/src/basic/bitmap.c b/src/basic/bitmap.c
4bff0a
index c17c6a7a02..a4cd6451b0 100644
4bff0a
--- a/src/basic/bitmap.c
4bff0a
+++ b/src/basic/bitmap.c
4bff0a
@@ -206,7 +206,7 @@ bool bitmap_equal(Bitmap *a, Bitmap *b) {
4bff0a
                 return true;
4bff0a
 
4bff0a
         common_n_bitmaps = MIN(a->n_bitmaps, b->n_bitmaps);
4bff0a
-        if (memcmp(a->bitmaps, b->bitmaps, sizeof(uint64_t) * common_n_bitmaps) != 0)
4bff0a
+        if (memcmp_safe(a->bitmaps, b->bitmaps, sizeof(uint64_t) * common_n_bitmaps) != 0)
4bff0a
                 return false;
4bff0a
 
4bff0a
         c = a->n_bitmaps > b->n_bitmaps ? a : b;
4bff0a
diff --git a/src/basic/util.h b/src/basic/util.h
4bff0a
index b68ef25ed8..4659a21b06 100644
4bff0a
--- a/src/basic/util.h
4bff0a
+++ b/src/basic/util.h
4bff0a
@@ -134,7 +134,13 @@ static inline int memcmp_safe(const void *s1, const void *s2, size_t n) {
4bff0a
 
4bff0a
 int on_ac_power(void);
4bff0a
 
4bff0a
-#define memzero(x,l) (memset((x), 0, (l)))
4bff0a
+#define memzero(x,l)                                            \
4bff0a
+        ({                                                      \
4bff0a
+                size_t _l_ = (l);                               \
4bff0a
+                void *_x_ = (x);                                \
4bff0a
+                _l_ == 0 ? _x_ : memset(_x_, 0, _l_);           \
4bff0a
+        })
4bff0a
+
4bff0a
 #define zero(x) (memzero(&(x), sizeof(x)))
4bff0a
 
4bff0a
 static inline void *mempset(void *s, int c, size_t n) {
4bff0a
diff --git a/src/test/test-hexdecoct.c b/src/test/test-hexdecoct.c
4bff0a
index da9f3008bb..a972ddcef7 100644
4bff0a
--- a/src/test/test-hexdecoct.c
4bff0a
+++ b/src/test/test-hexdecoct.c
4bff0a
@@ -84,7 +84,7 @@ static void test_unhexmem_one(const char *s, size_t l, int retval) {
4bff0a
                         l = strlen(s);
4bff0a
 
4bff0a
                 assert_se(hex = hexmem(mem, len));
4bff0a
-                answer = strndupa(s, l);
4bff0a
+                answer = strndupa(s ?: "", l);
4bff0a
                 assert_se(streq(delete_chars(answer, WHITESPACE), hex));
4bff0a
         }
4bff0a
 }