audio: hal: Fix for memory leaks.

Change-Id: I3206b5a05869f3eb2c4dffaaf44ebf4ce0f08b4a
diff --git a/hal/acdb.c b/hal/acdb.c
index 8dfcc2c..4b1ef31 100644
--- a/hal/acdb.c
+++ b/hal/acdb.c
@@ -4,6 +4,10 @@
  *
  * Copyright (C) 2013 The Android Open Source Project
  *
+ * Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -243,6 +247,19 @@
             list_remove(node);
             free(key_info);
         }
+
+        if (result < 0) {
+
+            if (snd_card_name)
+                free((void *)snd_card_name);
+
+            if (my_data->acdb_init_data.snd_card_name)
+                free(my_data->acdb_init_data.snd_card_name);
+
+            if (my_data)
+                platform_info_deinit();
+        }
+
         free(my_data);
     }
 
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 2407cfc..554b624 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -1909,6 +1909,7 @@
 
     snd_device_t d1 = uc->out_snd_device;
     snd_device_t d2 = new_snd_device;
+    snd_device_t ret = 0;
 
     list_init(&a1);
     list_init(&a2);
@@ -1939,28 +1940,39 @@
                       __func__,
                       list_length(&a1) > 1 ? d1 : d2);
             }
+            ret = d2;
             goto end;
         }
 
         if (platform_check_backends_match(d3[0], d3[1])) {
-            return d2; // case 5
+            ret = d2;
+            goto end; // case 5
         } else {
             if ((list_length(&a1) > 1) && (list_length(&a2) > 1) &&
-                 platform_check_backends_match(d1, d2))
-                return d2; //case 9
-            if (list_length(&a1) > 1)
-                return d1; //case 7
+                 platform_check_backends_match(d1, d2)) {
+                    ret = d2;
+                    goto end; //case 9
+                 }
+            if (list_length(&a1) > 1) {
+                ret = d1;
+                goto end; //case 7
+            }
             // check if d1 is related to any of d3's
-            if (d1 == d3[0] || d1 == d3[1])
-                return d1; // case 1
-            else
-                return d3[1]; // case 8
+            if (d1 == d3[0] || d1 == d3[1]) {
+                ret = d1;
+                goto end; // case 1
+            } else {
+                 ret = d3[1];
+                goto end; // case 8
+            }
         }
     } else {
         if (platform_check_backends_match(d1, d2)) {
-            return d2; // case 2, 4
+           ret = d2;
+           goto end; // case 2, 4
         } else {
-            return d1; // case 6, 3
+            ret = d1;
+            goto end; // case 6, 3
         }
     }
 
@@ -1968,7 +1980,9 @@
     clear_devices(&a2);
 
 end:
-    return d2; // return whatever was calculated before.
+    clear_devices(&a1);
+    clear_devices(&a2);
+    return ret; // return whatever was calculated before.
 }
 
 static void check_usecases_codec_backend(struct audio_device *adev,
diff --git a/hal/msm8974/hw_info.c b/hal/msm8974/hw_info.c
index 178c89c..ef6c790 100644
--- a/hal/msm8974/hw_info.c
+++ b/hal/msm8974/hw_info.c
@@ -1,6 +1,10 @@
 /*
  * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  *
+ * Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
@@ -1001,7 +1005,7 @@
 {
     struct hardware_info *my_data = (struct hardware_info*) hw_info;
 
-    if(!my_data)
+    if (my_data)
         free(my_data);
 }
 
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 0736daf..c92dfe4 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -2729,8 +2729,6 @@
     hw_interface_table[SND_DEVICE_IN_CAPTURE_FM] = strdup("SLIMBUS_8_TX");
     hw_interface_table[SND_DEVICE_IN_AANC_HANDSET_MIC] = strdup("SLIMBUS_0_TX");
     hw_interface_table[SND_DEVICE_IN_QUAD_MIC] = strdup("SLIMBUS_0_TX");
-    hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
-    hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
     hw_interface_table[SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = strdup("SLIMBUS_4_TX");
     hw_interface_table[SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_1] = strdup("SLIMBUS_4_TX");
     hw_interface_table[SND_DEVICE_IN_CAPTURE_VI_FEEDBACK_MONO_2] = strdup("SLIMBUS_4_TX");
diff --git a/hal/platform_api.h b/hal/platform_api.h
index d789494..850f525 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -293,6 +293,7 @@
 
 /* From platform_info.c */
 int platform_info_init(const char *filename, void *, caller_t);
+void platform_info_deinit();
 
 void platform_snd_card_update(void *platform, card_status_t scard_status);
 
diff --git a/hal/platform_info.c b/hal/platform_info.c
index 0dd7c8c..80b6e0d 100644
--- a/hal/platform_info.c
+++ b/hal/platform_info.c
@@ -1688,7 +1688,9 @@
 
     my_data.caller = caller_type;
     my_data.platform = platform;
-    my_data.kvpairs = str_parms_create();
+
+    if (!my_data.kvpairs)
+        my_data.kvpairs = str_parms_create();
 
     XML_SetElementHandler(parser, start_tag, end_tag);
 
@@ -1727,3 +1729,9 @@
     pthread_mutex_unlock(&parser_lock);
     return ret;
 }
+
+void platform_info_deinit()
+{
+    if (my_data.kvpairs)
+        str_parms_destroy(my_data.kvpairs);
+}