Clean up a number of memory leaks, as revealed by clang's sanitiser

This commit is contained in:
Jeremy Lakeman 2017-09-11 14:09:05 +09:30
parent 6853f9a3e6
commit e158a38137
16 changed files with 69 additions and 16 deletions

View File

@ -2046,29 +2046,43 @@ int keyring_load_from_dump(keyring_file *k, unsigned entry_pinc, const char **en
line[--linelen] = '\0';
if (linelen && line[linelen - 1] == '\r')
line[--linelen] = '\0';
} else
} else {
if (id)
keyring_free_identity(id);
return WHY("line too long");
}
unsigned idn;
unsigned ktype;
int i, j;
int n = sscanf(line, "%u: type=%u (%n%*[^)]%n)", &idn, &ktype, &i, &j);
if (n == EOF && (ferror(input) || feof(input)))
break;
if (n != 2)
if (n != 2){
if (id)
keyring_free_identity(id);
return WHYF("malformed input n=%u", n);
if (ktype == 0)
}
if (ktype == 0){
if (id)
keyring_free_identity(id);
return WHY("invalid input: ktype=0");
}
const char *ktypestr = &line[i];
line[j] = '\0';
const char *content = &line[j + 1];
//DEBUGF(keyring, "n=%d i=%u ktypestr=%s j=%u content=%s", n, i, alloca_str_toprint(ktypestr), j, alloca_str_toprint(content));
keypair *kp = keyring_alloc_keypair(ktype, 0);
if (kp == NULL)
if (kp == NULL){
if (id)
keyring_free_identity(id);
return -1;
}
int (*loader)(keypair *, const char *) = load_unknown;
if (strcmp(ktypestr, "unknown") != 0 && ktype < NELS(keytypes))
loader = keytypes[ktype].loader;
if (loader(kp, content) == -1) {
if (id)
keyring_free_identity(id);
keyring_free_keypair(kp);
return -1;
}

View File

@ -755,12 +755,14 @@ enum meshms_status meshms_message_iterator_open(struct meshms_message_iterator *
c = c->_next;
}
rhizome_manifest_free(m);
meshms_free_conversations(conv);
return MESHMS_STATUS_OK;
error:
status = MESHMS_STATUS_ERROR;
fail:
rhizome_manifest_free(m);
meshms_message_iterator_close(iter);
meshms_free_conversations(conv);
return status;

View File

