Fix coverity issues (PROC_USE.VULNERABLE) 09/299109/3 accepted/tizen_8.0_unified accepted/tizen_unified tizen tizen_8.0 accepted/tizen/8.0/unified/20231005.092441 accepted/tizen/unified/20230921.063901 tizen_8.0_m2_release
authorJaechul Lee <jcsing.lee@samsung.com>
Wed, 20 Sep 2023 02:27:13 +0000 (11:27 +0900)
committerJaechul Lee <jcsing.lee@samsung.com>
Wed, 20 Sep 2023 07:51:14 +0000 (16:51 +0900)
checker said "strerror makes no guaranteee of thread safety. Use strerror_r function instead."
In addition to this, coverity found the possibility of the resource leak

[Version] 0.1.21
[Issue Type] SVACE

Change-Id: I1293c655a8f973ef3d45b50fc6093154c9007436
Signed-off-by: Jaechul Lee <jcsing.lee@samsung.com>
packaging/audio-hal-emul.spec
tizen-audio-file.c
tizen-audio-glue.c

index 3d68631..f61c198 100644 (file)
@@ -1,6 +1,6 @@
 Name:       audio-hal-emul
 Summary:    TIZEN Audio HAL for Emulator
-Version:    0.1.20
+Version:    0.1.21
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0
index 1ad1488..5091028 100644 (file)
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <errno.h>
 
 #include <tizen-audio.h>
 #include <tizen-audio-internal.h>
 #include <hal/hal-common-interface.h>
 #include <tizen-audio-file.h>
 
+#define AUDIO_LOG_ERROR_WITH_STDERR(msg) __print_std_error(__LINE__, #msg)
+
+static void __print_std_error(int line, char *msg)
+{
+    char e[256];
+
+    strerror_r(errno, e, sizeof(e));
+
+    AUDIO_LOG_ERROR("line(%d). %s. err(%s)", line, msg, e);
+}
+
 audio_return_e audio_file_open(const char *type, const char *filename, uint32_t direction, void *sample_spec, uint32_t period_size, uint32_t periods, void **handle)
 {
     FILE *f;
@@ -115,23 +127,23 @@ audio_return_e audio_file_avail(void *handle, uint32_t *avail)
 
     cur = ftell(f);
     if (cur < 0) {
-        AUDIO_LOG_ERROR("Failed to get ftell(%s)", strerror(errno));
+        AUDIO_LOG_ERROR_WITH_STDERR("Failed to get the current location");
         return AUDIO_ERR_INTERNAL;
     }
 
     if (fseek(f, 0L, SEEK_END) < 0) {
-        AUDIO_LOG_ERROR("Failed to seek. err(%s)", strerror(errno));
+        AUDIO_LOG_ERROR_WITH_STDERR("Failed to seek with SEEK_END");
         return AUDIO_ERR_INTERNAL;
     }
 
     end = ftell(f);
     if (end < 0) {
-        AUDIO_LOG_ERROR("Failed to get ftell(%s)", strerror(errno));
+        AUDIO_LOG_ERROR_WITH_STDERR("Failed to get the end location");
         return AUDIO_ERR_INTERNAL;
     }
 
     if (fseek(f, cur, SEEK_SET) < 0) {
-        AUDIO_LOG_ERROR("Failed to seek. err(%s)", strerror(errno));
+        AUDIO_LOG_ERROR_WITH_STDERR("Failed to seek with SEEK_SET");
         return AUDIO_ERR_INTERNAL;
     }
 
@@ -151,7 +163,7 @@ audio_return_e audio_file_write(void *handle, const void *buffer, uint32_t frame
 
     ret = fwrite(buffer, sizeof(short), frames, f);
     if (ret == 0) {
-        AUDIO_LOG_ERROR("Failed to write(%s)", strerror(errno));
+        AUDIO_LOG_ERROR_WITH_STDERR("Failed to write");
         return AUDIO_ERR_INTERNAL;
     }
 
@@ -169,7 +181,7 @@ audio_return_e audio_file_read(void *handle, void *buffer, uint32_t frames)
 
     ret = fread(buffer, sizeof(short), frames, f);
     if (ret == 0) {
-        AUDIO_LOG_ERROR("Failed to read(%s)", strerror(errno));
+        AUDIO_LOG_ERROR_WITH_STDERR("Failed to read");
         return AUDIO_ERR_INTERNAL;
     }
 
index d2cb085..8b8ecf9 100644 (file)
@@ -22,11 +22,9 @@ struct audio_interface_s {
 
 audio_return_e audio_open(void *audio_handle, const char *card, const char *device, uint32_t direction, void *sample_spec, uint32_t period_size, uint32_t periods, void **handle)
 {
-    struct audio_interface_s *interface = (struct audio_interface_s *)calloc(1, sizeof(struct audio_interface_s));
+    struct audio_interface_s *interface;
     void *glue_handle = NULL;
 
-    AUDIO_RETURN_VAL_IF_FAIL(interface, AUDIO_ERR_RESOURCE);
-
     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_RESOURCE);
     AUDIO_RETURN_VAL_IF_FAIL(card, AUDIO_ERR_RESOURCE);
     AUDIO_RETURN_VAL_IF_FAIL(device, AUDIO_ERR_RESOURCE);
@@ -36,6 +34,12 @@ audio_return_e audio_open(void *audio_handle, const char *card, const char *devi
     AUDIO_LOG_INFO("card(%s), device(%s), direction(%u), period_size(%u), periods(%u)",
                         card, device, direction, period_size, periods);
 
+    interface = (struct audio_interface_s *)calloc(1, sizeof(struct audio_interface_s));
+    if (!interface) {
+        AUDIO_LOG_INFO("Failed to allocate memory");
+        return AUDIO_ERR_INTERNAL;
+    }
+
     if (!strcmp(card,"file")) {
         interface->open = audio_file_open;
         interface->start = audio_file_start;