]> git.ipfire.org Git - thirdparty/tvheadend.git/commit
Avoid breaking strict aliasing in IP_AS_V{4,6}
authorDavid Kalnischkies <david@kalnischkies.de>
Wed, 23 Nov 2022 11:11:26 +0000 (12:11 +0100)
committerFlole998 <Flole998@users.noreply.github.com>
Thu, 24 Nov 2022 11:55:34 +0000 (12:55 +0100)
commit7b95ba4cf9113ae8808b3e4a9425010b607dbaca
tree54bdf04ff9987f60ed7bd93e9dbe45973e2c6891
parent5543ce518faaeeb0677fd7c2fca26f8ae0d265d3
Avoid breaking strict aliasing in IP_AS_V{4,6}

GCC complains (one example, more in tcp.h):

In file included from src/main.c:41:
src/tcp.h: In function ‘ip_check_equal_v4’:
src/tcp.h:29:31: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
   29 | #define IP_AS_V4(storage, f) ((struct sockaddr_in *)&(storage))->sin_##f
      |                              ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/tcp.h:67:14: note: in expansion of macro ‘IP_AS_V4’
   67 |     { return IP_AS_V4(a, addr).s_addr == IP_AS_V4(b, addr).s_addr; }
      |              ^~~~~~~~

storage (a) here is a pointer to sockaddr_storage which is either backed
by a sockaddr_in or sockaddr_in6 struct (here, it would be good if it is
the former, we decided based on sa_family in ip_check_equal). Referencing
it means we have a pointer to a pointer to sockaddr_storage here, which
we then cast to a pointer to sockaddr_in. Our so type-punned pointer is
then dereferenced breaking strict-aliasing as this pointer as well as
our original pointer share a memory location and both could be used to
change it even through they are of different types.

We can avoid this situation simply by removing the reference as then it
is just casting a pointer to a different type (which in this case is
legal as storage is really a sockaddr_in).

Removing the reference breaks users of the macro who do not feed it a
pointer to a sockaddr_storage, so while the warnings were all produced
by tcp.h, we end up changing code everywhere else to resolve them –
usually by just taking a reference or not dereferencing there.

As this is the only instance of (detected) strict-aliasing breakage
the Makefile is also changed to no longer build the entirety of
tvheadend with strict-aliasing rules disabled. Similar for the disabled
stringop warnings which weren't (correctly) triggered in the current
code.

As a small bonus, this also prevents gcc-11 from tripping with a false
positive over the previous tcp change in c0f616e / #1473.
Makefile
src/tcp.c
src/tcp.h
src/tvhlog.c
src/udp.c
src/upnp.c