From e7357419cd767aff36807ea8cabc0155271ddd88 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 29 Jun 2020 09:52:30 +0200 Subject: [PATCH] shared: add nm_str_buf_finalize_to_gbytes() helper --- shared/nm-glib-aux/nm-str-buf.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/shared/nm-glib-aux/nm-str-buf.h b/shared/nm-glib-aux/nm-str-buf.h index 61246969de..f4a3542c4f 100644 --- a/shared/nm-glib-aux/nm-str-buf.h +++ b/shared/nm-glib-aux/nm-str-buf.h @@ -429,6 +429,23 @@ nm_str_buf_finalize (NMStrBuf *strbuf, return g_steal_pointer (&strbuf->_priv_str); } +static inline GBytes * +nm_str_buf_finalize_to_gbytes (NMStrBuf *strbuf) +{ + char *s; + gsize l; + + /* this always returns a non-NULL, newly allocated GBytes instance. + * The data buffer always has an additional NUL character after + * the data, and the data is allocated with malloc. + * + * That means, the caller who takes ownership of the GBytes can + * safely modify the content of the buffer (including the additional + * NUL sentinel). */ + s = nm_str_buf_finalize (strbuf, &l); + return g_bytes_new_take (s ?: g_new0 (char, 1), l); +} + /** * nm_str_buf_destroy: * @strbuf: an initialized #NMStrBuf