daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
Blob Blame History Raw
From 4339aa85279d268a671a656f142445559be771b7 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Wed, 14 Nov 2018 18:39:37 +0100
Subject: [PATCH] format-table: make sure we never call memcmp() with NULL
 parameters

(cherry picked from commit 88db94fa57c9a5b1a0b926c49d3624fc84c88090)

Related: #1689832
---
 src/basic/format-table.c | 2 +-
 src/basic/util.h         | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/basic/format-table.c b/src/basic/format-table.c
index 0a1777e9a2..5517adad82 100644
--- a/src/basic/format-table.c
+++ b/src/basic/format-table.c
@@ -291,7 +291,7 @@ static bool table_data_matches(
         if (k != l)
                 return false;
 
-        return memcmp(data, d->data, l) == 0;
+        return memcmp_safe(data, d->data, l) == 0;
 }
 
 static TableData *table_data_new(
diff --git a/src/basic/util.h b/src/basic/util.h
index 27b5a09782..b68ef25ed8 100644
--- a/src/basic/util.h
+++ b/src/basic/util.h
@@ -123,6 +123,15 @@ static inline void memcpy_safe(void *dst, const void *src, size_t n) {
         memcpy(dst, src, n);
 }
 
+/* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */
+static inline int memcmp_safe(const void *s1, const void *s2, size_t n) {
+        if (n == 0)
+                return 0;
+        assert(s1);
+        assert(s2);
+        return memcmp(s1, s2, n);
+}
+
 int on_ac_power(void);
 
 #define memzero(x,l) (memset((x), 0, (l)))