@ -287,6 +287,9 @@ static int app_trace(const struct cli_parsed *parsed, struct cli_context *contex
struct mdp_header mdp_header;
bzero(&mdp_header, sizeof mdp_header);
uint8_t payload[MDP_MTU];
struct overlay_buffer *b = ob_static(payload, sizeof payload);
mdp_header.local.sid = BIND_PRIMARY;
if (mdp_bind(mdp_sockfd, &mdp_header.local))
@ -304,8 +307,6 @@ static int app_trace(const struct cli_parsed *parsed, struct cli_context *contex
cli_delim(context, "\n");
cli_flush(context);
time_ms_t end_time = gettime_ms() + timeout_ms;
uint8_t payload[MDP_MTU];
struct overlay_buffer *b = ob_static(payload, sizeof payload);
while(1){
ob_clear(b);
@ -354,6 +355,8 @@ static int app_trace(const struct cli_parsed *parsed, struct cli_context *contex
end:
if (b)
ob_free(b);
mdp_close(mdp_sockfd);
return ret;
}

View File

@ -447,8 +447,10 @@ int send_please_explain(struct decode_context *context, struct subscriber *sourc
frame_add_destination(frame, NULL, context->interface->destination);
struct network_destination *dest = create_unicast_destination(&context->addr, context->interface);
if (dest)
if (dest){
frame_add_destination(frame, NULL, dest);
release_destination_ref(dest);
}
}else{
FATAL("This context doesn't have an interface?");

View File

@ -90,6 +90,9 @@ void overlay_interface_close(overlay_interface *interface)
unschedule(&interface->alarm);
if (interface->radio_link_state)
radio_link_free(interface);
set_destination_ref(&interface->destination, NULL);
interface->state=INTERFACE_STATE_DOWN;
INFOF("Interface %s addr %s is down",

View File

@ -874,8 +874,8 @@ int _overlay_send_frame(struct __sourceloc whence, struct internal_mdp_header *h
/* crypted and signed (using CryptoBox authcryption primitive) */
frame->payload = encrypt_payload(frame->source, frame->destination, ob_ptr(plaintext), ob_position(plaintext));
ob_free(plaintext);
if (!frame->payload){
ob_free(plaintext);
op_free(frame);
return -1;
}

View File

@ -62,7 +62,7 @@ static int overlay_calc_queue_time(struct overlay_frame *frame);
int overlay_queue_init(){
/* Set default congestion levels for queues */
int i;
unsigned i;
for(i=0;i<OQ_MAX;i++) {
overlay_tx[i].maxLength=100;
overlay_tx[i].latencyTarget=0; // no QOS time limit by default, depend on per destination timeouts
@ -103,6 +103,14 @@ overlay_queue_remove(overlay_txqueue *queue, struct overlay_frame *frame){
return next;
}
void overlay_queue_release(){
unsigned i;
for(i=0;i<OQ_MAX;i++) {
while(overlay_tx[i].first)
overlay_queue_remove(&overlay_tx[i], overlay_tx[i].first);
}
}
#if 0 // unused
static int
overlay_queue_dump(overlay_txqueue *q)

View File

@ -1193,7 +1193,7 @@ void _rhizome_manifest_free(struct __sourceloc __whence, rhizome_manifest *m)
/* Free variable and signature blocks. */
rhizome_manifest_clear(m);
free(m);
return;
}

View File

@ -80,19 +80,21 @@ static uint64_t max_rowid=0;
static int sqlite_trace_callback(unsigned UNUSED(mask), void *UNUSED(context), void * p, void * x)
{
if (sqlite_trace_func()) {
const char * expanded_sql = NULL;
const char * rendered_sql = NULL;
switch (mask) {
case SQLITE_TRACE_STMT:
if (!sqlite_trace_done) {
sqlite3_stmt * stmt = p;
const char * unexpanded_sql = x;
rendered_sql = (unexpanded_sql[0] == '-' && unexpanded_sql[1] == '-') ? &unexpanded_sql[2] : sqlite3_expanded_sql(stmt);
rendered_sql = (unexpanded_sql[0] == '-' && unexpanded_sql[1] == '-') ?
&unexpanded_sql[2] : (expanded_sql = sqlite3_expanded_sql(stmt));
}
break;
case SQLITE_TRACE_PROFILE:
if (!sqlite_trace_done) {
sqlite3_stmt * stmt = p;
rendered_sql = sqlite3_expanded_sql(stmt);
rendered_sql = (expanded_sql = sqlite3_expanded_sql(stmt));
}
break;
}
@ -100,6 +102,8 @@ static int sqlite_trace_callback(unsigned UNUSED(mask), void *UNUSED(context), v
logMessage(LOG_LEVEL_DEBUG, sqlite_trace_whence ? *sqlite_trace_whence : __HERE__, "%s", rendered_sql);
++sqlite_trace_done;
}
if (expanded_sql)
sqlite3_free((void*)expanded_sql);
}
return 0; // ignored by SQLite
}

View File

@ -550,8 +550,10 @@ int rhizome_direct_bundle_iterator_fill(rhizome_direct_bundle_cursor *c,int max_
void rhizome_direct_bundle_iterator_free(rhizome_direct_bundle_cursor **c)
{
free((*c)->buffer); (*c)->buffer=NULL;
free((*c)->buffer);
(*c)->buffer=NULL;
bzero(*c,sizeof(rhizome_direct_bundle_cursor));
free(*c);
*c=NULL;
}

View File

@ -55,12 +55,16 @@ static int rhizome_sync_with_peers(int mode, int peer_count, const struct config
int peer_number;
for (peer_number = 0; peer_number < peer_count; ++peer_number) {
const struct config_rhizome_peer *peer = peers[peer_number];
if (strcasecmp(peer->protocol, "http") != 0)
if (strcasecmp(peer->protocol, "http") != 0){
free(state);
return WHYF("Unsupported Rhizome Direct protocol %s", alloca_str_toprint(peer->protocol));
}
strbuf h = strbuf_local(state->host, sizeof state->host);
strbuf_puts(h, peer->host);
if (strbuf_overrun(h))
if (strbuf_overrun(h)){
free(state);
return WHYF("Rhizome Direct host name too long: %s", alloca_str_toprint(peer->host));
}
state->port = peer->port;
DEBUGF(rhizome_direct, "Rhizome direct peer is %s://%s:%d", peer->protocol, state->host, state->port);
rhizome_direct_sync_request *s = rhizome_direct_new_sync_request(rhizome_direct_http_dispatch, 65536, 0, mode, state);
@ -69,6 +73,7 @@ static int rhizome_sync_with_peers(int mode, int peer_count, const struct config
while (fd_poll() && rd_sync_handle_count > 0)
;
}
free(state);
return 0;
}

View File

@ -621,6 +621,7 @@ void sync_send_keys(struct sched_ent *alarm)
header.qos = OQ_OPPORTUNISTIC;
header.ttl = 1;
overlay_send_frame(&header, payload);
ob_free(payload);
time_ms_t now = gettime_ms();

View File

@ -261,6 +261,7 @@ int overlay_send_stun_request(struct subscriber *server, struct subscriber *requ
void rhizome_check_connections(struct sched_ent *alarm);
int overlay_queue_init();
void overlay_queue_release();
void monitor_client_poll(struct sched_ent *alarm);
void monitor_poll(struct sched_ent *alarm);

View File

@ -671,6 +671,7 @@ static void serverCleanUp()
{
assert(serverMode != SERVER_NOT_RUNNING);
INFOF("Server cleaning up");
overlay_queue_release();
rhizome_close_db();
dna_helper_shutdown();
overlay_interface_close_all();

View File

@ -599,6 +599,13 @@ int main()
DEBUGF(verbose, "Closing peer proxy socket, TX %d RX %d", p->tx_count, p->rx_count);
unwatch(&p->alarm);
socket_unlink_close(p->alarm.poll.fd);
while(p->_head){
struct packet *packet = p->_head;
p->_head = packet->_next;
free(packet);
}
struct peer *f = p;
p=p->_next;
free(f);

View File

@ -1761,6 +1761,7 @@ tfw_quietly() {
execute() {
$_tfw_assert_noise && tfw_log "# execute" $(shellarg "$@")
_tfw_getopts execute "$@"
_tfw_dump_on_fail --stderr
shift $_tfw_getopts_shift
_tfw_execute "$@"
}
@ -1769,7 +1770,6 @@ executeOk() {
$_tfw_assert_noise && tfw_log "# executeOk" $(shellarg "$@")
_tfw_getopts executeok "$@"
_tfw_opt_exit_status=0
_tfw_dump_on_fail --stderr
shift $_tfw_getopts_shift
_tfw_execute "$@"
}