From 961aa55d1701574db41db750ea87d2b40f254173 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Apr 07 2021 22:35:34 +0000 Subject: format-table: make sure we never call memcmp() with NULL parameters (cherry picked from commit 88db94fa57c9a5b1a0b926c49d3624fc84c88090) Related: #1689832 patch_name: 0134-format-table-make-sure-we-never-call-memcmp-with-NUL.patch present_in_specfile: true location_in_specfile: 134 squash_commits: true --- diff --git a/src/basic/format-table.c b/src/basic/format-table.c index 0a1777e..5517ada 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 27b5a09..b68ef25 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)))