From 38dbeec767ad10021020e3833b1dbc3d04442f85 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Fri, 27 Jan 2023 16:31:28 +0200 Subject: [PATCH] lib: Add memcpy() macro that assert-crashes if either parameter is NULL --- src/lib/buffer.c | 4 ++++ src/lib/lib.h | 8 ++++++++ src/lib/printf-format-fix.c | 4 ++++ src/lib/strfuncs.c | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/src/lib/buffer.c b/src/lib/buffer.c index a6d8fef03e..74f4c03b7f 100644 --- a/src/lib/buffer.c +++ b/src/lib/buffer.c @@ -6,6 +6,10 @@ #include "safe-memset.h" #include "buffer.h" +/* Disable our memcpy() safety wrapper. This file is very performance sensitive + and it's been checked to work correctly with memcpy(). */ +#undef memcpy + struct real_buffer { union { struct buffer buf; diff --git a/src/lib/lib.h b/src/lib/lib.h index 2b1ecde41d..16dc71f589 100644 --- a/src/lib/lib.h +++ b/src/lib/lib.h @@ -66,6 +66,14 @@ typedef void lib_atexit_callback_t(void); #define static_assert_array_size(arr, count) \ static_assert(N_ELEMENTS(arr) == (count), "array/enum size mismatch") +/* Using memcpy() with NULL pointers is undefined behavior. Make sure we don't + do that. */ +static inline void *i_memcpy(void *dest, const void *src, size_t n) { + i_assert(dest != NULL && src != NULL); + return memcpy(dest, src, n); +} +#define memcpy(dest, src, n) i_memcpy(dest, src, n) + /* /dev/null opened as O_WRONLY. Opened at lib_init(), so it can be accessed also inside chroots. */ extern int dev_null_fd; diff --git a/src/lib/printf-format-fix.c b/src/lib/printf-format-fix.c index f72253fef4..d68e4f6159 100644 --- a/src/lib/printf-format-fix.c +++ b/src/lib/printf-format-fix.c @@ -3,6 +3,10 @@ #include "lib.h" #include "printf-format-fix.h" +/* Disable our memcpy() safety wrapper. This file is very performance sensitive + and it's been checked to work correctly with memcpy(). */ +#undef memcpy + static const char * fix_format_real(const char *fmt, const char *p, size_t *len_r) { diff --git a/src/lib/strfuncs.c b/src/lib/strfuncs.c index 8aeecc10a8..4a4e1d1f59 100644 --- a/src/lib/strfuncs.c +++ b/src/lib/strfuncs.c @@ -12,6 +12,10 @@ #include #include +/* Disable our memcpy() safety wrapper. This file is very performance sensitive + and it's been checked to work correctly with memcpy(). */ +#undef memcpy + #define STRCONCAT_BUFSIZE 512 enum _str_trim_sides { -- 2.47.3