From 33d1febbd7bf4cd10ce537ec86a8e884c47c529a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 20 Nov 2023 17:04:46 +0100 Subject: [PATCH] iovec-util: rework IOVEC_MAKE_STRING() to work with compound initialized input This avoids the ({}) that IOVEC_MAKE_STRING() so far used and might cause a memory corruption if the parameter passed in is itself allocated via a compount initialized array or so. Also, this makes sure both IOVEC_MAKE_STRING() and IOVEC_MAKE() accept 'const' parameters without this causing a compiler warning. --- src/basic/iovec-util.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/basic/iovec-util.h b/src/basic/iovec-util.h index 3f5cdd02e10..038f342fb2d 100644 --- a/src/basic/iovec-util.h +++ b/src/basic/iovec-util.h @@ -12,12 +12,22 @@ size_t iovec_total_size(const struct iovec *iovec, size_t n); bool iovec_increment(struct iovec *iovec, size_t n, size_t k); -#define IOVEC_MAKE(base, len) (struct iovec) { .iov_base = (base), .iov_len = (len) } -#define IOVEC_MAKE_STRING(string) \ - ({ \ - const char *_s = (string); \ - IOVEC_MAKE((char*) _s, strlen(_s)); \ - }) +/* This accepts both const and non-const pointers */ +#define IOVEC_MAKE(base, len) \ + (struct iovec) { \ + .iov_base = (void*) (base), \ + .iov_len = (len), \ + } + +static inline struct iovec* iovec_make_string(struct iovec *iovec, const char *s) { + assert(iovec); + /* We don't use strlen_ptr() here, because we don't want to include string-util.h for now */ + *iovec = IOVEC_MAKE(s, s ? strlen(s) : 0); + return iovec; +} + +#define IOVEC_MAKE_STRING(s) \ + *iovec_make_string(&(struct iovec) {}, s) static inline void iovec_done(struct iovec *iovec) { /* A _cleanup_() helper that frees the iov_base in the iovec */ -- 2.47.3