sysdeps: Reassure gcc 8 that we are not overflowing struct sockaddr_un
authorSimon McVittie <smcv@collabora.com>
Mon, 23 Jul 2018 17:52:01 +0000 (18:52 +0100)
committerSimon McVittie <smcv@collabora.com>
Thu, 2 Aug 2018 16:13:02 +0000 (17:13 +0100)
commit4937a36c310c94644a60bc9fce8bde5f511395f5
tree071d0fe97802b26769d6f1ec6b1afa34699e4bd5
parentab8ef4ae31bd5aa28dd228ceb8794877ccf9fd32
sysdeps: Reassure gcc 8 that we are not overflowing struct sockaddr_un

Using strncpy (buffer, str, strlen (str)) is a "code smell" that
might indicate a serious bug (it effectively turns strncpy into
strcpy), and gcc 8 now warns about it. In fact we avoided the bug
here, but it wasn't at all obvious.

We already checked that path_len is less than or equal to
_DBUS_MAX_SUN_PATH_LENGTH, which is 99, chosen to be strictly less
than the POSIX minimum sizeof(sun_path) >= 100, so we couldn't
actually be overflowing the available buffer.

The new static assertion in this commit matches a comment above the
definition of _DBUS_MAX_SUN_PATH_LENGTH: we define
_DBUS_MAX_SUN_PATH_LENGTH to 99, because POSIX says struct
sockaddr_un's sun_path member is at least 100 bytes (including space
for a \0 terminator). dbus will now fail to compile on
platforms that are non-POSIX-compliant in this way, except for Windows.

We zeroed the struct sockaddr_un before writing into it, so stopping
one byte short of the end of sun_path ensures that we get \0
termination.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=107350
Reviewed-by: Thiago Macieira <thiago@kde.org>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
(cherry picked from commit f429631365ba59a1749438af2184cab138a31772)
dbus/dbus-sysdeps-unix.c