From 8fcbbdd7a4ddf6dac62e0cc87f6027cc16d3f080 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 26 May 2021 09:31:07 +0200 Subject: [PATCH] all: reimplement g_strstrip() macro to avoid Coverity warning Coverity has issues with functions that handle ownership like g_strstrip(). Thus the scan is full of false positives like: Error: RESOURCE_LEAK (CWE-772): [#def45] [important] NetworkManager-1.31.5/src/core/devices/wwan/nm-service-providers.c:134: alloc_fn: Storage is returned from allocation function "g_strdup". NetworkManager-1.31.5/src/core/devices/wwan/nm-service-providers.c:134: noescape: Resource "g_strdup(attribute_values[i])" is not freed or pointed-to in "g_strchug". NetworkManager-1.31.5/src/core/devices/wwan/nm-service-providers.c:134: leaked_storage: Failing to save or free storage allocated by "g_strdup(attribute_values[i])" leaks it. # 132| if (strcmp(attribute_names[i], "value") == 0) { # 133| parse_context->state = PARSER_METHOD_GSM_APN; # 134|-> parse_context->apn = g_strstrip(g_strdup(attribute_values[i])); # 135| break; # 136| } Add a workaround for that. There are other functions that have the same problem, but the usage g_strstrip(g_strdup(...)) is common to warrant a special workaround. --- src/libnm-glib-aux/nm-glib.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libnm-glib-aux/nm-glib.h b/src/libnm-glib-aux/nm-glib.h index 44a7d19f82..66e5c16e14 100644 --- a/src/libnm-glib-aux/nm-glib.h +++ b/src/libnm-glib-aux/nm-glib.h @@ -693,4 +693,18 @@ _nm_g_cancellable_reset(GCancellable *cancellable); /*****************************************************************************/ +/* Coverity gets confused by g_strstrip(g_strdup(foo)). Reimplement the macro + * in a way that hopefully works better to avoid the false positive. */ +#undef g_strstrip +#define g_strstrip(str) \ + ({ \ + char *const _str = (str); \ + \ + g_strchug(_str); \ + g_strchomp(_str); \ + _str; \ + }) + +/*****************************************************************************/ + #endif /* __NM_GLIB_H__ */