From: Guus Sliepen Date: Sun, 15 Nov 2015 16:18:37 +0000 (+0100) Subject: Fix resource leaks found by cppcheck. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=d90774f9eebb54dfb509b7005665a524cd0c3470;p=meshlink Fix resource leaks found by cppcheck. --- diff --git a/src/conf.c b/src/conf.c index 8acb64b9..ef595efc 100644 --- a/src/conf.c +++ b/src/conf.c @@ -333,30 +333,26 @@ bool write_config_file(const struct splay_tree_t *config_tree, const char *fname for splay_each(config_t, cnf, config_tree) { - if(fwrite(cnf->variable, sizeof(char), strlen(cnf->variable), fp) < strlen(cnf->variable)) { - logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); - return false; - } + if(fwrite(cnf->variable, sizeof(char), strlen(cnf->variable), fp) < strlen(cnf->variable)) + goto error; - if(fwrite(" = ", sizeof(char), 3, fp) < 3) { - logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); - return false; - } + if(fwrite(" = ", sizeof(char), 3, fp) < 3) + goto error; - if(fwrite(cnf->value, sizeof(char), strlen(cnf->value), fp) < strlen(cnf->value)) { - logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); - return false; - } + if(fwrite(cnf->value, sizeof(char), strlen(cnf->value), fp) < strlen(cnf->value)) + goto error; - if(fwrite("\n", sizeof(char), 1, fp) < 1) { - logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); - return false; - } + if(fwrite("\n", sizeof(char), 1, fp) < 1) + goto error; } fclose(fp); - return true; + +error: + logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno)); + fclose(fp); + return false; } bool read_server_config(meshlink_handle_t *mesh) { diff --git a/src/ed25519/seed.c b/src/ed25519/seed.c index dda3ccb2..15f41c0a 100644 --- a/src/ed25519/seed.c +++ b/src/ed25519/seed.c @@ -30,8 +30,10 @@ int ed25519_create_seed(unsigned char *seed) { return 1; } - if(fread(seed, 32, 1, f) != 1) + if(fread(seed, 32, 1, f) != 1) { + fclose(f); return 1; + } fclose(f); #endif diff --git a/src/meshlink.c b/src/meshlink.c index 446bc522..68286d29 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -2042,7 +2042,6 @@ meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink *nmemb = result_size; } else { *nmemb = 0; - free(result); meshlink_errno = MESHLINK_ENOMEM; }