fixed issues identified by coverity
Costa Tsaousis (ktsaou) committed
Sep 6, 2017 at 01:20 UTC
e5cd44c1c9f6bea724a3944b40e535579f4b063c
1 file changed
+12
-3
src/socket.c
+12
-3
@@ -99,8 +99,12 @@ int create_listen_socket_unix(const char *path, int listen_backlog) {
99
name.sun_family = AF_UNIX;
100
strncpy(name.sun_path, path, sizeof(name.sun_path)-1);
101
102
- // we have to delete any old socket, or bind() will fail
103
- unlink(path);
102
+ struct stat stbuf;
103
+ if(stat(path, &stbuf) == 0) {
104
+ // we have to delete any old socket, or bind() will fail
105
+ if (unlink(path) == -1)
106
+ error("LISTENER: failed to remove existing (probably obsolete or left-over) file on UNIX socket path '%s'.", path);
107
+ }
108
109
if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
110
close(sock);
@@ -110,7 +114,8 @@ int create_listen_socket_unix(const char *path, int listen_backlog) {
114
115
// we have to chmod this to 0777 so that the client will be able
116
// to read from and write to this socket.
113
- chmod(path, 0777);
117
+ if(chmod(path, 0777) == -1)
118
+ error("LISTENER: failed to chmod() socket file '%s'.", path);
119
120
if(listen(sock, listen_backlog) < 0) {
121
close(sock);
@@ -485,6 +490,10 @@ int listen_sockets_setup(LISTEN_SOCKETS *sockets) {
490
491
static inline int connect_to_unix(const char *path, struct timeval *timeout) {
492
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
493
+ if(fd == -1) {
494
+ error("Failed to create UNIX socket() for '%s'", path);
495
+ return -1;
496
+ }
497
498
if(timeout) {
499
if(setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *) timeout, sizeof(struct timeval)) < 0)