[mod_avmd] Coverity fixes

* [mod_avmd] Coverity CID 1395501 (Dereference null return value)

* [mod_avmd] Coverity CID 1395478 (Resource leak)
This commit is contained in:
Jakub Karolczyk 2023-04-17 19:49:42 +01:00 committed by GitHub
parent cb3373872a
commit 6eefc674fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 23 deletions

View File

@ -86,7 +86,6 @@ typedef union {
static uint32_t index_from_float(float f); static uint32_t index_from_float(float f);
static float float_from_index(uint32_t d); static float float_from_index(uint32_t d);
static float *acos_table = NULL; static float *acos_table = NULL;
static int acos_fd = -1;
#ifdef FAST_ACOSF_TESTING #ifdef FAST_ACOSF_TESTING
@ -112,6 +111,10 @@ extern int compute_table(void)
acos_table_file = fopen(ACOS_TABLE_FILENAME, "w"); acos_table_file = fopen(ACOS_TABLE_FILENAME, "w");
if (!acos_table_file) {
return -3;
}
for (i = 0; i < ACOS_TABLE_LENGTH; i++) { for (i = 0; i < ACOS_TABLE_LENGTH; i++) {
f = acosf(float_from_index(i)); f = acosf(float_from_index(i));
res = fwrite(&f, sizeof(f), 1, acos_table_file); res = fwrite(&f, sizeof(f), 1, acos_table_file);
@ -124,10 +127,12 @@ extern int compute_table(void)
if (res != 0) { if (res != 0) {
return -2; return -2;
} }
return 0; return 0;
fail: fail:
fclose(acos_table_file); fclose(acos_table_file);
return -1; return -1;
} }
@ -144,8 +149,9 @@ extern int init_fast_acosf(void)
* or some other error occured */ * or some other error occured */
errsv = errno; errsv = errno;
strerror_r(errsv, err, 150); strerror_r(errsv, err, 150);
if (errsv != ENOENT) return -1; if (errsv != ENOENT) {
else { return -1;
} else {
switch_log_printf( switch_log_printf(
SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_LOG,
SWITCH_LOG_NOTICE, SWITCH_LOG_NOTICE,
@ -166,10 +172,10 @@ extern int init_fast_acosf(void)
acos_fp = fopen(ACOS_TABLE_FILENAME, "r"); acos_fp = fopen(ACOS_TABLE_FILENAME, "r");
if (acos_fp == NULL) return -3; if (acos_fp == NULL) return -3;
/* can't fail */ /* can't fail */
acos_fd = fileno(acos_fp);
acos_table = (float *) mmap( acos_table = (float *) mmap(
NULL, /* kernel chooses the address at which to create the mapping */ NULL, /* kernel chooses the address at which to create the mapping */
ACOS_TABLE_LENGTH * sizeof(float), PROT_READ, MAP_SHARED, acos_fd, 0); ACOS_TABLE_LENGTH * sizeof(float), PROT_READ, MAP_SHARED, fileno(acos_fp), 0);
fclose(acos_fp);
if (acos_table == MAP_FAILED) return -4; if (acos_table == MAP_FAILED) return -4;
return 0; return 0;
@ -178,9 +184,7 @@ extern int init_fast_acosf(void)
extern int destroy_fast_acosf(void) extern int destroy_fast_acosf(void)
{ {
if (munmap(acos_table, ACOS_TABLE_LENGTH) == -1) return -1; if (munmap(acos_table, ACOS_TABLE_LENGTH) == -1) return -1;
if (acos_fd != -1) {
if (close(acos_fd) == -1) return -2;
}
/* disable use of fast arc cosine file */ /* disable use of fast arc cosine file */
acos_table = NULL; acos_table = NULL;

View File

@ -1622,9 +1622,6 @@ SWITCH_STANDARD_APP(avmd_start_function) {
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown) { SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown) {
size_t session_n; size_t session_n;
#ifndef WIN32
int res;
#endif
switch_mutex_lock(avmd_globals.mutex); switch_mutex_lock(avmd_globals.mutex);
@ -1638,18 +1635,8 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown) {
#ifndef WIN32 #ifndef WIN32
if (avmd_globals.settings.fast_math == 1) { if (avmd_globals.settings.fast_math == 1) {
res = destroy_fast_acosf(); if (destroy_fast_acosf()) {
if (res != 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed unmap arc cosine table\n");
switch (res) {
case -1:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed unmap arc cosine table\n");
break;
case -2:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed closing arc cosine table\n");
break;
default:
break;
}
} }
} }
#endif #endif
@ -1658,6 +1645,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown) {
switch_mutex_unlock(avmd_globals.mutex); switch_mutex_unlock(avmd_globals.mutex);
switch_mutex_destroy(avmd_globals.mutex); switch_mutex_destroy(avmd_globals.mutex);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Advanced voicemail detection disabled\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Advanced voicemail detection disabled\n");
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }