Fix various strict aliasing problems with sockaddr

Fix various strict aliasing problems caused by casting between (struct
sockaddr *) and (struct sockaddr_storage *): the correct code here is to
keep the two in a union.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=791622
This commit is contained in:
Philip Withnall
2017-12-21 17:46:24 +00:00
parent 8f7cc8cb75
commit d8fe926ba4
3 changed files with 47 additions and 26 deletions

View File

@@ -2167,12 +2167,15 @@ g_log_writer_is_journald (gint output_fd)
if (g_once_init_enter (&initialized))
{
struct sockaddr_storage addr;
union {
struct sockaddr_storage storage;
struct sockaddr sa;
struct sockaddr_un un;
} addr;
socklen_t addr_len = sizeof(addr);
int err = getpeername (output_fd, (struct sockaddr *) &addr, &addr_len);
if (err == 0 && addr.ss_family == AF_UNIX)
fd_is_journal = g_str_has_prefix (((struct sockaddr_un *)&addr)->sun_path,
"/run/systemd/journal/");
int err = getpeername (output_fd, &addr.sa, &addr_len);
if (err == 0 && addr.storage.ss_family == AF_UNIX)
fd_is_journal = g_str_has_prefix (addr.un.sun_path, "/run/systemd/journal/");
g_once_init_leave (&initialized, TRUE);
}