From 02d307a23112b1e6f3ed2bd26ca4ac1bf71c3c0a Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Sat, 25 May 2019 23:32:28 +0200 Subject: [PATCH] posix.cfg: Added partial support for scandir() and a TODO comment. --- cfg/posix.cfg | 30 ++++++++++++++++++++++++++++++ test/cfg/posix.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/cfg/posix.cfg b/cfg/posix.cfg index 075fbab13..a0a80b8f8 100644 --- a/cfg/posix.cfg +++ b/cfg/posix.cfg @@ -4026,6 +4026,32 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s + + + + + + false + + + + + + + + + + + + + + + + + @@ -4863,6 +4889,10 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s posix_memalign free + + scandir + free + strdup strndup diff --git a/test/cfg/posix.c b/test/cfg/posix.c index f22406b91..3b4338e40 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -21,6 +21,38 @@ #include #include +void memleak_scandir(void) +{ + struct dirent **namelist; + int n = scandir(".", &namelist, NULL, alphasort); + if (n == -1) { + return; + } + + // http://man7.org/linux/man-pages/man3/scandir.3.html + /* The scandir() function scans the directory dirp, calling filter() on + each directory entry. Entries for which filter() returns nonzero are + stored in strings allocated via malloc(3), sorted using qsort(3) with + the comparison function compar(), and collected in array namelist + which is allocated via malloc(3). If filter is NULL, all entries are + selected.*/ + + // TODO: cppcheck-suppress memleak +} + +void no_memleak_scandir(void) +{ + struct dirent **namelist; + int n = scandir(".", &namelist, NULL, alphasort); + if (n == -1) { + return; + } + while (n--) { + free(namelist[n]); + } + free(namelist); +} + void validCode() { void *ptr;