]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Fix valgrind warnings about reading uninitialized bytes.
[meshlink] / src / meshlink.c
index 4ff26ac16f8847fd3b3ad0522b0d274ef78ec4ce..1de54e916dab53abaa5f85a3db8739783fa3d3f1 100644 (file)
@@ -1127,6 +1127,7 @@ bool meshlink_start(meshlink_handle_t *mesh) {
                logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno));
                memset(&mesh->thread, 0, sizeof(mesh)->thread);
                meshlink_errno = MESHLINK_EINTERNAL;
+               event_loop_stop(&mesh->loop);
                pthread_mutex_unlock(&(mesh->mesh_mutex));
                return false;
        }
@@ -1173,12 +1174,14 @@ void meshlink_stop(meshlink_handle_t *mesh) {
                }
        }
 
-       // Wait for the main thread to finish
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
-       pthread_join(mesh->thread, NULL);
-       pthread_mutex_lock(&(mesh->mesh_mutex));
+       if(mesh->threadstarted) {
+               // Wait for the main thread to finish
+               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               pthread_join(mesh->thread, NULL);
+               pthread_mutex_lock(&(mesh->mesh_mutex));
 
-       mesh->threadstarted = false;
+               mesh->threadstarted = false;
+       }
 
        // Close all metaconnections
        if(mesh->connections) {
@@ -1192,10 +1195,9 @@ void meshlink_stop(meshlink_handle_t *mesh) {
 
        if(mesh->outgoings) {
                list_delete_list(mesh->outgoings);
+               mesh->outgoings = NULL;
        }
 
-       mesh->outgoings = NULL;
-
        pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
@@ -2396,6 +2398,11 @@ static void channel_accept(struct utcp_connection *utcp_connection, uint16_t por
 
 static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) {
        node_t *n = utcp->priv;
+
+       if(n->status.destroyed) {
+               return -1;
+       }
+
        meshlink_handle_t *mesh = n->mesh;
        return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? (ssize_t)len : -1;
 }