BUILD: tree-wide: avoid warnings caused by redundant checks of obj_types
authorWilly Tarreau <w@1wt.eu>
Mon, 6 Dec 2021 07:01:02 +0000 (07:01 +0000)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 16 Dec 2021 08:41:57 +0000 (09:41 +0100)
commitdcb4fad90b42b94a156e4999d2452350175ea1d5
tree8fb22664f127f9c445364c8d7a410fba660dbcbf
parent02c2cc8cbec61287af9af54b7b712fdb5f9747ca
BUILD: tree-wide: avoid warnings caused by redundant checks of obj_types

At many places we use construct such as:

   if (objt_server(blah))
       do_something(objt_server(blah));

At -O2 the compiler manages to simplify the operation and see that the
second one returns the same result as the first one. But at -O1 that's
not always the case, and the compiler is able to emit a second
expression and sees the potential null that results from it, and may
warn about a potential null deref (e.g. with gcc-6.5). There are two
solutions to this:
  - either the result of the first test has to be passed to a local
    variable
  - or the second reference ought to be unchecked using the __objt_*
    variant.

This patch fixes all occurrences at once by taking the second approach
(the least intrusive). For constructs like:

   objt_server(blah) ? objt_server(blah)->name : "no name"

a macro could be useful. It would for example take the object type
(server), the field name (name) and the default value. But there
are probably not enough occurrences across the whole code for this
to really matter.

This should be backported wherever it applies.

(cherry picked from commit 88bc800eae8d0a2118a273afc52ecdc529c9f523)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
src/backend.c
src/http_ana.c
src/proto_tcp.c
src/proto_uxst.c
src/stream.c
src/stream_interface.c