From: Brent Cook <[email protected]>

These are all sort of related, so they're merged into one.

 - Nothing seems to free the result of host_dns(), so add
   host_dns_free() and call after each query.
 - If imsg_add fails, it frees buf. But, so does imsg_close. Punt for
   now and just die if imsg_add fails (maybe this should be handled more
   nicely?)
 - Regardless of whether imsg_create succeeds or fails, we should
   cleanup the results of host_dns().

ok?
---
 src/usr.sbin/ntpd/config.c  | 11 +++++++++++
 src/usr.sbin/ntpd/ntp_dns.c | 13 +++++++------
 src/usr.sbin/ntpd/ntpd.c    | 13 +++++++------
 src/usr.sbin/ntpd/ntpd.h    |  1 +
 4 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/usr.sbin/ntpd/config.c b/src/usr.sbin/ntpd/config.c
index d660750..48bdad7 100644
--- a/src/usr.sbin/ntpd/config.c
+++ b/src/usr.sbin/ntpd/config.c
@@ -111,6 +111,17 @@ host_v6(const char *s)
        return (h);
 }
 
+void
+host_dns_free(struct ntp_addr *hn)
+{
+       struct ntp_addr *h = hn, *tmp;
+       while (h) {
+               tmp = h;
+               h = h->next;
+               free(tmp);
+       }
+}
+
 int
 host_dns(const char *s, struct ntp_addr **hn)
 {
diff --git a/src/usr.sbin/ntpd/ntp_dns.c b/src/usr.sbin/ntpd/ntp_dns.c
index 7726f92..3a88a21 100644
--- a/src/usr.sbin/ntpd/ntp_dns.c
+++ b/src/usr.sbin/ntpd/ntp_dns.c
@@ -159,13 +159,14 @@ dns_dispatch_imsg(void)
                        buf = imsg_create(ibuf_dns, IMSG_HOST_DNS,
                            imsg.hdr.peerid, 0,
                            cnt * sizeof(struct sockaddr_storage));
-                       if (buf == NULL)
-                               break;
-                       if (cnt > 0)
+                       if (buf != NULL) {
                                for (h = hn; h != NULL; h = h->next)
-                                       imsg_add(buf, &h->ss, sizeof(h->ss));
-
-                       imsg_close(ibuf_dns, buf);
+                                       if (imsg_add(buf, &h->ss, 
sizeof(h->ss)) == -1)
+                                               fatalx("could not complete 
imsg");
+                               imsg_close(ibuf_dns, buf);
+                       }
+                       host_dns_free(hn);
+                       hn = NULL;
                        break;
                default:
                        break;
diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c
index cfc7e56..a5b4189 100644
--- a/src/usr.sbin/ntpd/ntpd.c
+++ b/src/usr.sbin/ntpd/ntpd.c
@@ -358,13 +358,14 @@ dispatch_imsg(struct ntpd_conf *lconf)
                        buf = imsg_create(ibuf, IMSG_HOST_DNS,
                            imsg.hdr.peerid, 0,
                            cnt * sizeof(struct sockaddr_storage));
-                       if (buf == NULL)
-                               break;
-                       if (cnt > 0)
+                       if (buf != NULL) {
                                for (h = hn; h != NULL; h = h->next)
-                                       imsg_add(buf, &h->ss, sizeof(h->ss));
-
-                       imsg_close(ibuf, buf);
+                                       if (imsg_add(buf, &h->ss, 
sizeof(h->ss)) == -1)
+                                               fatalx("could not append imsg");
+                               imsg_close(ibuf, buf);
+                       }
+                       host_dns_free(hn);
+                       hn = NULL;
                        break;
                default:
                        break;
diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h
index 464353c..8cd1e3b 100644
--- a/src/usr.sbin/ntpd/ntpd.h
+++ b/src/usr.sbin/ntpd/ntpd.h
@@ -281,6 +281,7 @@ int  parse_config(const char *, struct ntpd_conf *);
 /* config.c */
 int                     host(const char *, struct ntp_addr **);
 int                     host_dns(const char *, struct ntp_addr **);
+void                    host_dns_free(struct ntp_addr *);
 struct ntp_peer                *new_peer(void);
 struct ntp_conf_sensor *new_sensor(char *);
 
-- 
1.9.1

Reply via email to