netlink: fix a memory leak
authorDavid Härdeman <david@hardeman.nu>
Sat, 10 Feb 2024 00:44:48 +0000 (01:44 +0100)
committerdedeckeh <dedeckeh@gmail.com>
Mon, 30 Dec 2024 19:44:26 +0000 (20:44 +0100)
valgrind noted that addrs is allocated in netlink_get_interface_linklocal(),
but never freed in one case. While I was looking at that, I found a mysterious
double-realloc in netlink.c.

Signed-off-by: David Härdeman <david@hardeman.nu>
src/config.c
src/netlink.c

index 42fc2fe0caeb16b164834292ae3a331253b023b9..78dd0a2865fde4f09a60c4d692811dd6272ad4d2 100644 (file)
@@ -938,13 +938,12 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        addrs_len = netlink_get_interface_linklocal(iface->ifindex, &addrs);
        if (addrs_len > 0) {
                for (ssize_t i = 0; i < addrs_len; i++) {
-                       struct odhcpd_ipaddr *addr = &addrs[i];
-
-                       if (!addr->tentative) {
+                       if (!addrs[i].tentative) {
                                iface->have_link_local = true;
                                break;
                        }
                }
+               free(addrs);
        }
 
        iface->inuse = true;
index 368e69c44b3c9315e6fe863e0854eab627b3f1ae..6b38caab339048ff142f8233549a783abab53e9b 100644 (file)
@@ -807,12 +807,6 @@ static int cb_linklocal_valid(struct nl_msg *msg, void *arg)
                return NL_SKIP;
 
        memset(&addrs[ctxt->ret], 0, sizeof(addrs[ctxt->ret]));
-
-       addrs = realloc(addrs, sizeof(*addrs)*(ctxt->ret + 1));
-       if (!addrs)
-               return NL_SKIP;
-
-
        memcpy(&addrs[ctxt->ret].addr, &addr, sizeof(addrs[ctxt->ret].addr));
 
        if (ifa->ifa_flags & IFA_F_TENTATIVE)