]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[mod_avmd] Coverity fixes
authorJakub Karolczyk <jakub.karolczyk@signalwire.com>
Mon, 17 Apr 2023 18:49:42 +0000 (19:49 +0100)
committerGitHub <noreply@github.com>
Mon, 17 Apr 2023 18:49:42 +0000 (21:49 +0300)
* [mod_avmd] Coverity CID 1395501 (Dereference null return value)

* [mod_avmd] Coverity CID 1395478 (Resource leak)

src/mod/applications/mod_avmd/avmd_fast_acosf.c
src/mod/applications/mod_avmd/mod_avmd.c

index 572c6a84f9136dfa020fac603990edab254d81b1..59b581b98d6f5f3b1f69cd2b26c8ebdfa7668784 100644 (file)
@@ -86,7 +86,6 @@ typedef union {
 static uint32_t index_from_float(float f);
 static float float_from_index(uint32_t d);
 static float *acos_table = NULL;
-static int acos_fd = -1;
 
 
 #ifdef FAST_ACOSF_TESTING
@@ -112,6 +111,10 @@ extern int compute_table(void)
 
        acos_table_file = fopen(ACOS_TABLE_FILENAME, "w");
 
+       if (!acos_table_file) {
+               return -3;
+       }
+
        for (i = 0; i < ACOS_TABLE_LENGTH; i++) {
                f = acosf(float_from_index(i));
                res = fwrite(&f, sizeof(f), 1, acos_table_file);
@@ -124,10 +127,12 @@ extern int compute_table(void)
        if (res != 0) {
                return -2;
        }
+
        return 0;
 
 fail:
        fclose(acos_table_file);
+
        return -1;
 }
 
@@ -144,8 +149,9 @@ extern int init_fast_acosf(void)
                         * or some other error occured */
                        errsv = errno;
                        strerror_r(errsv, err, 150);
-                       if (errsv != ENOENT) return -1;
-                       else {
+                       if (errsv != ENOENT) {
+                               return -1;
+                       } else {
                                switch_log_printf(
                                        SWITCH_CHANNEL_LOG,
                                        SWITCH_LOG_NOTICE,
@@ -166,10 +172,10 @@ extern int init_fast_acosf(void)
        acos_fp = fopen(ACOS_TABLE_FILENAME, "r");
        if (acos_fp == NULL) return -3;
        /* can't fail */
-       acos_fd = fileno(acos_fp);
        acos_table = (float *) mmap(
                        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;
 
        return 0;
@@ -178,9 +184,7 @@ extern int init_fast_acosf(void)
 extern int destroy_fast_acosf(void)
 {
        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 */
        acos_table = NULL;
 
index dde3dfbf9309278cca472e5578ed0aa7a924da7c..6c59256eda992a416e574c33c9e06f97a11a58b4 100644 (file)
@@ -1622,9 +1622,6 @@ SWITCH_STANDARD_APP(avmd_start_function) {
 
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown) {
        size_t session_n;
-#ifndef WIN32
-       int res;
-#endif
 
        switch_mutex_lock(avmd_globals.mutex);
 
@@ -1638,18 +1635,8 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown) {
 
 #ifndef WIN32
        if (avmd_globals.settings.fast_math == 1) {
-               res = destroy_fast_acosf();
-               if (res != 0) {
-                       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;
-                       }
+               if (destroy_fast_acosf()) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed unmap arc cosine table\n");
                }
        }
 #endif
@@ -1658,6 +1645,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown) {
        switch_mutex_unlock(avmd_globals.mutex);
        switch_mutex_destroy(avmd_globals.mutex);
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Advanced voicemail detection disabled\n");
+
        return SWITCH_STATUS_SUCCESS;
 }