Is free(NULL) OK?

James Browning jamesb192 at jamesb192.com
Sun Jan 12 11:18:42 UTC 2025


On Saturday, January 11, 2025 10:43:47 PM Pacific Standard Time Hal Murray via 
devel wrote:
> The Linux man page says
>        If ptr is NULL, no operation is performed.
> 
> I'd expect a wrapper if there really was a problem.
> 
> This code is in ntpd/ntp_config.c
>         if (NULL == s) {                        /* free() hates NULL */
>                 s = estrdup("");
>         }
> 
> Any reason not to nuke that chunk of code?

Have we looked beyond the sound bite? A bush league check by this armchair 
asshole suggest that a NULL that slips through here can go into estrdup, 
strlen, get_logmask, stats_config, or other places I haven't found yet. Just be 
aware that sucker is a stopper; it would be prudent to check everything 
downstream.

Ah, it looks like estrdup and strlen at least would react poorly[1] to being 
fed a null from the wrapping code. huh.

[1] https://stackoverflow.com/questions/5796103/strlen-not-checking-for-null




More information about the devel mailing list