TT#14053 Fix code issues reported by coverity

In parsing config options, it might be possible that an option is
provided more than once, where the allocated string buffer is then
overwritten without being free'd.

When copying call codes, assume a fixed length size of 3.

Check return code of open() when opening /dev/null for std* re-
directs.

Fixed spaces vs tabs in intendations.

Use fropen() for daemonization.

Refactor medmysql_fetch_callids to return malloc'd array.

Change-Id: I51ab7ebc01bdd747c8b5bb320998a5c13d64009e
changes/74/12474/8
Andreas Granig 8 years ago
parent 8a7d055da3
commit 4f20a8a63a

@ -12,7 +12,7 @@ LDFLAGS := -lmysqlclient -g
CFILES := $(wildcard *.c)
OFILES := $(CFILES:.c=.o)
.PHONY: $(BIN) all
.PHONY: $(BIN) all clean coverity
all: $(BIN)
@ -26,4 +26,15 @@ clean:
rm -f *.o
rm -f core*
rm -f $(BIN)
rm -rf project.tgz cov-int
coverity:
cov-build --dir cov-int $(MAKE)
tar -czf project.tgz cov-int
curl --form token=$(COVERITY_MEDIATOR_TOKEN) \
--form email=$(DEBEMAIL) \
--form file=@project.tgz \
--form version="$(MEDIATOR_VERSION)" \
--form description="automatic upload" \
https://scan.coverity.com/builds?project=$(COVERITY_MEDIATOR_PROJECT)

@ -52,7 +52,7 @@ int cdr_process_records(med_entry_t *records, u_int64_t count, u_int64_t *ext_co
char *callid = records[0].callid;
cdr_entry_t *cdrs;
cdr_entry_t *cdrs = NULL;
u_int64_t cdr_count;
*ext_count = 0;
@ -124,6 +124,7 @@ int cdr_process_records(med_entry_t *records, u_int64_t count, u_int64_t *ext_co
/* TODO: no CDRs created? */
}
free(cdrs);
cdrs = NULL;
}
}
}
@ -150,6 +151,8 @@ int cdr_process_records(med_entry_t *records, u_int64_t count, u_int64_t *ext_co
error:
if(cdrs)
free(cdrs);
return -1;
}

@ -114,6 +114,8 @@ int config_parse_cmdopts(int argc, char **argv)
}
else if(c == 'D')
{
if(config_pid_path_free)
free(config_pid_path);
config_pid_path = (char*)strdup(optarg);
config_pid_path_free = 1;
}
@ -123,21 +125,29 @@ int config_parse_cmdopts(int argc, char **argv)
}
else if(c == 'h')
{
if(config_med_host_free)
free(config_med_host);
config_med_host = (char*)strdup(optarg);
config_med_host_free = 1;
}
else if(c == 'u')
{
if(config_med_user_free)
free(config_med_user);
config_med_user = (char*)strdup(optarg);
config_med_user_free = 1;
}
else if(c == 'p')
{
if(config_med_pass_free)
free(config_med_pass);
config_med_pass = (char*)strdup(optarg);
config_med_pass_free = 1;
}
else if(c == 'b')
{
if(config_med_db_free)
free(config_med_db);
config_med_db = (char*)strdup(optarg);
config_med_db_free = 1;
}
@ -147,21 +157,29 @@ int config_parse_cmdopts(int argc, char **argv)
}
else if(c == 'H')
{
if(config_cdr_host_free)
free(config_cdr_host);
config_cdr_host = (char*)strdup(optarg);
config_cdr_host_free = 1;
}
else if(c == 'U')
{
if(config_cdr_user_free)
free(config_cdr_user);
config_cdr_user = (char*)strdup(optarg);
config_cdr_user_free = 1;
}
else if(c == 'P')
{
if(config_cdr_pass_free)
free(config_cdr_pass);
config_cdr_pass = (char*)strdup(optarg);
config_cdr_pass_free = 1;
}
else if(c == 'B')
{
if(config_cdr_db_free)
free(config_cdr_db);
config_cdr_db = (char*)strdup(optarg);
config_cdr_db_free = 1;
}
@ -171,21 +189,29 @@ int config_parse_cmdopts(int argc, char **argv)
}
else if(c == 'S')
{
if(config_prov_host_free)
free(config_prov_host);
config_prov_host = (char*)strdup(optarg);
config_prov_host_free = 1;
}
else if(c == 'R')
{
if(config_prov_user_free)
free(config_prov_user);
config_prov_user = (char*)strdup(optarg);
config_prov_user_free = 1;
}
else if(c == 'A')
{
if(config_prov_pass_free)
free(config_prov_pass);
config_prov_pass = (char*)strdup(optarg);
config_prov_pass_free = 1;
}
else if(c == 'N')
{
if(config_prov_db_free)
free(config_prov_db);
config_prov_db = (char*)strdup(optarg);
config_prov_db_free = 1;
}
@ -195,6 +221,8 @@ int config_parse_cmdopts(int argc, char **argv)
}
else if(c == 'Z')
{
if(config_stats_host_free)
free(config_stats_host);
config_stats_host = (char*)strdup(optarg);
config_stats_host_free = 1;
}
@ -204,16 +232,22 @@ int config_parse_cmdopts(int argc, char **argv)
}
else if(c == 'W')
{
if(config_stats_user_free)
free(config_stats_user);
config_stats_user = (char*)strdup(optarg);
config_stats_user_free = 1;
}
else if(c == 'w')
{
if(config_stats_pass_free)
free(config_stats_pass);
config_stats_pass = (char*)strdup(optarg);
config_stats_pass_free = 1;
}
else if(c == 'X')
{
if(config_stats_db_free)
free(config_stats_db);
config_stats_db = (char*)strdup(optarg);
config_stats_db_free = 1;
}

