audio: fix misc static errors

Fix accessing index out of array boundary.
Fix dereference of NULL pointer.
Fix use of uninitialized variable.

Change-Id: I77d7be8709bd1c1276ba1b9bb18321083a22c9c2
CRs-Fixed: 2284371
diff --git a/hal/audio_extn/a2dp.c b/hal/audio_extn/a2dp.c
index 0d0a48b..f3c6bb6 100644
--- a/hal/audio_extn/a2dp.c
+++ b/hal/audio_extn/a2dp.c
@@ -1255,53 +1255,54 @@
     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");
+        return false;
+    }
+
 #ifndef LINUX_ENABLED
     struct aptx_enc_cfg_t aptx_dsp_cfg;
     struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
-    if(a2dp.is_aptx_adaptive)
-       mixer_size = sizeof(struct aptx_ad_enc_cfg_t);
-    else {
+    if(a2dp.is_aptx_adaptive) {
+        mixer_size = sizeof(struct aptx_ad_enc_cfg_t);
+        ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
+        sample_rate_backup = aptx_ad_dsp_cfg.custom_cfg.sample_rate;
+    } else {
         mixer_size = sizeof(struct aptx_enc_cfg_t);
         sample_rate_backup = aptx_bt_cfg->default_cfg->sampling_rate;
+        ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
+    }
+    if (ret) {
+        is_configured = false;
+        goto fail;
+    }
+
+    if(a2dp.is_aptx_adaptive) {
+        ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
+                              mixer_size);
+    } else {
+        ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
+                              mixer_size);
     }
 #else
     struct custom_enc_cfg_t aptx_dsp_cfg;
     mixer_size = sizeof(struct custom_enc_cfg_t);
     sample_rate_backup = aptx_bt_cfg->sampling_rate;
-#endif
-
-    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");
-        is_configured = false;
-        goto fail;
-    }
-
-#ifndef LINUX_ENABLED
-    if(a2dp.is_aptx_adaptive) {
-        ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
-        sample_rate_backup = aptx_ad_dsp_cfg.custom_cfg.sample_rate;
-    } else
-        ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
-#else
     ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
-#endif
-
     if (ret) {
         is_configured = false;
         goto fail;
     }
-    if(a2dp.is_aptx_adaptive)
-        ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
-                             mixer_size);
-    else
-        ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
-                              mixer_size);
+    ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
+                          mixer_size);
+#endif
     if (ret != 0) {
         ALOGE("%s: Failed to set APTX encoder config", __func__);
         is_configured = false;
         goto fail;
     }
+
     ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
                                             MIXER_ENC_BIT_FORMAT);
     if (!ctrl_bit_format) {
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index fa36879..624c875 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -897,6 +897,11 @@
     ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
 
     usecase = get_usecase_from_list(adev, in->usecase);
+    if (usecase == NULL) {
+        ALOGE("%s: Could not find the usecase (%d) in the list",
+              __func__, in->usecase);
+        return -EINVAL;
+    }
 
     ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
     if (ret < 0) {
diff --git a/hal/platform_info.c b/hal/platform_info.c
index dca577b..e35b7c4 100644
--- a/hal/platform_info.c
+++ b/hal/platform_info.c
@@ -639,7 +639,7 @@
         goto done;
     }
     microphone.num_frequency_responses = atoi(attr[curIdx++]);
-    if (microphone.num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
+    if (microphone.num_frequency_responses >= AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
         ALOGE("%s: num_frequency_responses is too large", __func__);
         goto done;
     }
@@ -653,7 +653,7 @@
         uint32_t num_frequencies = 0;
         while (token) {
             microphone.frequency_responses[0][num_frequencies++] = atof(token);
-            if (num_frequencies > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
+            if (num_frequencies >= AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
                 ALOGE("%s: num %u of frequency is too large", __func__, num_frequencies);
                 goto done;
             }
@@ -668,7 +668,7 @@
         uint32_t num_responses = 0;
         while (token) {
             microphone.frequency_responses[1][num_responses++] = atof(token);
-            if (num_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
+            if (num_responses >= AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
                 ALOGE("%s: num %u of response is too large", __func__, num_responses);
                 goto done;
             }
@@ -724,13 +724,13 @@
         uint32_t idx = 0;
         while (token) {
             orientation[idx++] = atof(token);
-            if (idx > 3) {
+            if (idx >= 3) {
                 ALOGE("%s: orientation invalid", __func__);
                 goto done;
             }
             token = strtok_r(NULL, " ", &context);
         }
-        if (idx != 3) {
+        if (idx != 2) {
             ALOGE("%s: orientation invalid", __func__);
             goto done;
         }
@@ -754,13 +754,13 @@
         uint32_t idx = 0;
         while (token) {
             geometric_location[idx++] = atof(token);
-            if (idx > 3) {
+            if (idx >= 3) {
                 ALOGE("%s: geometric_location invalid", __func__);
                 goto done;
             }
             token = strtok_r(NULL, " ", &context);
         }
-        if (idx != 3) {
+        if (idx != 2) {
             ALOGE("%s: geometric_location invalid", __func__);
             goto done;
         }