From 5390588cdae84c6be7669e50dbc180adaa9bbbb8 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 2 Aug 2019 07:41:32 +0200 Subject: [PATCH] gnu.cfg: Add xfree() (#2051) It is hard to find good references, one that describes it a bit can be found here: https://manpages.ubuntu.com/manpages/bionic/man3/xmalloc.3pub.html xfree() can be used instead of free(). A check, to verify that a memory leak is found if the memory allocated via xmalloc() is not freed, has also been added. --- cfg/gnu.cfg | 8 ++++++++ test/cfg/gnu.c | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/cfg/gnu.cfg b/cfg/gnu.cfg index 688145e2f..d22bdbef8 100644 --- a/cfg/gnu.cfg +++ b/cfg/gnu.cfg @@ -14,6 +14,7 @@ xstrdup xrealloc free + xfree @@ -66,6 +67,13 @@ + + + + + + + diff --git a/test/cfg/gnu.c b/test/cfg/gnu.c index db3531e3a..e0daf2f98 100644 --- a/test/cfg/gnu.c +++ b/test/cfg/gnu.c @@ -19,6 +19,7 @@ extern void *xcalloc(size_t nmemb, size_t size); extern void *xmalloc(size_t size); extern void *xrealloc(void *block, size_t newsize); +extern void xfree(void *ptr); void resourceLeak_mkostemps(char *template, int suffixlen, int flags) { @@ -76,8 +77,19 @@ int no_resourceLeak_mkostemp_02(char *template, int flags) void valid_code(int argInt1) { + char *p; + if (__builtin_expect(argInt1, 0)) {} if (__builtin_expect_with_probability(argInt1 + 1, 2, 0.5)) {} + + p = (char *)malloc(10); + free(p); + p = (char *)malloc(5); + xfree(p); + p = (char *)xmalloc(10); + free(p); + p = (char *)xmalloc(5); + xfree(p); } void ignoreleak(void) @@ -98,6 +110,13 @@ void memleak_asprintf(char **ptr, const char *fmt, const int arg) } } +void memleak_xmalloc() +{ + char *p = (char*)xmalloc(10); + p[9] = 0; + // cppcheck-suppress memleak +} + void uninitvar__builtin_memset(void) { void *s;