From 55e87af06f93daa497c389be8cd2acfefa70aa13 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 26 Sep 2019 22:43:45 +0200 Subject: [PATCH] Fix errors found by Clang's static analyzer. --- src/devtools.c | 4 ++-- src/discovery.c | 1 - src/meshlink.c | 24 ++++++++++++------------ src/net.c | 4 +++- src/net_setup.c | 2 +- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/devtools.c b/src/devtools.c index 629d90bd..47f18bc6 100644 --- a/src/devtools.c +++ b/src/devtools.c @@ -61,7 +61,7 @@ devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *e // if result is smaller than edges, we have to dealloc all the excess devtool_edge_t if((size_t)result_size > *nmemb) { - result = realloc(edges, result_size * sizeof(*result)); + result = xrealloc(edges, result_size * sizeof(*result)); } else { result = edges; } @@ -93,7 +93,7 @@ devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *e } // shrink result to the actual amount of memory used - result = realloc(result, n * sizeof(*result)); + result = xrealloc(result, n * sizeof(*result)); *nmemb = n; } else { *nmemb = 0; diff --git a/src/discovery.c b/src/discovery.c index 72ff8bc5..ae2cf895 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -221,7 +221,6 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI pthread_mutex_lock(&(mesh->mesh_mutex)); node_name += 1; - node_fp += 1; meshlink_node_t *node = meshlink_get_node(mesh, node_name); diff --git a/src/meshlink.c b/src/meshlink.c index 08264876..c4822cdf 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -846,6 +846,7 @@ static struct timeval idle(event_loop_t *loop, void *data) { // Get our local address(es) by simulating connecting to an Internet host. static void add_local_addresses(meshlink_handle_t *mesh) { struct sockaddr_storage sn; + sn.ss_family = AF_UNKNOWN; socklen_t sl = sizeof(sn); // IPv4 example.org @@ -1013,13 +1014,14 @@ meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const ch if(!name || !*name) { logger(NULL, MESHLINK_ERROR, "No name given!\n"); - //return NULL; - } else { //check name only if there is a name != NULL - if(!check_id(name)) { - logger(NULL, MESHLINK_ERROR, "Invalid name given!\n"); - meshlink_errno = MESHLINK_EINVAL; - return NULL; - } + meshlink_errno = MESHLINK_EINVAL; + return NULL; + }; + + if(!check_id(name)) { + logger(NULL, MESHLINK_ERROR, "Invalid name given!\n"); + meshlink_errno = MESHLINK_EINVAL; + return NULL; } if(devclass < 0 || devclass >= DEV_CLASS_COUNT) { @@ -2192,10 +2194,10 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { meshlink_errno = MESHLINK_ESTORAGE; free_node(mesh->self); mesh->self = NULL; + goto done; } else if(!setup_network(mesh)) { meshlink_errno = MESHLINK_ENETWORK; - } else { - rval = true; + goto done; } /* Rebuild our own list of recent addresses */ @@ -2205,9 +2207,7 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { /* Write meshlink.conf with the updated port number */ write_main_config_files(mesh); - if(!config_sync(mesh, "current")) { - return false; - } + rval = config_sync(mesh, "current"); done: pthread_mutex_unlock(&(mesh->mesh_mutex)); diff --git a/src/net.c b/src/net.c index b5705a47..9dd42dd5 100644 --- a/src/net.c +++ b/src/net.c @@ -40,6 +40,7 @@ static inline int min(int a, int b) { #endif static const int default_timeout = 5; +static const int default_interval = 60; /* Terminate a connection: @@ -113,6 +114,7 @@ static void timeout_handler(event_loop_t *loop, void *data) { for list_each(connection_t, c, mesh->connections) { int pingtimeout = c->node ? mesh->dev_class_traits[c->node->devclass].pingtimeout : default_timeout; + int pinginterval = c->node ? mesh->dev_class_traits[c->node->devclass].pinginterval : default_interval; // Also make sure that if outstanding key requests for the UDP counterpart of a connection has timed out, we restart it. if(c->node) { @@ -125,7 +127,7 @@ static void timeout_handler(event_loop_t *loop, void *data) { if(c->status.active) { if(c->status.pinged) { logger(mesh, MESHLINK_INFO, "%s didn't respond to PING in %ld seconds", c->name, (long)mesh->loop.now.tv_sec - c->last_ping_time); - } else if(c->last_ping_time + mesh->dev_class_traits[c->node->devclass].pinginterval <= mesh->loop.now.tv_sec) { + } else if(c->last_ping_time + pinginterval <= mesh->loop.now.tv_sec) { send_ping(mesh, c); continue; } else { diff --git a/src/net_setup.c b/src/net_setup.c index fc6b3c97..e7955032 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -53,7 +53,7 @@ static bool node_get_config(meshlink_handle_t *mesh, node_t *n, config_t *config const char *name; uint32_t len = packmsg_get_str_raw(in, &name); - if(len != strlen(n->name) || strncmp(name, n->name, len)) { + if(len != strlen(n->name) || !name || strncmp(name, n->name, len)) { config_free(config); return false; } -- 2.39.5