CLEANUP: tree-wide: replace free(x);x=NULL with ha_free(&x)
authorWilly Tarreau <w@1wt.eu>
Sat, 20 Feb 2021 09:46:51 +0000 (10:46 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 26 Feb 2021 20:21:09 +0000 (21:21 +0100)
commit61cfdf4fd8a93dc6fd9922d5b309a71bdc7d2853
treebd03604a606c914533ab2e6c139dbdb6cf41e38f
parentee17b97eeaf733e6ca90cd69f7c2e7713c0db6f1
CLEANUP: tree-wide: replace free(x);x=NULL with ha_free(&x)

This makes the code more readable and less prone to copy-paste errors.
In addition, it allows to place some __builtin_constant_p() predicates
to trigger a link-time error in case the compiler knows that the freed
area is constant. It will also produce compile-time error if trying to
free something that is not a regular pointer (e.g. a function).

The DEBUG_MEM_STATS macro now also defines an instance for ha_free()
so that all these calls can be checked.

178 occurrences were converted. The vast majority of them were handled
by the following Coccinelle script, some slightly refined to better deal
with "&*x" or with long lines:

  @ rule @
  expression E;
  @@
  - free(E);
  - E = NULL;
  + ha_free(&E);

It was verified that the resulting code is the same, more or less a
handful of cases where the compiler optimized slightly differently
the temporary variable that holds the copy of the pointer.

A non-negligible amount of {free(str);str=NULL;str_len=0;} are still
present in the config part (mostly header names in proxies). These
ones should also be cleaned for the same reasons, and probably be
turned into ist strings.
45 files changed:
include/haproxy/bug.h
src/51d.c
src/acl.c
src/arg.c
src/auth.c
src/cache.c
src/cfgparse-listen.c
src/cfgparse-ssl.c
src/cfgparse.c
src/check.c
src/cli.c
src/dict.c
src/dns.c
src/ev_epoll.c
src/ev_evports.c
src/ev_kqueue.c
src/ev_poll.c
src/ev_select.c
src/extcheck.c
src/fcgi-app.c
src/fd.c
src/flt_spoe.c
src/haproxy.c
src/http_fetch.c
src/http_htx.c
src/mailers.c
src/map.c
src/mworker-prog.c
src/mworker.c
src/pattern.c
src/protocol.c
src/proxy.c
src/resolvers.c
src/server.c
src/sink.c
src/sock.c
src/ssl_ckch.c
src/ssl_crtlist.c
src/ssl_sock.c
src/stats.c
src/stream.c
src/tcpcheck.c
src/tools.c
src/wurfl.c
src/xprt_quic.c