Using asprintf without memory leaks

Florian Weimer fweimer@redhat.com
Sun Dec 6 16:55:00 GMT 2015


On 11/26/2015 11:21 PM, Ian Pilcher wrote:
> asprintf(3) says:
> 
>   When successful, these functions return the number of bytes printed,
>   just like sprintf(3).  If memory allocation wasn't possible, or some
>   other error occurs, these functions will return -1, and the contents
>   of strp is undefined.
> 
> If -1 is returned, can I at least assume that any memory that was
> allocated has been freed?

You can.  If glibc doesn't free allocated memory on error paths, it's a bug.

(Technically, there are corner cases where free cannot actually return
memory for later reuse, but they are rare.)

Florian



More information about the Libc-help mailing list