[Dnsmasq-discuss] Memory leak + fix

Simon Kelley simon at thekelleys.org.uk
Tue Jun 3 09:20:20 UTC 2025


Tim,

Thanks for the report.

The diagnosis looks good, and the minimal fix also.

Looking at the code, it's a bit of a dog's dinner with allocation and 
freeing very hard to follow, hence the problem.

I did a more extensive rewrite which I hope fixes the problem and the 
underlying cause.

I just pushed 2.92test11 to the git repo which includes this.

Cheers,

Simon.


On 29/05/2025 05:56, Tim Wilkinson wrote:
> We use dnsmasq as part of our AREDN platform (arednmesh.org <http:// 
> arednmesh.org>) which is in turn based on OpenWRT (openwrt.org <http:// 
> openwrt.org>). In our particular setup we watch a directory of host 
> files, sometimes thousands of them, and they change frequently. With 
> this configuration we’ve noticed a slow memory leak which looks to be 
> caused by these frequent file changes. We fixed this with the included 
> patch (see below). Please take a look. Essentially the assumption in the 
> current code is that the function dyndir_addhosts is passed a malloced 
> “path” which should not be freed unless it errors, but because this path 
> is usually found in the struct dyndir file cache this isn’t (usually) 
> true. And so the path leaks.
> 
> Thanks
> Tim - KN6PLV
> 
> --- a/src/inotify.c
> 
> +++ b/src/inotify.c
> 
> @@ -155,6 +155,11 @@
> 
>     newah->index = daemon->host_index++;
> 
>     newah->fname = path;
> 
> +  if (!(newah->fname = whine_malloc(strlen(path)+1))) {
> 
> +    free(newah);
> 
> +    return NULL;
> 
> +  }
> 
> +  strcpy(newah->fname, path);
> 
>     return newah;
> 
>   }
> 
> @@ -239,6 +244,8 @@
> 
>      option_read_dynfile(path, dd->flags);
> 
>   #endif
> 
> }
> 
> +
> 
> +      free(path);
> 
>      }
> 
> }
> 
> @@ -339,8 +346,7 @@
> 
>        option_read_dynfile(path, AH_DHCP_OPT);
> 
>   #endif
> 
> -    if (!ah)
> 
> -      free(path);
> 
> +    free(path);
> 
>    }
> 
>        }
> 
> }
> 
> 
> 
> _______________________________________________
> Dnsmasq-discuss mailing list
> Dnsmasq-discuss at lists.thekelleys.org.uk
> https://lists.thekelleys.org.uk/cgi-bin/mailman/listinfo/dnsmasq-discuss




More information about the Dnsmasq-discuss mailing list