audio:  Linux Yocto SDK Coverity Check - aml-audio-service. [1/1]

PD#SWPL-172608

Problem:
Linux Yocto SDK Coverity Check - aml-audio-service.

Solution:
Fix Coverity Check - aml-audio-service.

Verify:
t5w.

Change-Id: Ie136a8ad54568d2684fe9d39eaa298c1983fa06b
Signed-off-by: yuliang.hu <yuliang.hu@amlogic.com>
diff --git a/src/audio_client.cpp b/src/audio_client.cpp
index 83e0dd9..eb61ef8 100644
--- a/src/audio_client.cpp
+++ b/src/audio_client.cpp
@@ -165,6 +165,7 @@
   } else if (config->type == AUDIO_PORT_TYPE_SESSION) {
     c.mutable_session()->set_session(config->ext.session.session);
   }
+  /*coverity[UNINIT]: The definition is not in our code and cannot be changed*/
   return c;
 }
 
diff --git a/src/audio_if_client.cpp b/src/audio_if_client.cpp
index b4bda68..a0746cb 100644
--- a/src/audio_if_client.cpp
+++ b/src/audio_if_client.cpp
@@ -321,6 +321,7 @@
 
   stream_out_client->stream_out = stream_out_template;
   *stream_out = &(stream_out_client->stream_out);
+  /*coverity[resource_leak]: stream_out_client is free in Device_close_output_stream but not here*/
   return r;
 }
 
@@ -356,6 +357,7 @@
 
   stream_in_client->stream_in = stream_in_template;
   *stream_in = &(stream_in_client->stream_in);
+  /*coverity[resource_leak]: stream_in_client is free in Device_close_input_stream but not here*/
   return r;
 }
 
@@ -464,6 +466,7 @@
 int audio_hw_load_interface(audio_hw_device_t **dev)
 {
   TRACE_ENTRY();
+  /*coverity[missing_lock]:printf no need to lock*/
   printf("PID = %d, inited = %d\n", ::getpid(), inited);
 
   const char *url = std::getenv("AUDIO_SERVER_SOCKET");
diff --git a/src/audio_server.cpp b/src/audio_server.cpp
index 36dcb23..2caedd2 100644
--- a/src/audio_server.cpp
+++ b/src/audio_server.cpp
@@ -203,10 +203,12 @@
 
       char *param = dev_->get_parameters(dev_, request->keys().c_str());
       response->set_ret(param ? 0 : -1);
-      response->set_status_string(std::string(param));
+      if (param) {
+          response->set_status_string(std::string(param));
 
-      // param is heap allocated and need free in behalf of client
-      free(param);
+          // param is heap allocated and need free in behalf of client
+          free(param);
+      }
 
       return Status::OK;
     }
@@ -373,10 +375,12 @@
 
       char *param = dev_->dump(dev_, 0);
       response->set_ret(param ? 0 : -1);
-      response->set_status_string(std::string(param));
+      if (param) {
+          response->set_status_string(std::string(param));
 
-      // param is heap allocated and need free
-      free(param);
+          // param is heap allocated and need free
+          free(param);
+      }
 
       return Status::OK;
     }
@@ -435,7 +439,8 @@
           // AudioPortConfigDeviceExt
           configs[i].ext.device.hw_module = (audio_module_handle_t)config.device().hw_module();
           configs[i].ext.device.type = (audio_devices_t)config.device().type();