@ -20,16 +20,28 @@ int daemonize()
{
int fds;
setsid();
for(fds = getdtablesize(); fds >= 0; --fds)
for(fds = getdtablesize(); fds >= 3; --fds)
{
if(fds != mediator_lockfd)
close(fds);
}
fds = open("/dev/null", O_RDWR); /* stdin */
if(dup(fds) < 0) {}; /* stdout */
if(dup(fds) < 0) {}; /* stderr */
if (freopen("/dev/null", "r", stdin) == NULL) {
syslog(LOG_CRIT, "Failed to reopen stdin to /dev/null: %s", strerror(errno));
return -1;
}
if (freopen("/dev/null", "w", stdout) == NULL) {
syslog(LOG_CRIT, "Failed to reopen stdout to /dev/null: %s", strerror(errno));
return -1;
}
if (freopen("/dev/null", "w", stderr) == NULL) {
syslog(LOG_CRIT, "Failed to reopen stderr to /dev/null: %s", strerror(errno));
return -1;
}
umask(027);
if(chdir("/") < 0) {};
if(chdir("/") < 0) {
syslog(LOG_CRIT, "Failed to chdir to root: %s", strerror(errno));
return -1;
}
}
return 0;

@ -233,47 +233,44 @@ int main(int argc, char **argv)
id_count = 0, rec_count = 0, cdr_count = 0;
last_count = mediator_count;
if(medmysql_fetch_callids(&callids, &id_count) != 0)
callids = medmysql_fetch_callids(&id_count);
if(!callids)
break;
if(id_count > 0)
{
if (medmysql_batch_start(&batches))
break;
if (medmysql_batch_start(&batches))
break;
/*syslog(LOG_DEBUG, "Processing %"PRIu64" accounting record group(s).", id_count);*/
for(i = 0; i < id_count && !mediator_shutdown; ++i)
{
/*syslog(LOG_DEBUG, "Processing %"PRIu64" accounting record group(s).", id_count);*/
for(i = 0; i < id_count && !mediator_shutdown; ++i)
{
#ifdef WITH_TIME_CALC
gettimeofday(&tv_start, NULL);
gettimeofday(&tv_start, NULL);
#endif
if(medmysql_fetch_records(&(callids[i]), &records, &rec_count) != 0)
goto out;
if(cdr_process_records(records, rec_count, &cdr_count, &batches) != 0)
goto out;
if(medmysql_fetch_records(&(callids[i]), &records, &rec_count) != 0)
goto out;
if(rec_count > 0)
{
free(records);
}
if(cdr_process_records(records, rec_count, &cdr_count, &batches) != 0)
goto out;
mediator_count += cdr_count;
#ifdef WITH_TIME_CALC
gettimeofday(&tv_stop, NULL);
runtime = mediator_calc_runtime(&tv_start, &tv_stop);
syslog(LOG_DEBUG, "Runtime for record group was %"PRIu64" ms.", runtime);
#endif
if(rec_count > 0)
{
free(records);
}
free(callids);
if (medmysql_batch_end(&batches))
break;
mediator_count += cdr_count;
#ifdef WITH_TIME_CALC
gettimeofday(&tv_stop, NULL);
runtime = mediator_calc_runtime(&tv_start, &tv_stop);
syslog(LOG_DEBUG, "Runtime for record group was %"PRIu64" ms.", runtime);
#endif
}
free(callids);
if (medmysql_batch_end(&batches))
break;
if(mediator_count > last_count)
{
syslog(LOG_DEBUG, "Overall %"PRIu64" CDRs created so far.", mediator_count);

@ -10,24 +10,24 @@
#define XFERSUFFIX "_xfer-1"
#define MED_CALLID_QUERY "select a.callid from acc a" \
" where a.method = 'INVITE' " \
" and (a.sip_code != '200' " \
" OR EXISTS " \
" (select b.id from acc b " \
" where b.callid = a.callid " \
" and b.method = 'BYE' " \
" limit 1) " \
" OR EXISTS " \
" (select b.id from acc b " \
" where b.callid = concat(a.callid, '"PBXSUFFIX"') " \
" and b.method = 'BYE' " \
" limit 1) " \
" OR EXISTS " \
" (select b.id from acc b " \
" where b.callid = concat(a.callid, '"XFERSUFFIX"') " \
" and b.method = 'BYE' " \
" limit 1) " \
" ) " \
" where a.method = 'INVITE' " \
" and (a.sip_code != '200' " \
" OR EXISTS " \
" (select b.id from acc b " \
" where b.callid = a.callid " \
" and b.method = 'BYE' " \
" limit 1) " \
" OR EXISTS " \
" (select b.id from acc b " \
" where b.callid = concat(a.callid, '"PBXSUFFIX"') " \
" and b.method = 'BYE' " \
" limit 1) " \
" OR EXISTS " \
" (select b.id from acc b " \
" where b.callid = concat(a.callid, '"XFERSUFFIX"') " \
" and b.method = 'BYE' " \
" limit 1) " \
" ) " \
" group by a.callid limit 0,200000"
#define MED_FETCH_QUERY "(select distinct sip_code, sip_reason, method, callid, time, time_hires, " \
@ -181,14 +181,14 @@ void medmysql_cleanup()
}
/**********************************************************************/
int medmysql_fetch_callids(med_callid_t **callids, u_int64_t *count)
med_callid_t *medmysql_fetch_callids(u_int64_t *count)
{
MYSQL_RES *res;
MYSQL_ROW row;
char query[1024] = "";
size_t callid_size;
u_int64_t i = 0;
int ret = 0;
med_callid_t *callids = NULL;
*count = 0;
@ -200,7 +200,7 @@ int medmysql_fetch_callids(med_callid_t **callids, u_int64_t *count)
{
syslog(LOG_CRIT, "Error getting acc callids: %s",
mysql_error(med_handler));
return -1;
return NULL;
}
res = mysql_store_result(med_handler);
@ -211,18 +211,20 @@ int medmysql_fetch_callids(med_callid_t **callids, u_int64_t *count)
}
callid_size = sizeof(med_callid_t) * (*count);
*callids = (med_callid_t*)malloc(callid_size);
memset(*callids, '\0', callid_size);
if(*callids == NULL)
callids = malloc(callid_size);
if(callids == NULL)
{
syslog(LOG_CRIT, "Error allocating callid memory: %s", strerror(errno));
ret = -1;
free(callids);
callids = NULL;
goto out;
}
memset(callids, '\0', callid_size);
while((row = mysql_fetch_row(res)) != NULL)
{
med_callid_t *c = &(*callids)[i++];
med_callid_t *c = &callids[i++];
if(row == NULL || row[0] == NULL)
{
g_strlcpy(c->value, "0", sizeof(c->value));
@ -232,13 +234,15 @@ int medmysql_fetch_callids(med_callid_t **callids, u_int64_t *count)
/*syslog(LOG_DEBUG, "callid[%"PRIu64"]='%s'", i, c->value);*/
if (check_shutdown())
return -1;
if (check_shutdown()) {
free(callids);
return NULL;
}
}
out:
mysql_free_result(res);
return ret;
return callids;
}
/**********************************************************************/
@ -255,9 +259,9 @@ int medmysql_fetch_records(med_callid_t *callid,
*count = 0;
snprintf(query, sizeof(query), MED_FETCH_QUERY,
callid->value,
callid->value, callid->value,
callid->value, callid->value);
callid->value,
callid->value, callid->value,
callid->value, callid->value);
/*syslog(LOG_DEBUG, "q='%s'", query);*/
@ -373,7 +377,7 @@ int medmysql_delete_entries(const char *callid, struct medmysql_batches *batches
int medmysql_insert_cdrs(cdr_entry_t *entries, u_int64_t count, struct medmysql_batches *batches)
{
u_int64_t i;
int gpp;
int gpp;
for(i = 0; i < count; ++i)
{
@ -395,12 +399,12 @@ int medmysql_insert_cdrs(cdr_entry_t *entries, u_int64_t count, struct medmysql_
"source_carrier_cost, source_reseller_cost, source_customer_cost, " \
"destination_carrier_cost, destination_reseller_cost, destination_customer_cost, " \
"split, " \
"source_gpp0, source_gpp1, source_gpp2, source_gpp3, source_gpp4, " \
"source_gpp5, source_gpp6, source_gpp7, source_gpp8, source_gpp9, " \
"destination_gpp0, destination_gpp1, destination_gpp2, destination_gpp3, destination_gpp4, " \
"destination_gpp5, destination_gpp6, destination_gpp7, destination_gpp8, destination_gpp9, " \
"source_gpp0, source_gpp1, source_gpp2, source_gpp3, source_gpp4, " \
"source_gpp5, source_gpp6, source_gpp7, source_gpp8, source_gpp9, " \
"destination_gpp0, destination_gpp1, destination_gpp2, destination_gpp3, destination_gpp4, " \
"destination_gpp5, destination_gpp6, destination_gpp7, destination_gpp8, destination_gpp9, " \
"source_lnp_prefix, destination_lnp_prefix" \
") values ");
") values ");
}
cdr_entry_t *e = &(entries[i]);
@ -505,32 +509,32 @@ int medmysql_insert_cdrs(cdr_entry_t *entries, u_int64_t count, struct medmysql_
CDRESCAPE(str_dest_customer_cost);
CDRPRINT(",");
CDRESCAPE(str_split);
for(gpp = 0; gpp < 10; ++gpp)
{
if(strnlen(e->source_gpp[gpp], sizeof(e->source_gpp[gpp])) > 0)
{
CDRPRINT(",'");
CDRESCAPE(e->source_gpp[gpp]);
CDRPRINT("'");
}
else
{
CDRPRINT(",NULL");
}
}
for(gpp = 0; gpp < 10; ++gpp)
{
if(strnlen(e->destination_gpp[gpp], sizeof(e->destination_gpp[gpp])) > 0)
{
CDRPRINT(",'");
CDRESCAPE(e->destination_gpp[gpp]);
CDRPRINT("'");
}
else
{
CDRPRINT(",NULL");
}
}
for(gpp = 0; gpp < 10; ++gpp)
{
if(strnlen(e->source_gpp[gpp], sizeof(e->source_gpp[gpp])) > 0)
{
CDRPRINT(",'");
CDRESCAPE(e->source_gpp[gpp]);
CDRPRINT("'");
}
else
{
CDRPRINT(",NULL");
}
}
for(gpp = 0; gpp < 10; ++gpp)
{
if(strnlen(e->destination_gpp[gpp], sizeof(e->destination_gpp[gpp])) > 0)
{
CDRPRINT(",'");
CDRESCAPE(e->destination_gpp[gpp]);
CDRPRINT("'");
}
else
{
CDRPRINT(",NULL");
}
}
CDRPRINT(",'");
CDRESCAPE(e->source_lnp_prefix);
@ -584,7 +588,7 @@ int medmysql_update_call_stat_info(const char *call_code, const double start_tim
if ((period_t = g_hash_table_lookup(med_call_stat_info_table, &period_key)) == NULL) {
period_t = malloc(sizeof(struct medmysql_call_stat_info_t));
strcpy(period_t->period, period);
strcpy(period_t->call_code, call_code);
g_strlcpy(period_t->call_code, call_code, sizeof(period_t->call_code));
period_t->amount = 1;
g_hash_table_insert(med_call_stat_info_table, strdup(period_key), period_t);
} else {

@ -23,14 +23,14 @@ struct medmysql_batches {
};
struct medmysql_call_stat_info_t {
char period[STAT_PERIOD_SIZE];
char period[STAT_PERIOD_SIZE];
char call_code[4];
u_int64_t amount;
};
int medmysql_init();
void medmysql_cleanup();
int medmysql_fetch_callids(med_callid_t **callids, u_int64_t *count);
med_callid_t *medmysql_fetch_callids(u_int64_t *count);
int medmysql_fetch_records(med_callid_t *callid, med_entry_t **entries, u_int64_t *count);
int medmysql_trash_entries(const char *callid, struct medmysql_batches *);
int medmysql_backup_entries(const char *callid, struct medmysql_batches *);

Loading…
Cancel
Save