ff6046
From 4339aa85279d268a671a656f142445559be771b7 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Wed, 14 Nov 2018 18:39:37 +0100
ff6046
Subject: [PATCH] format-table: make sure we never call memcmp() with NULL
ff6046
 parameters
ff6046
ff6046
(cherry picked from commit 88db94fa57c9a5b1a0b926c49d3624fc84c88090)
ff6046
ff6046
Related: #1689832
ff6046
---
ff6046
 src/basic/format-table.c | 2 +-
ff6046
 src/basic/util.h         | 9 +++++++++
ff6046
 2 files changed, 10 insertions(+), 1 deletion(-)
ff6046
ff6046
diff --git a/src/basic/format-table.c b/src/basic/format-table.c
ff6046
index 0a1777e9a2..5517adad82 100644
ff6046
--- a/src/basic/format-table.c
ff6046
+++ b/src/basic/format-table.c
ff6046
@@ -291,7 +291,7 @@ static bool table_data_matches(
ff6046
         if (k != l)
ff6046
                 return false;
ff6046
 
ff6046
-        return memcmp(data, d->data, l) == 0;
ff6046
+        return memcmp_safe(data, d->data, l) == 0;
ff6046
 }
ff6046
 
ff6046
 static TableData *table_data_new(
ff6046
diff --git a/src/basic/util.h b/src/basic/util.h
ff6046
index 27b5a09782..b68ef25ed8 100644
ff6046
--- a/src/basic/util.h
ff6046
+++ b/src/basic/util.h
ff6046
@@ -123,6 +123,15 @@ static inline void memcpy_safe(void *dst, const void *src, size_t n) {
ff6046
         memcpy(dst, src, n);
ff6046
 }
ff6046
 
ff6046
+/* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */
ff6046
+static inline int memcmp_safe(const void *s1, const void *s2, size_t n) {
ff6046
+        if (n == 0)
ff6046
+                return 0;
ff6046
+        assert(s1);
ff6046
+        assert(s2);
ff6046
+        return memcmp(s1, s2, n);
ff6046
+}
ff6046
+
ff6046
 int on_ac_power(void);
ff6046
 
ff6046
 #define memzero(x,l) (memset((x), 0, (l)))