]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Fix a potential NULL-pointer dereference when generating invitations.
[meshlink] / src / meshlink.c
index 0bc947a6acae95e4eebf07a2ae6cfdfab288e35a..885bfba3be57034f3007987e8cc7336de0f9b7e2 100644 (file)
@@ -388,7 +388,7 @@ static char *get_my_hostname(meshlink_handle_t *mesh) {
                return NULL;
        }
 
-       if(!strcmp(hostname[0], hostname[1])) {
+       if(hostname[0] && hostname[1] && !strcmp(hostname[0], hostname[1])) {
                free(hostname[1]);
                hostname[1] = NULL;
        }
@@ -1211,10 +1211,18 @@ void meshlink_stop(meshlink_handle_t *mesh) {
        event_loop_stop(&mesh->loop);
 
        // Send ourselves a UDP packet to kick the event loop
-       listen_socket_t *s = &mesh->listen_socket[0];
+       for(int i = 0; i < mesh->listen_sockets; i++) {
+               sockaddr_t sa;
+               socklen_t salen = sizeof(sa.sa);
 
-       if(sendto(s->udp.fd, "", 1, MSG_NOSIGNAL, &s->sa.sa, SALEN(s->sa.sa)) == -1) {
-               logger(mesh, MESHLINK_ERROR, "Could not send a UDP packet to ourself");
+               if(getsockname(mesh->listen_socket[i].udp.fd, &sa.sa, &salen) == -1) {
+                       logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getsockname", sockstrerror(sockerrno));
+                       continue;
+               }
+
+               if(sendto(mesh->listen_socket[i].udp.fd, "", 1, MSG_NOSIGNAL, &sa.sa, salen) == -1) {
+                       logger(mesh, MESHLINK_ERROR, "Could not send a UDP packet to ourself: %s", sockstrerror(sockerrno));
+               }
        }
 
        // Wait for the main thread to finish