hal : Add NULL check before dereferencing pointer

-Add proper checks to fix NULL pointer dereference issues.

Change-Id: Idd3d165ac179d21745958710d28cfae43a50c793
diff --git a/hal/audio_extn/a2dp.c b/hal/audio_extn/a2dp.c
index c484713..91a1e15 100644
--- a/hal/audio_extn/a2dp.c
+++ b/hal/audio_extn/a2dp.c
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -788,6 +788,10 @@
     bool is_configured = false;
     int ret = 0;
     int sample_rate_backup;
+
+    if(aptx_bt_cfg == NULL)
+        return false;
+
 #ifndef LINUX_ENABLED
     struct aptx_enc_cfg_t aptx_dsp_cfg;
     mixer_size = sizeof(struct aptx_enc_cfg_t);
@@ -798,9 +802,6 @@
     sample_rate_backup = aptx_bt_cfg->sampling_rate;
 #endif
 
-    if(aptx_bt_cfg == NULL)
-        return false;
-
     ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
     if (!ctl_enc_data) {
         ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identifed");
diff --git a/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c b/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c
index 5239a8e..636c38b 100644
--- a/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c
+++ b/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c
@@ -1,5 +1,5 @@
 /*--------------------------------------------------------------------------
-Copyright (c) 2010-2014, 2016-2017 The Linux Foundation. All rights reserved.
+Copyright (c) 2010-2014, 2016-2018 The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
@@ -271,14 +271,15 @@
     int status = 0;
 
     errno = 0;
-    ptr = (char *)malloc(strlen(input) + 1);
-    if (ptr == NULL) {
-        DEBUG_PRINT("Low memory\n");
+
+    if (input == NULL){
+        DEBUG_PRINT("No input is given\n");
         status = -1;
         goto exit;
     }
-    if (input == NULL){
-        DEBUG_PRINT("No input is given\n");
+    ptr = (char *)malloc(strlen(input) + 1);
+    if (ptr == NULL) {
+        DEBUG_PRINT("Low memory\n");
         status = -1;
         goto exit;
     }
@@ -320,6 +321,8 @@
             break;
     }
 exit:
+    if (ptr != NULL)
+        free(ptr);
     if (status != 0)
         exit(0);
     return value;
diff --git a/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c b/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c
index f9071dc..8053fa1 100644
--- a/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c
+++ b/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c
@@ -1,6 +1,6 @@
 
 /*--------------------------------------------------------------------------
-Copyright (c) 2010-2014, 2017 The Linux Foundation. All rights reserved.
+Copyright (c) 2010-2014, 2017-2018 The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
@@ -284,14 +284,15 @@
     int status = 0;
 
     errno = 0;
-    ptr = (char *)malloc(strlen(input) + 1);
-    if (ptr == NULL) {
-        DEBUG_PRINT("Low memory\n");
+
+    if (input == NULL){
+        DEBUG_PRINT("No input is given\n");
         status = -1;
         goto exit;
     }
-    if (input == NULL){
-        DEBUG_PRINT("No input is given\n");
+    ptr = (char *)malloc(strlen(input) + 1);
+    if (ptr == NULL) {
+        DEBUG_PRINT("Low memory\n");
         status = -1;
         goto exit;
     }
@@ -333,6 +334,8 @@
             break;
     }
 exit:
+    if (ptr != NULL)
+        free(ptr);
     if (status != 0)
         exit(0);
     return value;
diff --git a/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c b/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c
index 258fd2a..3aa6ff6 100644
--- a/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c
+++ b/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c
@@ -1,6 +1,6 @@
 
 /*--------------------------------------------------------------------------
-Copyright (c) 2010-2014, 2017 The Linux Foundation. All rights reserved.
+Copyright (c) 2010-2014, 2017-2018 The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
@@ -283,14 +283,15 @@
     int status = 0;
 
     errno = 0;
-    ptr = (char *)malloc(strlen(input) + 1);
-    if (ptr == NULL) {
-        DEBUG_PRINT("Low memory\n");
+
+    if (input == NULL){
+        DEBUG_PRINT("No input is given\n");
         status = -1;
         goto exit;
     }
-    if (input == NULL){
-        DEBUG_PRINT("No input is given\n");
+    ptr = (char *)malloc(strlen(input) + 1);
+    if (ptr == NULL) {
+        DEBUG_PRINT("Low memory\n");
         status = -1;
         goto exit;
     }
@@ -332,6 +333,8 @@
             break;
     }
 exit:
+    if (ptr != NULL)
+        free(ptr);
     if (status != 0)
         exit(0);
     return value;