From 77fb782060495121a34ff67b81d33aa3ea27ea71 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 11 May 2021 22:58:51 +0200 Subject: [PATCH] glib-aux: avoid potential undefined behavior for nm_str_buf_append_printf() The string buffer may be empty and _priv_str still %NULL. Doing pointer arithmetic with a %NULL pointer is undefined behavior. Avoid that. It's probably not an issue, because it results in computing &(((char *) NULL)[0], and then g_vsnprintf() would not even inspect the pointer (so it doesn't matter whether the computed pointer is bogus). But still, there is undefined behavior involved. --- src/libnm-glib-aux/nm-shared-utils.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c index cf9ba6f993..26856cd1ee 100644 --- a/src/libnm-glib-aux/nm-shared-utils.c +++ b/src/libnm-glib-aux/nm-shared-utils.c @@ -5555,7 +5555,10 @@ nm_str_buf_append_printf(NMStrBuf *strbuf, const char *format, ...) nm_assert(available < G_MAXULONG); va_start(args, format); - l = g_vsnprintf(&strbuf->_priv_str[strbuf->_priv_len], available, format, args); + l = g_vsnprintf(strbuf->_priv_str ? &strbuf->_priv_str[strbuf->_priv_len] : NULL, + available, + format, + args); va_end(args); nm_assert(l >= 0);