]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
test: Fix short lived memory leaks
authorGuillem Jover <guillem@hadrons.org>
Sun, 16 Aug 2020 20:01:42 +0000 (22:01 +0200)
committerGuillem Jover <guillem@hadrons.org>
Mon, 21 Dec 2020 16:44:26 +0000 (17:44 +0100)
These are non-issues, but having a clean ASAN test suite makes it
possible to detect actual problems in the tested code.

Warned-by: gcc ASAN
test/fgetln.c
test/setmode.c

index 7d1e9dc3961f8e006397e057315aceb5a7b81144..5e04281ca2b1cab3890a975a69ac3afba49e7b86 100644 (file)
@@ -61,6 +61,7 @@ static const wchar_t *data_wide[] = {
 
 struct file {
        FILE *fp;
+       void *line_alloc;
        const void **lines;
 
        const void *got_buf;
@@ -97,6 +98,7 @@ test_fgetln_multi(void)
                str = strdup("A\n");
                str[0] += i;
 
+               files[i].line_alloc = str;
                files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
                files[i].lines[0] = str;
                files[i].lines[1] = str;
@@ -123,8 +125,11 @@ test_fgetln_multi(void)
                }
        }
 
-       for (i = 0; i < LINE_COUNT; i++)
+       for (i = 0; i < FILE_COUNT; i++) {
+               free(files[i].line_alloc);
+               free(files[i].lines);
                pipe_close(files[i].fp);
+       }
 }
 
 static void
@@ -159,6 +164,7 @@ test_fgetwln_multi(void)
                wstr = wcsdup(L"A\n");
                wstr[0] += i;
 
+               files[i].line_alloc = wstr;
                files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
                files[i].lines[0] = wstr;
                files[i].lines[1] = wstr;
@@ -185,8 +191,11 @@ test_fgetwln_multi(void)
                }
        }
 
-       for (i = 0; i < LINE_COUNT; i++)
+       for (i = 0; i < FILE_COUNT; i++) {
+               free(files[i].line_alloc);
+               free(files[i].lines);
                pipe_close(files[i].fp);
+       }
 }
 
 static void
index c6fbcba34046c89aa9483cb53938bcfe2fc4f1ae..c72deb8779ea6c33efc053dd5a1ddd30ade97af5 100644 (file)
  */
 
 #include <assert.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 int
 main(int argc, char **argv)
 {
+       void *set;
+
        umask(0);
 
-       assert(getmode(setmode("0"), 0) == 0);
+       set = setmode("0");
+       assert(getmode(set, 0) == 0);
+       free(set);
+
+       set = setmode("7777");
+       assert(getmode(set, 0) == 07777);
+       free(set);
 
-       assert(getmode(setmode("7777"), 0) == 07777);
-       assert(getmode(setmode("1555"), 0) == 01555);
+       set = setmode("1555");
+       assert(getmode(set, 0) == 01555);
+       free(set);
 
-       assert(getmode(setmode("ugo=rwx"), 0) == 0777);
+       set = setmode("ugo=rwx");
+       assert(getmode(set, 0) == 0777);
+       free(set);
 
        /* FIXME: Complete unit tests. */