]> granicus.if.org Git - libevent/commitdiff
dns-example: free result in getaddrinfo callback
authorBogdan Harjoc <bharjoc@bitdefender.com>
Thu, 9 Aug 2018 11:47:17 +0000 (14:47 +0300)
committerAzat Khuzhin <a3at.mail@gmail.com>
Fri, 17 Aug 2018 00:22:28 +0000 (03:22 +0300)
According to evdns.c, the result not freed by libevent after
the callback runs:

evdns_getaddrinfo_gotresolve()
{
    ...
    data->user_cb(0, data->pending_result, data->user_data);
    data->pending_result = NULL;
    ...
}

To reproduce, build with -fsanitize=address, add -g to the getopt
list in dns-example.c like in the current commit and run

  dns-example -g google.com

Closes: #681 # cherry-picked
sample/dns-example.c

index 6243458abca73a2c2a1a3d97094fb8d1b0bad22d..21a75de8635663ff992e7f9fad8e5e789609815d 100644 (file)
@@ -78,6 +78,8 @@ gai_callback(int err, struct evutil_addrinfo *ai, void *arg)
 {
        const char *name = arg;
        int i;
+       struct evutil_addrinfo *first_ai = ai;
+
        if (err) {
                printf("%s: %s\n", name, evutil_gai_strerror(err));
        }
@@ -99,6 +101,9 @@ gai_callback(int err, struct evutil_addrinfo *ai, void *arg)
                        printf("[%d] %s: %s\n",i,name,buf);
                }
        }
+
+       if (first_ai)
+               evutil_freeaddrinfo(first_ai);
 }
 
 static void
@@ -166,7 +171,7 @@ main(int c, char **v) {
                return 1;
        }
 
-       while ((opt = getopt(c, v, "xvc:Ts:")) != -1) {
+       while ((opt = getopt(c, v, "xvc:Ts:g")) != -1) {
                switch (opt) {
                        case 'x': o.reverse = 1; break;
                        case 'v': ++verbose; break;