From 637c785f4eaeac3f4804aedd765bde0629742285 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 19 Jun 2019 15:17:05 +0200 Subject: [PATCH] shared: fix nm_utils_bin2hexstr_full() for buffers of length zero --- shared/nm-glib-aux/nm-shared-utils.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index c8b0eef84e..49037ed7b4 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -2906,10 +2906,13 @@ nm_utils_memeqzero (gconstpointer data, gsize length) * be returned and must be freed by the caller. * If not %NULL, the buffer must already be preallocated and contain * at least (@length*2+1) or (@length*3) bytes, depending on the delimiter. + * If @length is zero, then of course at least one byte will be allocated + * or @out (if given) must contain at least room for the trailing NUL byte. * * Returns: the binary value converted to a hex string. If @out is given, * this always returns @out. If @out is %NULL, a newly allocated string - * is returned. + * is returned. This never returns %NULL, for buffers of length zero + * an empty string is returend. */ char * nm_utils_bin2hexstr_full (gconstpointer addr, @@ -2925,9 +2928,11 @@ nm_utils_bin2hexstr_full (gconstpointer addr, if (out) out0 = out; else { - out0 = out = g_new (char, delimiter == '\0' - ? length * 2 + 1 - : length * 3); + out0 = out = g_new (char, length == 0 + ? 1u + : ( delimiter == '\0' + ? length * 2u + 1u + : length * 3u)); } /* @out must contain at least @length*3 bytes if @delimiter is set,