www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples | github

Archive Index This month's Index

Subject: Re: libssh2 master e48907e... avoid a free(NULL)

Re: libssh2 master e48907e... avoid a free(NULL)

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Sun, 10 Jan 2010 00:29:11 +0100 (CET)

On Sun, 10 Jan 2010, Mikhail Gusarov wrote:

> DS> 1 - aesthetics. I think calling the callback function to free something when
> DS> we know there's nothing to free is useless.
> OTOH, double-checking memory for NULL (one call outside of free(3) and
> another one inside) is a bit slower.

Well, it depends how often they are made. It is one check to avoid at least
two calls when the callback for free is used. Possibly a lot more logic is
avoided too. I think the fact that this might call a callback that replaces
free makes the question a little wider than just the fact that free(NULL) is

> I'd call this setup broken, as lot of code relies on the fact free(NULL) =
> NOP.

You're entitled that opinion. I won't try to push that setup onto anyone, but
it has helped me in the past and I've used it for many years. I don't use it
on random code.

And for the record, this is the first time I noticed a free(NULL) in the
libssh2 code, and I've been checking them like this since I joined the

  / daniel.haxx.se
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-01-10

the libssh2 team