ff6046
From fe6895fb6a5f8c61f0c47aa95e1c86bb88b7cf4f Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Tue, 21 Aug 2018 19:44:48 +0200
ff6046
Subject: [PATCH] test-socket-util: avoid "memleak" reported by valgrind
ff6046
ff6046
valgrind reports the allocation done in the short-lived child as a leak.
ff6046
Let's restructure the code to avoid this.
ff6046
ff6046
(cherry picked from commit 181c4ba750770b54a54b5abbe8ae8ff4f6db59b5)
ff6046
ff6046
Resolves: #1683319
ff6046
---
ff6046
 src/test/test-socket-util.c | 15 +++++++++------
ff6046
 1 file changed, 9 insertions(+), 6 deletions(-)
ff6046
ff6046
diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c
ff6046
index ac2ea52a5c..588485d881 100644
ff6046
--- a/src/test/test-socket-util.c
ff6046
+++ b/src/test/test-socket-util.c
ff6046
@@ -431,7 +431,6 @@ static void test_getpeercred_getpeergroups(void) {
ff6046
         if (r == 0) {
ff6046
                 static const gid_t gids[] = { 3, 4, 5, 6, 7 };
ff6046
                 gid_t *test_gids;
ff6046
-                _cleanup_free_ gid_t *peer_groups = NULL;
ff6046
                 size_t n_test_gids;
ff6046
                 uid_t test_uid;
ff6046
                 gid_t test_gid;
ff6046
@@ -472,12 +471,16 @@ static void test_getpeercred_getpeergroups(void) {
ff6046
                 assert_se(ucred.gid == test_gid);
ff6046
                 assert_se(ucred.pid == getpid_cached());
ff6046
 
ff6046
-                r = getpeergroups(pair[0], &peer_groups);
ff6046
-                assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
ff6046
+                {
ff6046
+                        _cleanup_free_ gid_t *peer_groups = NULL;
ff6046
 
ff6046
-                if (r >= 0) {
ff6046
-                        assert_se((size_t) r == n_test_gids);
ff6046
-                        assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
ff6046
+                        r = getpeergroups(pair[0], &peer_groups);
ff6046
+                        assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
ff6046
+
ff6046
+                        if (r >= 0) {
ff6046
+                                assert_se((size_t) r == n_test_gids);
ff6046
+                                assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
ff6046
+                        }
ff6046
                 }
ff6046
 
ff6046
                 safe_close_pair(pair);