NLnetLabs / ldns

LDNS is a DNS library that facilitates DNS tool programming

Home Page:https://nlnetlabs.nl/ldns

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak in _ldns_rr_new_frm_fp_l_internal

bbczeuz opened this issue · comments

Valgrind complains about a leak parsing a zone file:

==946908== 10,231 bytes in 1 blocks are definitely lost in loss record 2 of 2
==946908==    at 0x484486F: malloc (vg_replace_malloc.c:381)
==946908==    by 0x4913FAD: ldns_fget_token_l_st (parse.c:58)
==946908==    by 0x492829F: _ldns_rr_new_frm_fp_l_internal (rr.c:737)
==946908==    by 0x492EC14: ldns_zone_new_frm_fp_l (zone.c:243)
==946908==    by 0x40CA5E: RrList::load_file(char const*) (ldns_resolver_internals.cpp:297)
RrList::load_file(char const*p_filename)
{
    FILE *fp = fopen(p_filename, "r");
    if (fp == nullptr)
    {
            throw [...]
    }

    ldns_zone *zone = nullptr;
    int line_nr = 0;
    auto status = ldns_zone_new_frm_fp_l(&zone, fp, nullptr, 0, LDNS_RR_CLASS_ANY, &line_nr);
    fclose(fp);
[...]
     ldns_zone_deep_free(zone);
[...]

Function ldns_zone_new_frm_fp_l internally parses the file line by line (Line 737 on tag 1.8.1 or line 745 on develop branch):

        /* read an entire line in from the file */
        if ((s = ldns_fget_token_l_st( fp, &line, &limit, false
                                     , LDNS_PARSE_SKIP_SPACE, line_nr)))
        {
                return s;
        }

When returning here, the ~10kB malloced at &line are lost. I'll prepare a patch freeing the mem at &line