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.
This commit is contained in:
Thomas Haller 2021-05-26 09:31:07 +02:00
parent 9154f0128a
commit 8fcbbdd7a4
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -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__ */