From 0e098b15c76e222f7de381203c0c35a75a5b2f24 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 7 Oct 2010 02:34:17 +0200 Subject: util: never use sizeof(sa_family_t) when calculating sockaddr sizes --- src/sd-daemon.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/sd-daemon.c') diff --git a/src/sd-daemon.c b/src/sd-daemon.c index 9c23b917f9..e12fb0483a 100644 --- a/src/sd-daemon.c +++ b/src/sd-daemon.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "sd-daemon.h" @@ -227,7 +228,7 @@ int sd_is_socket(int fd, int family, int type, int listening) { if (getsockname(fd, &sockaddr.sa, &l) < 0) return -errno; - if (l < sizeof(sa_family_t)) + if (l < offsetof(struct sockaddr_un, sun_path)) return -EINVAL; return sockaddr.sa.sa_family == family; @@ -253,7 +254,7 @@ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port if (getsockname(fd, &sockaddr.sa, &l) < 0) return -errno; - if (l < sizeof(sa_family_t)) + if (l < offsetof(struct sockaddr_un, sun_path)) return -EINVAL; if (sockaddr.sa.sa_family != AF_INET && @@ -295,7 +296,7 @@ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t if (getsockname(fd, &sockaddr.sa, &l) < 0) return -errno; - if (l < sizeof(sa_family_t)) + if (l < offsetof(struct sockaddr_un, sun_path)) return -EINVAL; if (sockaddr.sa.sa_family != AF_UNIX) @@ -307,17 +308,17 @@ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t if (length <= 0) /* Unnamed socket */ - return l == sizeof(sa_family_t); + return l == offsetof(struct sockaddr_un, sun_path); if (path[0]) /* Normal path socket */ return - (l >= sizeof(sa_family_t) + length + 1) && + (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) && memcmp(path, sockaddr.un.sun_path, length+1) == 0; else /* Abstract namespace socket */ return - (l == sizeof(sa_family_t) + length) && + (l == offsetof(struct sockaddr_un, sun_path) + length) && memcmp(path, sockaddr.un.sun_path, length) == 0; } @@ -366,7 +367,7 @@ int sd_notify(int unset_environment, const char *state) { memset(&msghdr, 0, sizeof(msghdr)); msghdr.msg_name = &sockaddr; - msghdr.msg_namelen = sizeof(sa_family_t) + strlen(e); + msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e); if (msghdr.msg_namelen > sizeof(struct sockaddr_un)) msghdr.msg_namelen = sizeof(struct sockaddr_un); -- cgit v1.2.3-54-g00ecf