Merge "primary-hal: Fix stability issues"
diff --git a/hal/acdb.c b/hal/acdb.c
index 4b1ef31..5284228 100644
--- a/hal/acdb.c
+++ b/hal/acdb.c
@@ -5,7 +5,7 @@
  * 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.
+ * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  * SPDX-License-Identifier: BSD-3-Clause-Clear
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -248,14 +248,14 @@
             free(key_info);
         }
 
+        if (my_data->acdb_init_data.snd_card_name)
+            free(my_data->acdb_init_data.snd_card_name);
+
         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();
         }
diff --git a/hal/audio_extn/spkr_protection.c b/hal/audio_extn/spkr_protection.c
index c7f5726..31bf94c 100644
--- a/hal/audio_extn/spkr_protection.c
+++ b/hal/audio_extn/spkr_protection.c
@@ -30,7 +30,7 @@
 /*
 * Changes from Qualcomm Innovation Center are provided under the following license:
 *
-* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+* Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted (subject to the limitations in the
@@ -1370,8 +1370,8 @@
                         }
                        thermal_fd = open(wsa_path, O_RDONLY);
                        if (thermal_fd > 0) {
-                           if ((ret = read(thermal_fd, buf, sizeof(buf))) >= 0) {
-                               buf[ret-1] = '\0';
+                           if ((ret = read(thermal_fd, buf, sizeof(buf)-1)) >= 0) {
+                               buf[ret] = '\0';
                                t0_spk_1 = atoi(buf);
                            }
                            else
@@ -1406,8 +1406,8 @@
                         }
                         thermal_fd = open(wsa_path, O_RDONLY);
                         if (thermal_fd > 0) {
-                           if ((ret = read(thermal_fd, buf, sizeof(buf))) >= 0) {
-                               buf[ret-1] = '\0';
+                           if ((ret = read(thermal_fd, buf, sizeof(buf)-1)) >= 0) {
+                               buf[ret] = '\0';
                                t0_spk_2 = atoi(buf);
                            }
                            else
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 94c75a8..860e38c 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -35,7 +35,7 @@
  * limitations under the License.
  *
  * Changes from Qualcomm Innovation Center are provided under the following license:
- * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  * SPDX-License-Identifier: BSD-3-Clause-Clear
  *
  */
@@ -10817,8 +10817,13 @@
         s_info = hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
         if (s_info == NULL) {
             ALOGE("%s: Failed to obtain stream info", __func__);
-            if (new_patch)
+            if (new_patch) {
+
+                if(p_info->patch)
+                    free(p_info->patch);
+
                 free(p_info);
+            }
             pthread_mutex_unlock(&adev->lock);
             ret = -EINVAL;
             goto done;
@@ -10839,8 +10844,13 @@
         if (ret < 0) {
             pthread_mutex_lock(&adev->lock);
             s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
-            if (new_patch)
+            if (new_patch) {
+
+                if(p_info->patch)
+                    free(p_info->patch);
+
                 free(p_info);
+            }
             pthread_mutex_unlock(&adev->lock);
             ALOGE("%s: Stream routing failed for io_handle %d", __func__, io_handle);
             goto done;