ryantimwilson / rpms / systemd

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