From cc1ce15fb74f05e78e09e3a1a504e2ad5ea95c4e Mon Sep 17 00:00:00 2001 From: Junkai Cai Date: Sat, 21 Aug 2021 15:07:41 -0700 Subject: [PATCH] ASoC: codecs: add null check before use Add null check for swr dmic component and swr_dmic_priv before use. Add array bounds check for slave port index before accessing the array. Change-Id: I6f1976e98e41e513a9a9db83d4e804a5cb1fe79b Signed-off-by: Junkai Cai --- asoc/codecs/swr-dmic.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/asoc/codecs/swr-dmic.c b/asoc/codecs/swr-dmic.c index 0729e56e3b..367f73a708 100644 --- a/asoc/codecs/swr-dmic.c +++ b/asoc/codecs/swr-dmic.c @@ -132,9 +132,20 @@ static int swr_dmic_tx_master_port_get(struct snd_kcontrol *kcontrol, { struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); - struct swr_dmic_priv *swr_dmic = snd_soc_component_get_drvdata(component); + struct swr_dmic_priv *swr_dmic = NULL; int ret = 0; - int slave_port_idx; + unsigned int slave_port_idx = SWR_DMIC_MAX_PORTS; + + if (NULL == component) { + pr_err("%s: swr dmic component is NULL\n", __func__); + return -EINVAL; + } + + swr_dmic = snd_soc_component_get_drvdata(component); + if (NULL == swr_dmic) { + pr_err("%s: swr_dmic_priv is NULL\n", __func__); + return -EINVAL; + } ret = swr_dmic_tx_get_slave_port_type_idx(kcontrol->id.name, &slave_port_idx); @@ -143,6 +154,11 @@ static int swr_dmic_tx_master_port_get(struct snd_kcontrol *kcontrol, return ret; } + if (slave_port_idx >= SWR_DMIC_MAX_PORTS) { + pr_err("%s: invalid slave port id\n", __func__); + return -EINVAL; + } + ucontrol->value.integer.value[0] = swr_dmic_get_master_port_val( swr_dmic->tx_master_port_map[slave_port_idx]); @@ -158,9 +174,21 @@ static int swr_dmic_tx_master_port_put(struct snd_kcontrol *kcontrol, { struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); - struct swr_dmic_priv *swr_dmic = snd_soc_component_get_drvdata(component); + struct swr_dmic_priv *swr_dmic = NULL; int ret = 0; - unsigned int slave_port_idx = SWR_DMIC_MAX_PORTS, idx = 0; + unsigned int slave_port_idx = SWR_DMIC_MAX_PORTS; + unsigned int idx = 0; + + if (NULL == component) { + pr_err("%s: swr dmic component is NULL\n", __func__); + return -EINVAL; + } + + swr_dmic = snd_soc_component_get_drvdata(component); + if (NULL == swr_dmic) { + pr_err("%s: swr_dmic_priv is NULL\n", __func__); + return -EINVAL; + } ret = swr_dmic_tx_get_slave_port_type_idx(kcontrol->id.name, &slave_port_idx); @@ -169,6 +197,11 @@ static int swr_dmic_tx_master_port_put(struct snd_kcontrol *kcontrol, return ret; } + if (slave_port_idx >= SWR_DMIC_MAX_PORTS) { + pr_err("%s: invalid slave port id\n", __func__); + return -EINVAL; + } + idx = ucontrol->value.enumerated.item[0]; if (idx < 0 || idx >= ARRAY_SIZE(swr_master_channel_map)) return -EINVAL;