From c5f4e36fd9147cfbb162383da857292727aab5ac Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 8 Oct 2016 19:03:16 +0200 Subject: [PATCH] shared: assert against buffer length in nm_sprintf_buf() --- shared/nm-utils/nm-macros-internal.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 1451053580..8b7510973c 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -646,23 +646,27 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) { #define nm_sprintf_buf(buf, format, ...) ({ \ char * _buf = (buf); \ + int _buf_len; \ \ /* some static assert trying to ensure that the buffer is statically allocated. * It disallows a buffer size of sizeof(gpointer) to catch that. */ \ G_STATIC_ASSERT (G_N_ELEMENTS (buf) == sizeof (buf) && sizeof (buf) != sizeof (char *)); \ - g_snprintf (_buf, sizeof (buf), \ - ""format"", ##__VA_ARGS__); \ + _buf_len = g_snprintf (_buf, sizeof (buf), \ + ""format"", ##__VA_ARGS__); \ + nm_assert (_buf_len < sizeof (buf)); \ _buf; \ }) #define nm_sprintf_bufa(n_elements, format, ...) \ ({ \ char *_buf; \ + int _buf_len; \ \ G_STATIC_ASSERT (sizeof (char[MAX ((n_elements), 1)]) == (n_elements)); \ _buf = g_alloca (n_elements); \ - g_snprintf (_buf, n_elements, \ - ""format"", ##__VA_ARGS__); \ + _buf_len = g_snprintf (_buf, (n_elements), \ + ""format"", ##__VA_ARGS__); \ + nm_assert (_buf_len < (n_elements)); \ _buf; \ })