From c6809df4cdf2f909ffcd98e842447fca523f1c0b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 22 Jun 2020 21:19:42 +0200 Subject: [PATCH] shared: make NM_STR_BUF_INIT() an inline function In the previous form, NM_STR_BUF_INIT() was a macro. That makes sense, however it's not really possible to make that a macro without evaluating the reservation length multiple times. That means, NMStrBuf strbuf = NM_STR_BUF_INIT (nmtst_get_rand_uint32 () % 100, FALSE); leads to a crash. That is unfortunate, so instead make it an inline function that returns a NMStrBut struct. Usually, we avoid functions that returns structs, but here we do it. --- shared/nm-glib-aux/nm-str-buf.h | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/shared/nm-glib-aux/nm-str-buf.h b/shared/nm-glib-aux/nm-str-buf.h index 7121ba609a..b582e2c8a6 100644 --- a/shared/nm-glib-aux/nm-str-buf.h +++ b/shared/nm-glib-aux/nm-str-buf.h @@ -39,13 +39,18 @@ _nm_str_buf_assert (NMStrBuf *strbuf) nm_assert (strbuf->_priv_len <= strbuf->_priv_allocated); } -#define NM_STR_BUF_INIT(len, do_bzero_mem) \ - ((NMStrBuf) { \ - ._priv_str = (len) ? g_malloc (len) : NULL, \ - ._priv_allocated = (len), \ - ._priv_len = 0, \ - ._priv_do_bzero_mem = (do_bzero_mem), \ - }) +static inline NMStrBuf +NM_STR_BUF_INIT (gsize allocated, gboolean do_bzero_mem) +{ + NMStrBuf strbuf = { + ._priv_str = allocated ? g_malloc (allocated) : NULL, + ._priv_allocated = allocated, + ._priv_len = 0, + ._priv_do_bzero_mem = do_bzero_mem, + }; + + return strbuf; +} static inline void nm_str_buf_init (NMStrBuf *strbuf,