From: Lennart Poettering Date: Mon, 20 Nov 2023 16:04:46 +0000 (+0100) Subject: iovec-util: rework IOVEC_MAKE_STRING() to work with compound initialized input X-Git-Tag: v256-rc1~1263^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33d1febbd7bf4cd10ce537ec86a8e884c47c529a;p=thirdparty%2Fsystemd.git 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. --- 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 */