From 8227f70cd70f497cb03c1f9aab63950bcd979d8b Mon Sep 17 00:00:00 2001 From: Jonathan Rose Date: Thu, 10 May 2012 18:35:14 +0000 Subject: [PATCH] Coverity Report: Fix issues for error type CHECKED_RETURN for core (issue ASTERISK-19658) Reported by: Matt Jordan Review: https://reviewboard.asterisk.org/r/1905/ ........ Merged revisions 366094 from http://svn.asterisk.org/svn/asterisk/branches/1.8 ........ Merged revisions 366106 from http://svn.asterisk.org/svn/asterisk/branches/10 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@366126 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- apps/app_queue.c | 12 +++++++++--- apps/app_voicemail.c | 15 ++++++++++++--- channels/chan_iax2.c | 12 +++++++++--- channels/chan_sip.c | 10 ++++++++-- channels/iax2-provision.c | 6 +++++- channels/sig_analog.c | 5 ++++- funcs/func_devstate.c | 1 + funcs/func_lock.c | 17 +++++++++++++---- main/acl.c | 6 +++++- main/asterisk.c | 9 ++++++--- main/features.c | 4 +++- main/pbx.c | 4 +++- main/xmldoc.c | 6 +++++- res/ael/ael.flex | 8 ++++++-- 14 files changed, 89 insertions(+), 26 deletions(-) diff --git a/apps/app_queue.c b/apps/app_queue.c index e6b997021b..7db08193ca 100644 --- a/apps/app_queue.c +++ b/apps/app_queue.c @@ -4946,7 +4946,9 @@ static int try_calling(struct queue_ent *qe, const struct ast_flags opts, char * res2 |= ast_safe_sleep(peer, qe->parent->memberdelay * 1000); } if (!res2 && announce) { - play_file(peer, announce); + if (play_file(peer, announce) < 0) { + ast_log(LOG_ERROR, "play_file failed for '%s' on %s\n", announce, ast_channel_name(peer)); + } } if (!res2 && qe->parent->reportholdtime) { if (!play_file(peer, qe->parent->sound_reporthold)) { @@ -4957,11 +4959,15 @@ static int try_calling(struct queue_ent *qe, const struct ast_flags opts, char * holdtimesecs = abs((now - qe->start) % 60); if (holdtime > 0) { ast_say_number(peer, holdtime, AST_DIGIT_ANY, ast_channel_language(peer), NULL); - play_file(peer, qe->parent->sound_minutes); + if (play_file(peer, qe->parent->sound_minutes) < 0) { + ast_log(LOG_ERROR, "play_file failed for '%s' on %s\n", qe->parent->sound_minutes, ast_channel_name(peer)); + } } if (holdtimesecs > 1) { ast_say_number(peer, holdtimesecs, AST_DIGIT_ANY, ast_channel_language(peer), NULL); - play_file(peer, qe->parent->sound_seconds); + if (play_file(peer, qe->parent->sound_seconds) < 0) { + ast_log(LOG_ERROR, "play_file failed for '%s' on %s\n", qe->parent->sound_seconds, ast_channel_name(peer)); + } } } } diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c index 55aec5d22e..d4da42b03b 100644 --- a/apps/app_voicemail.c +++ b/apps/app_voicemail.c @@ -6983,7 +6983,10 @@ static int vm_forwardoptions(struct ast_channel *chan, struct ast_vm_user *vmu, strncat(vms->introfn, "intro", sizeof(vms->introfn)); ast_play_and_wait(chan, INTRO); ast_play_and_wait(chan, "beep"); - play_record_review(chan, NULL, vms->introfn, vmu->maxsecs, vm_fmts, 1, vmu, (int *) duration, NULL, NULL, record_gain, vms, flag); + cmd = play_record_review(chan, NULL, vms->introfn, vmu->maxsecs, vm_fmts, 1, vmu, (int *) duration, NULL, NULL, record_gain, vms, flag); + if (cmd == -1) { + break; + } cmd = 't'; #else @@ -9561,7 +9564,10 @@ static int vm_tempgreeting(struct ast_channel *chan, struct ast_vm_user *vmu, st retries = 0; RETRIEVE(prefile, -1, vmu->mailbox, vmu->context); if (ast_fileexists(prefile, NULL, NULL) <= 0) { - play_record_review(chan, "vm-rec-temp", prefile, maxgreet, fmtc, 0, vmu, &duration, NULL, NULL, record_gain, vms, NULL); + cmd = play_record_review(chan, "vm-rec-temp", prefile, maxgreet, fmtc, 0, vmu, &duration, NULL, NULL, record_gain, vms, NULL); + if (cmd == -1) { + break; + } cmd = 't'; } else { switch (cmd) { @@ -11762,6 +11768,7 @@ static void mwi_sub_event_cb(const struct ast_event *event, void *userdata) static void start_poll_thread(void) { + int errcode; mwi_sub_sub = ast_event_subscribe(AST_EVENT_SUB, mwi_sub_event_cb, "Voicemail MWI subscription", NULL, AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_MWI, AST_EVENT_IE_END); @@ -11775,7 +11782,9 @@ static void start_poll_thread(void) poll_thread_run = 1; - ast_pthread_create(&poll_thread, NULL, mb_poll_thread, NULL); + if ((errcode = ast_pthread_create(&poll_thread, NULL, mb_poll_thread, NULL))) { + ast_log(LOG_ERROR, "Could not create thread: %s\n", strerror(errcode)); + } } static void stop_poll_thread(void) diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index baee946f2a..8903041db2 100644 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -4382,11 +4382,13 @@ static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in /* Whoops, we weren't supposed to exist! */ peer = peer_unref(peer); break; - } + } } else if (!strcasecmp(tmp->name, "regseconds")) { ast_get_time_t(tmp->value, ®seconds, 0, NULL); } else if (!strcasecmp(tmp->name, "ipaddr")) { - ast_sockaddr_parse(&peer->addr, tmp->value, PARSE_PORT_IGNORE); + if (!ast_sockaddr_parse(&peer->addr, tmp->value, PARSE_PORT_IGNORE)) { + ast_log(LOG_WARNING, "Failed to parse sockaddr '%s' for ipaddr of realtime peer '%s'\n", tmp->value, tmp->name); + } } else if (!strcasecmp(tmp->name, "port")) { ast_sockaddr_set_port(&peer->addr, atoi(tmp->value)); } else if (!strcasecmp(tmp->name, "host")) { @@ -11653,6 +11655,7 @@ static void iax2_process_thread_cleanup(void *data) ast_mutex_destroy(&thread->init_lock); ast_cond_destroy(&thread->init_cond); ast_free(thread); + /* Ignore check_return warning from Coverity for ast_atomic_dec_and_test below */ ast_atomic_dec_and_test(&iaxactivethreadcount); } @@ -12234,7 +12237,10 @@ static int start_network_thread(void) AST_LIST_UNLOCK(&idle_list); } } - ast_pthread_create_background(&netthreadid, NULL, network_thread, NULL); + if (ast_pthread_create_background(&netthreadid, NULL, network_thread, NULL)) { + ast_log(LOG_ERROR, "Failed to create new thread!\n"); + return -1; + } ast_verb(2, "%d helper threads started\n", threadcount); return 0; } diff --git a/channels/chan_sip.c b/channels/chan_sip.c index c7c6a38ca7..de2c80e55a 100644 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -16384,7 +16384,9 @@ static void check_via(struct sip_pvt *p, struct sip_request *req) p->sa = p->recv; } - ast_sockaddr_resolve_first(&tmp, c, 0); + if (ast_sockaddr_resolve_first(&tmp, c, 0)) { + ast_log(LOG_WARNING, "Could not resolve socket address for '%s'\n", c); + } port = ast_sockaddr_port(&tmp); ast_sockaddr_set_port(&p->sa, port != 0 ? port : STANDARD_SIP_PORT); @@ -16733,6 +16735,7 @@ static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_requ /* Finally, apply the guest policy */ if (sip_cfg.allowguest) { + /* Ignore check_return warning from Coverity for get_rpid below. */ get_rpid(p, req); p->rtptimeout = global_rtptimeout; p->rtpholdtimeout = global_rtpholdtimeout; @@ -18682,7 +18685,10 @@ static int show_chanstats_cb(void *__cur, void *__arg, int flags) return 0; /* don't care, we scan all channels */ } - ast_rtp_instance_get_stats(cur->rtp, &stats, AST_RTP_INSTANCE_STAT_ALL); + if (ast_rtp_instance_get_stats(cur->rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) { + ast_log(LOG_WARNING, "Could not get RTP stats.\n"); + return 0; + } if (c && ast_channel_cdr(c) && !ast_tvzero(ast_channel_cdr(c)->start)) { duration = (int)(ast_tvdiff_ms(ast_tvnow(), ast_channel_cdr(c)->start) / 1000); diff --git a/channels/iax2-provision.c b/channels/iax2-provision.c index 03b042b10e..eaa3b74323 100644 --- a/channels/iax2-provision.c +++ b/channels/iax2-provision.c @@ -258,7 +258,11 @@ int iax_provision_version(unsigned int *version, const char *template, int force memset(&ied, 0, sizeof(ied)); ast_mutex_lock(&provlock); - ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp)); + if (!(ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp)))) { + ast_log(LOG_ERROR, "ast_db_get failed to retrieve iax/provisioning/cache\n"); + ast_mutex_unlock(&provlock); + return -1; + } if (sscanf(tmp, "v%30x", version) != 1) { if (strcmp(tmp, "u")) { ret = iax_provision_build(&ied, version, template, force); diff --git a/channels/sig_analog.c b/channels/sig_analog.c index 7112cf29da..63523358c8 100644 --- a/channels/sig_analog.c +++ b/channels/sig_analog.c @@ -2744,7 +2744,10 @@ static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_ analog_train_echocanceller(p); ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr)); p->dop.op = ANALOG_DIAL_OP_REPLACE; - analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop); + if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) { + int dial_err = errno; + ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(dial_err)); + } p->echobreak = 0; } else { analog_set_dialing(p, 0); diff --git a/funcs/func_devstate.c b/funcs/func_devstate.c index 1b1318a5d2..e1f34387a1 100644 --- a/funcs/func_devstate.c +++ b/funcs/func_devstate.c @@ -187,6 +187,7 @@ static enum ast_device_state custom_devstate_callback(const char *data) { char buf[256] = ""; + /* Ignore check_return warning from Coverity fow ast_db_get below */ ast_db_get(astdb_family, data, buf, sizeof(buf)); return ast_devstate_val(buf); diff --git a/funcs/func_lock.c b/funcs/func_lock.c index 38c3aea35e..060fcb8095 100644 --- a/funcs/func_lock.c +++ b/funcs/func_lock.c @@ -486,9 +486,11 @@ static int unload_module(void) ast_custom_function_unregister(&trylock_function); ast_custom_function_unregister(&unlock_function); - pthread_cancel(broker_tid); - pthread_kill(broker_tid, SIGURG); - pthread_join(broker_tid, NULL); + if (broker_tid != AST_PTHREADT_NULL) { + pthread_cancel(broker_tid); + pthread_kill(broker_tid, SIGURG); + pthread_join(broker_tid, NULL); + } AST_LIST_UNLOCK(&locklist); @@ -500,7 +502,14 @@ static int load_module(void) int res = ast_custom_function_register(&lock_function); res |= ast_custom_function_register(&trylock_function); res |= ast_custom_function_register(&unlock_function); - ast_pthread_create_background(&broker_tid, NULL, lock_broker, NULL); + + if (ast_pthread_create_background(&broker_tid, NULL, lock_broker, NULL)) { + ast_log(LOG_ERROR, "Failed to start lock broker thread. Unloading func_lock module.\n"); + broker_tid = AST_PTHREADT_NULL; + unload_module(); + return AST_MODULE_LOAD_DECLINE; + } + return res; } diff --git a/main/acl.c b/main/acl.c index 7616236bc3..5902026531 100644 --- a/main/acl.c +++ b/main/acl.c @@ -533,7 +533,11 @@ int ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr) if (ast_sockaddr_is_ipv6(addr)) { if (ast_sockaddr_is_ipv4_mapped(addr)) { /* IPv4 ACLs apply to IPv4-mapped addresses */ - ast_sockaddr_ipv4_mapped(addr, &mapped_addr); + if (!ast_sockaddr_ipv4_mapped(addr, &mapped_addr)) { + ast_log(LOG_ERROR, "%s provided to ast_sockaddr_ipv4_mapped could not be converted. That shouldn't be possible.\n", + ast_sockaddr_stringify(addr)); + continue; + } addr_to_use = &mapped_addr; } else { /* An IPv4 ACL does not apply to an IPv6 address */ diff --git a/main/asterisk.c b/main/asterisk.c index d7fda95882..972c2fbb10 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -1468,7 +1468,11 @@ static int ast_makesocket(void) ast_log(LOG_WARNING, "Unable to register network verboser?\n"); } - ast_pthread_create_background(<hread, NULL, listener, NULL); + if (ast_pthread_create_background(<hread, NULL, listener, NULL)) { + ast_log(LOG_WARNING, "Unable to create listener thread.\n"); + close(ast_socket); + return -1; + } if (!ast_strlen_zero(ast_config_AST_CTL_OWNER)) { struct passwd *pw; @@ -3345,9 +3349,8 @@ static void *canary_thread(void *unused) sleep(120); for (;;) { - stat(canary_filename, &canary_stat); now = ast_tvnow(); - if (now.tv_sec > canary_stat.st_mtime + 60) { + if (stat(canary_filename, &canary_stat) || now.tv_sec > canary_stat.st_mtime + 60) { ast_log(LOG_WARNING, "The canary is no more. He has ceased to be! " "He's expired and gone to meet his maker! " diff --git a/main/features.c b/main/features.c index c9ea91ac52..4ff629fb42 100644 --- a/main/features.c +++ b/main/features.c @@ -8658,7 +8658,9 @@ int ast_features_init(void) return res; } ast_cli_register_multiple(cli_features, ARRAY_LEN(cli_features)); - ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL); + if (ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL)) { + return -1; + } ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL); res = ast_register_application2(parkedcall, parked_call_exec, NULL, NULL, NULL); if (!res) diff --git a/main/pbx.c b/main/pbx.c index e4774f2efb..f04051ceb2 100644 --- a/main/pbx.c +++ b/main/pbx.c @@ -6709,8 +6709,10 @@ static int show_debug_helper(int fd, const char *context, const char *exten, str dpc->context_existence = 1; - if (!c->pattern_tree) + if (!c->pattern_tree) { + /* Ignore check_return warning from Coverity for ast_exists_extension below */ ast_exists_extension(NULL, c->name, "s", 1, ""); /* do this to force the trie to built, if it is not already */ + } ast_rdlock_context(c); diff --git a/main/xmldoc.c b/main/xmldoc.c index 700813457f..f3deb5dc3c 100644 --- a/main/xmldoc.c +++ b/main/xmldoc.c @@ -1715,7 +1715,11 @@ static void xmldoc_parse_parameter(struct ast_xml_node *fixnode, const char *tab ast_xml_free_attr(paramname); printed = 1; } - xmldoc_parse_para(node, internaltabs, "\n", buffer); + if (xmldoc_parse_para(node, internaltabs, "\n", buffer)) { + /* If anything ever goes in below this condition before the continue below, + * we should probably continue immediately. */ + continue; + } continue; } else if ((xmldoc_parse_specialtags(node, internaltabs, "\n", buffer))) { continue; diff --git a/res/ael/ael.flex b/res/ael/ael.flex index 4d441fb73b..dab6ac05ca 100644 --- a/res/ael/ael.flex +++ b/res/ael/ael.flex @@ -805,7 +805,9 @@ struct pval *ael2_parse(char *filename, int *errors) if (my_file) free(my_file); my_file = strdup(filename); - stat(filename, &stats); + if (stat(filename, &stats)) { + ast_log(LOG_WARNING, "failed to populate stats from file '%s'\n", filename); + } buffer = (char*)malloc(stats.st_size+2); if (fread(buffer, 1, stats.st_size, fin) != stats.st_size) { ast_log(LOG_ERROR, "fread() failed: %s\n", strerror(errno)); @@ -875,7 +877,9 @@ static void setup_filestack(char *fnamebuf2, int fnamebuf_siz, glob_t *globbuf, } else { char *buffer; struct stat stats; - stat(fnamebuf2, &stats); + if (stat(fnamebuf2, &stats)) { + ast_log(LOG_WARNING, "Failed to populate stats from file '%s'\n", fnamebuf2); + } buffer = (char*)malloc(stats.st_size+1); if (fread(buffer, 1, stats.st_size, in1) != stats.st_size) { ast_log(LOG_ERROR, "fread() failed: %s\n", strerror(errno));