wahern / dns

dns.c: Single file non-blocking DNS C library without callbacks or external dependencies.
http://25thandclement.com/~william/projects/dns.c.html
MIT License
256 stars 64 forks source link

Memory leak #17

Open sustrik opened 7 years ago

sustrik commented 7 years ago

Test program:

#include "dns/dns.c"

int main() {
    int rc;
    struct dns_resolv_conf *dns_conf = dns_resconf_local(&rc);
    assert(dns_conf);
    struct dns_hosts *dns_hosts = dns_hosts_local(&rc);
    assert(dns_hosts);
    struct dns_hints *dns_hints = dns_hints_local(dns_conf, &rc);
    assert(dns_hints);
    struct dns_resolver *resolver = dns_res_open(dns_conf, dns_hosts, dns_hints, NULL, dns_opts(), &rc);
    assert(resolver);
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_INET;
    struct dns_addrinfo *ai = dns_ai_open("example.org", "80", DNS_T_A, &hints, resolver, &rc);
    assert(ai);
    struct addrinfo *it = NULL;
    while(1) {
        rc = dns_ai_nextent(&it, ai);
        if(rc == EAGAIN) {
            rc = dns_res_poll(resolver, -1);
            assert(rc == 0);
            continue;
        }
        if(rc == ENOENT)
            break;
    }
    dns_ai_close(ai);
    dns_res_close(resolver);
    dns_hints_close(dns_hints);
    dns_hosts_close(dns_hosts);
    dns_resconf_close(dns_conf);
    return 0;
}

Valgrind output:

$ valgrind --leak-check=full ./test
==18871== Memcheck, a memory error detector
==18871== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==18871== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==18871== Command: ./test
==18871== 
==18871== 
==18871== HEAP SUMMARY:
==18871==     in use at exit: 64 bytes in 1 blocks
==18871==   total heap usage: 28 allocs, 27 frees, 16,838 bytes allocated
==18871== 
==18871== 64 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18871==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18871==    by 0x4130DC: dns_ai_setent (in /home/sustrik/dsock/test)
==18871==    by 0x4137A2: dns_ai_nextent (in /home/sustrik/dsock/test)
==18871==    by 0x414D6B: main (in /home/sustrik/dsock/test)
==18871== 
==18871== LEAK SUMMARY:
==18871==    definitely lost: 64 bytes in 1 blocks
==18871==    indirectly lost: 0 bytes in 0 blocks
==18871==      possibly lost: 0 bytes in 0 blocks
==18871==    still reachable: 0 bytes in 0 blocks
==18871==         suppressed: 0 bytes in 0 blocks
==18871== 
==18871== For counts of detected and suppressed errors, rerun with: -v
==18871== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
sustrik commented 7 years ago

It seems that entries returned dns_ai_nextent should be free()'d. If so, it would be nice if the documentation said so.