-          strncpy(configs[i].ext.device.address, config.device().address().c_str(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
+          strncpy(configs[i].ext.device.address, config.device().address().c_str(), AUDIO_DEVICE_MAX_ADDRESS_LEN - 1);
+          configs[i].ext.device.address[AUDIO_DEVICE_MAX_ADDRESS_LEN - 1] = '\0';
         } else if (configs[i].type == AUDIO_PORT_TYPE_MIX) {
           // AudioPortConfigMixExt
           configs[i].ext.mix.hw_module = (audio_module_handle_t)config.mix().hw_module();
@@ -492,7 +497,8 @@
         // AudioPortConfigDeviceExt
         config.ext.device.hw_module = (audio_module_handle_t)request->device().hw_module();
         config.ext.device.type = (audio_devices_t)request->device().type();
-        strncpy(config.ext.device.address, request->device().address().c_str(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
+        strncpy(config.ext.device.address, request->device().address().c_str(), AUDIO_DEVICE_MAX_ADDRESS_LEN - 1);
+        config.ext.device.address[AUDIO_DEVICE_MAX_ADDRESS_LEN - 1] = '\0';
       } else if (config.type == AUDIO_PORT_TYPE_MIX) {
         // AudioPortConfigMixExt
         config.ext.mix.hw_module = (audio_module_handle_t)request->mix().hw_module();
@@ -594,10 +600,12 @@
 
       char *param = stream->get_parameters(stream, request->keys().c_str());
       response->set_ret(param ? 0 : -1);
-      response->set_status_string(std::string(param));
+      if (param) {
+          response->set_status_string(std::string(param));
 
-      // param is heap allocated and need free in behalf of client
-      free(param);
+          // param is heap allocated and need free in behalf of client
+          free(param);
+      }
 
       return Status::OK;
     }
@@ -950,10 +958,14 @@
 
 static void SetAudioPermissions(const char* file_path){
   struct stat buf;
+  /*coverity[toctou]: The file_path status will not change after stat check*/
   if (stat(file_path, &buf) != 0)
     return;
 
-  chmod(file_path, (buf.st_mode & 0711) | 0066);
+  int ret = chmod(file_path, (buf.st_mode & 0711) | 0066);
+  if (-1 == ret) {
+    ALOGE("Error:chmod, need check!!");
+  }
 }
 
 static bool runListenerLoop(const std::unique_ptr<grpc::Server> &server, const std::string &address)
@@ -966,6 +978,7 @@
   std::string socketPath = address.substr(strlen("unix://"));
 
   struct stat st;
+  /*coverity[toctou]*/
   if ((stat(socketPath.c_str(), &st) == 0) && ((st.st_mode & S_IFMT) == S_IFSOCK)) {
     unlink(socketPath.c_str());
   }
@@ -984,6 +997,7 @@
   if (bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) {
     ALOGE("Error: failed to bind to unix socket @ '%s' (%d - %s)",
           socketPath.c_str(), errno, strerror(errno));
+    close(serverSock);
     return false;
   }
 
@@ -1089,6 +1103,7 @@
   exit(1);
 }
 
+/*coverity[UNCAUGHT_EXCEPT]: Do not catch exceptions in order to generate coredumps*/
 int main(int argc, char** argv)
 {
   int r = daemonize();
diff --git a/src/hal_capture.c b/src/hal_capture.c
index 0b1bc62..6c1b50a 100644
--- a/src/hal_capture.c
+++ b/src/hal_capture.c
@@ -108,7 +108,10 @@
     FILE *fp = fopen("/data/hal_capture.raw", "r");
     if (fp != NULL) {
         fclose(fp);
-        remove("/data/hal_capture.raw");
+        ret = remove("/data/hal_capture.raw");
+        if (ret == -1) {
+            printf("remove return err, need check!!\n");
+        }
     }
 
     fp = fopen("/data/hal_capture.raw", "a+");
@@ -125,7 +128,10 @@
         buffer = NULL;
         fclose(fp);
     }
-
+    if (buffer) {
+        free(buffer);
+        buffer = NULL;
+    }
     return;
 }
 
diff --git a/src/hal_patch.c b/src/hal_patch.c
index 172a65e..f4f61c6 100644
--- a/src/hal_patch.c
+++ b/src/hal_patch.c
@@ -8,7 +8,8 @@
     struct audio_port_config source = {0};
     struct audio_port_config sink = {0};
     audio_patch_handle_t patch_handle;
-    int ret, device_type;
+    int ret;
+    unsigned int device_type;
 
     if (argc < 2) {
         fprintf(stderr, "Usage: hal_patch [source]\n");
diff --git a/src/halplay.c b/src/halplay.c
index 248d336..e4bcd86 100644
--- a/src/halplay.c
+++ b/src/halplay.c
@@ -346,7 +346,7 @@
                     break;
             }
             break;
-
+        /*coverity[deadcode]: Default processing is ok*/
         default:
             break;
     }
diff --git a/src/start_arc.c b/src/start_arc.c
index c0990a4..984e5ea 100644
--- a/src/start_arc.c
+++ b/src/start_arc.c
@@ -109,7 +109,7 @@
             process_cec_msg(cecmsg,read_len);

         }

     }

-    return;

+    return NULL;
 }

 

 static void sighandler(int sig)

diff --git a/src/test.c b/src/test.c
index e28ab6b..e2a9c58 100644
--- a/src/test.c
+++ b/src/test.c
@@ -232,7 +232,7 @@
 static void test_input_stream(audio_hw_device_t *device)
 {
     int ret;
-    size_t s;
+    int s;
     struct audio_config config;
 
     memset(&config, 0, sizeof(config));
diff --git a/src/test_arc.c b/src/test_arc.c
index 977d0be..87fd2a7 100644
--- a/src/test_arc.c
+++ b/src/test_arc.c
@@ -293,6 +293,7 @@
             process_cec_msg(cecmsg,read_len);

         }

     }

+    return NULL;
 }

 

 static void sighandler(int sig)

diff --git a/src/vx_v4_ctr.c b/src/vx_v4_ctr.c
index 1ea28ce..fd586b0 100644
--- a/src/vx_v4_ctr.c
+++ b/src/vx_v4_ctr.c
@@ -868,6 +868,7 @@
 int getCStrData (char *data, char *value)
 {
     int status_temp,status;
+    /*coverity[DC.STREAM_BUFFER]*/
     status = scanf("%s%s", data, value);
     if (status == 0) {
         goto _exit_clear;
@@ -939,6 +940,7 @@
     LOG("Please enter Test case Num: ");
     if (getIntData (&caseNum) < 0) {
         LOG("[Failed] Invalid case Num, Exit!\n");
+        fclose(fp);
         return;
     }