ac3a84
From 1d93f12c7068dedf9393f8d4d86335e1f40537c3 Mon Sep 17 00:00:00 2001
ac3a84
From: Yu Watanabe <watanabe.yu+github@gmail.com>
ac3a84
Date: Fri, 20 Jan 2023 14:37:12 +0900
ac3a84
Subject: [PATCH] test-ndisc: fix memleak and fd leak
ac3a84
ac3a84
Fixes issues reported at #22576.
ac3a84
ac3a84
(cherry picked from commit 86d82cb8885afaac374225b945b2efc2a013cb7e)
ac3a84
ac3a84
Related: #2138081
ac3a84
---
ac3a84
 src/libsystemd-network/test-ndisc-ra.c | 20 ++++++--------------
ac3a84
 src/libsystemd-network/test-ndisc-rs.c | 21 ++++++++-------------
ac3a84
 2 files changed, 14 insertions(+), 27 deletions(-)
ac3a84
ac3a84
diff --git a/src/libsystemd-network/test-ndisc-ra.c b/src/libsystemd-network/test-ndisc-ra.c
ac3a84
index 001df4d473..bd8c0fd426 100644
ac3a84
--- a/src/libsystemd-network/test-ndisc-ra.c
ac3a84
+++ b/src/libsystemd-network/test-ndisc-ra.c
ac3a84
@@ -53,7 +53,6 @@ static uint8_t advertisement[] = {
ac3a84
 
ac3a84
 static bool test_stopped;
ac3a84
 static int test_fd[2];
ac3a84
-static sd_event_source *recv_router_advertisement;
ac3a84
 static struct {
ac3a84
         struct in6_addr address;
ac3a84
         unsigned char prefixlen;
ac3a84
@@ -281,9 +280,9 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat
ac3a84
 }
ac3a84
 
ac3a84
 TEST(ra) {
ac3a84
-        sd_event *e;
ac3a84
-        sd_radv *ra;
ac3a84
-        unsigned i;
ac3a84
+        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
ac3a84
+        _cleanup_(sd_event_source_unrefp) sd_event_source *recv_router_advertisement = NULL;
ac3a84
+        _cleanup_(sd_radv_unrefp) sd_radv *ra = NULL;
ac3a84
 
ac3a84
         assert_se(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) >= 0);
ac3a84
 
ac3a84
@@ -303,7 +302,7 @@ TEST(ra) {
ac3a84
         assert_se(sd_radv_set_rdnss(ra, 60, &test_rdnss, 1) >= 0);
ac3a84
         assert_se(sd_radv_set_dnssl(ra, 60, (char **)test_dnssl) >= 0);
ac3a84
 
ac3a84
-        for (i = 0; i < ELEMENTSOF(prefix); i++) {
ac3a84
+        for (unsigned i = 0; i < ELEMENTSOF(prefix); i++) {
ac3a84
                 sd_radv_prefix *p;
ac3a84
 
ac3a84
                 printf("Test prefix %u\n", i);
ac3a84
@@ -324,8 +323,8 @@ TEST(ra) {
ac3a84
                 assert_se(!p);
ac3a84
         }
ac3a84
 
ac3a84
-        assert_se(sd_event_add_io(e, &recv_router_advertisement, test_fd[0],
ac3a84
-                                  EPOLLIN, radv_recv, ra) >= 0);
ac3a84
+        assert_se(sd_event_add_io(e, &recv_router_advertisement, test_fd[0], EPOLLIN, radv_recv, ra) >= 0);
ac3a84
+        assert_se(sd_event_source_set_io_fd_own(recv_router_advertisement, true) >= 0);
ac3a84
 
ac3a84
         assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
ac3a84
                                              2 * USEC_PER_SEC, 0,
ac3a84
@@ -334,13 +333,6 @@ TEST(ra) {
ac3a84
         assert_se(sd_radv_start(ra) >= 0);
ac3a84
 
ac3a84
         assert_se(sd_event_loop(e) >= 0);
ac3a84
-
ac3a84
-        ra = sd_radv_unref(ra);
ac3a84
-        assert_se(!ra);
ac3a84
-
ac3a84
-        close(test_fd[0]);
ac3a84
-
ac3a84
-        sd_event_unref(e);
ac3a84
 }
ac3a84
 
ac3a84
 DEFINE_TEST_MAIN(LOG_DEBUG);
ac3a84
diff --git a/src/libsystemd-network/test-ndisc-rs.c b/src/libsystemd-network/test-ndisc-rs.c
ac3a84
index 3c679f60b5..e501b64377 100644
ac3a84
--- a/src/libsystemd-network/test-ndisc-rs.c
ac3a84
+++ b/src/libsystemd-network/test-ndisc-rs.c
ac3a84
@@ -10,6 +10,7 @@
ac3a84
 #include "sd-ndisc.h"
ac3a84
 
ac3a84
 #include "alloc-util.h"
ac3a84
+#include "fd-util.h"
ac3a84
 #include "hexdecoct.h"
ac3a84
 #include "icmp6-util.h"
ac3a84
 #include "socket-util.h"
ac3a84
@@ -255,8 +256,8 @@ static void test_callback(sd_ndisc *nd, sd_ndisc_event_t event, sd_ndisc_router
ac3a84
 }
ac3a84
 
ac3a84
 TEST(rs) {
ac3a84
-        sd_event *e;
ac3a84
-        sd_ndisc *nd;
ac3a84
+        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
ac3a84
+        _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
ac3a84
 
ac3a84
         send_ra_function = send_ra;
ac3a84
 
ac3a84
@@ -279,17 +280,13 @@ TEST(rs) {
ac3a84
         assert_se(sd_ndisc_start(nd) >= 0);
ac3a84
         assert_se(sd_ndisc_start(nd) >= 0);
ac3a84
         assert_se(sd_ndisc_stop(nd) >= 0);
ac3a84
+        test_fd[1] = safe_close(test_fd[1]);
ac3a84
 
ac3a84
         assert_se(sd_ndisc_start(nd) >= 0);
ac3a84
 
ac3a84
         assert_se(sd_event_loop(e) >= 0);
ac3a84
 
ac3a84
-        nd = sd_ndisc_unref(nd);
ac3a84
-        assert_se(!nd);
ac3a84
-
ac3a84
-        close(test_fd[1]);
ac3a84
-
ac3a84
-        sd_event_unref(e);
ac3a84
+        test_fd[1] = safe_close(test_fd[1]);
ac3a84
 }
ac3a84
 
ac3a84
 static int test_timeout_value(uint8_t flags) {
ac3a84
@@ -342,8 +339,8 @@ static int test_timeout_value(uint8_t flags) {
ac3a84
 }
ac3a84
 
ac3a84
 TEST(timeout) {
ac3a84
-        sd_event *e;
ac3a84
-        sd_ndisc *nd;
ac3a84
+        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
ac3a84
+        _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
ac3a84
 
ac3a84
         send_ra_function = test_timeout_value;
ac3a84
 
ac3a84
@@ -367,9 +364,7 @@ TEST(timeout) {
ac3a84
 
ac3a84
         assert_se(sd_event_loop(e) >= 0);
ac3a84
 
ac3a84
-        nd = sd_ndisc_unref(nd);
ac3a84
-
ac3a84
-        sd_event_unref(e);
ac3a84
+        test_fd[1] = safe_close(test_fd[1]);
ac3a84
 }
ac3a84
 
ac3a84
 DEFINE_TEST_MAIN(LOG_DEBUG);