From: Ben Pfaff Date: Thu, 2 Oct 2008 21:31:29 +0000 (-0700) Subject: New function get_unix_name_len() to simplify code. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e5e638ede4a8d7f813558479f9ed3f59d93876b;p=openvswitch New function get_unix_name_len() to simplify code. --- diff --git a/include/socket-util.h b/include/socket-util.h index fec4565c..fe9c154e 100644 --- a/include/socket-util.h +++ b/include/socket-util.h @@ -45,5 +45,6 @@ int check_connection_completion(int fd); int drain_rcvbuf(int fd); int make_unix_socket(int style, bool nonblock, bool passcred, const char *bind_path, const char *connect_path); +int get_unix_name_len(socklen_t sun_len); #endif /* socket-util.h */ diff --git a/lib/socket-util.c b/lib/socket-util.c index 8fa663ad..ca08de5a 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -254,3 +254,11 @@ error: close(fd); return -error; } + +int +get_unix_name_len(socklen_t sun_len) +{ + return (sun_len >= offsetof(struct sockaddr_un, sun_path) + ? sun_len - offsetof(struct sockaddr_un, sun_path) + : 0); +} diff --git a/lib/vconn-unix.c b/lib/vconn-unix.c index 9b08b10e..f296ad7f 100644 --- a/lib/vconn-unix.c +++ b/lib/vconn-unix.c @@ -114,12 +114,11 @@ punix_accept(int fd, const struct sockaddr *sa, size_t sa_len, struct vconn **vconnp) { const struct sockaddr_un *sun = (const struct sockaddr_un *) sa; + int name_len = get_unix_name_len(sa_len); char name[128]; - if (sa_len >= offsetof(struct sockaddr_un, sun_path)) { - snprintf(name, sizeof name, "unix:%.*s", - (int) (sa_len - offsetof(struct sockaddr_un, sun_path)), - sun->sun_path); + if (name_len > 0) { + snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path); } else { strcpy(name, "unix"); }