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.
This commit is contained in:
Thomas Haller 2021-05-11 22:58:51 +02:00
parent 4bc9c59c07
commit 77fb782060
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -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);