linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ASoC: rt298: Variable "val" and "buf" in rt298_jack_detect() could be uninitialized
@ 2019-01-04 21:52 Yizhuo
  2019-01-07 16:28 ` Mark Brown
  0 siblings, 1 reply; 2+ messages in thread
From: Yizhuo @ 2019-01-04 21:52 UTC (permalink / raw)
  Cc: csong, zhiyunq, Yizhuo, Bard Liao, Oder Chiou, Liam Girdwood,
	Mark Brown, Jaroslav Kysela, Takashi Iwai, alsa-devel,
	linux-kernel

In function rt298_jack_detect(), local variable "val" and "buf"
are supposed to get initialized by regmap_read(). However, they
 could be leave uninitialized if regmap_read() returns -EINVAL.
Those two variables are used in control flow or update the argument,
which could lead to undefined behavior and thus unsafe.

Signed-off-by: Yizhuo <yzhai003@ucr.edu>
---
 sound/soc/codecs/rt298.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/sound/soc/codecs/rt298.c b/sound/soc/codecs/rt298.c
index ce963768449f..7f74349c17f3 100644
--- a/sound/soc/codecs/rt298.c
+++ b/sound/soc/codecs/rt298.c
@@ -223,6 +223,7 @@ static int rt298_jack_detect(struct rt298_priv *rt298, bool *hp, bool *mic)
 {
 	struct snd_soc_dapm_context *dapm;
 	unsigned int val, buf;
+	int ret = 0;
 
 	*hp = false;
 	*mic = false;
@@ -233,7 +234,10 @@ static int rt298_jack_detect(struct rt298_priv *rt298, bool *hp, bool *mic)
 	dapm = snd_soc_codec_get_dapm(rt298->codec);
 
 	if (rt298->pdata.cbj_en) {
-		regmap_read(rt298->regmap, RT298_GET_HP_SENSE, &buf);
+		ret = regmap_read(rt298->regmap, RT298_GET_HP_SENSE, &buf);
+		if (ret)
+			return ret;
+
 		*hp = buf & 0x80000000;
 		if (*hp == rt298->is_hp_in)
 			return -1;
@@ -260,16 +264,19 @@ static int rt298_jack_detect(struct rt298_priv *rt298, bool *hp, bool *mic)
 			regmap_update_bits(rt298->regmap,
 				RT298_CBJ_CTRL1, 0xfcc0, 0xd400);
 			msleep(300);
-			regmap_read(rt298->regmap, RT298_CBJ_CTRL2, &val);
-
+			ret = regmap_read(rt298->regmap, RT298_CBJ_CTRL2, &val);
+			if (ret)
+				return ret;
 			if (0x0070 == (val & 0x0070)) {
 				*mic = true;
 			} else {
 				regmap_update_bits(rt298->regmap,
 					RT298_CBJ_CTRL1, 0xfcc0, 0xe400);
 				msleep(300);
-				regmap_read(rt298->regmap,
+				ret = regmap_read(rt298->regmap,
 					RT298_CBJ_CTRL2, &val);
+				if (ret)
+					return ret;
 				if (0x0070 == (val & 0x0070))
 					*mic = true;
 				else
@@ -285,9 +292,14 @@ static int rt298_jack_detect(struct rt298_priv *rt298, bool *hp, bool *mic)
 				RT298_CBJ_CTRL1, 0x0400, 0x0000);
 		}
 	} else {
-		regmap_read(rt298->regmap, RT298_GET_HP_SENSE, &buf);
+		ret = regmap_read(rt298->regmap, RT298_GET_HP_SENSE, &buf);
+		if (ret)
+			return ret;
+
 		*hp = buf & 0x80000000;
-		regmap_read(rt298->regmap, RT298_GET_MIC1_SENSE, &buf);
+		ret = regmap_read(rt298->regmap, RT298_GET_MIC1_SENSE, &buf);
+		if (ret)
+			return ret;
 		*mic = buf & 0x80000000;
 	}
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ASoC: rt298: Variable "val" and "buf" in rt298_jack_detect() could be uninitialized
  2019-01-04 21:52 [PATCH] ASoC: rt298: Variable "val" and "buf" in rt298_jack_detect() could be uninitialized Yizhuo
@ 2019-01-07 16:28 ` Mark Brown
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Brown @ 2019-01-07 16:28 UTC (permalink / raw)
  To: Yizhuo
  Cc: csong, zhiyunq, Bard Liao, Oder Chiou, Liam Girdwood,
	Jaroslav Kysela, Takashi Iwai, alsa-devel, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 848 bytes --]

On Fri, Jan 04, 2019 at 01:52:53PM -0800, Yizhuo wrote:

>  {
>  	struct snd_soc_dapm_context *dapm;
>  	unsigned int val, buf;
> +	int ret = 0;

It's bad practice to just initailize like this without a reason - it
tends to just mask actual cases where we miss error handling by ensuring
that it's always initialized.

>  			if (0x0070 == (val & 0x0070)) {
>  				*mic = true;
>  			} else {
>  				regmap_update_bits(rt298->regmap,
>  					RT298_CBJ_CTRL1, 0xfcc0, 0xe400);
>  				msleep(300);
> -				regmap_read(rt298->regmap,
> +				ret = regmap_read(rt298->regmap,
>  					RT298_CBJ_CTRL2, &val);
> +				if (ret)
> +					return ret;

We've started doing some writes to the device (which you've not added
checks for) here but then if the read fails we just bomb out with an
error code - are you sure that none of the writes need to be reverted?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-01-07 16:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-04 21:52 [PATCH] ASoC: rt298: Variable "val" and "buf" in rt298_jack_detect() could be uninitialized Yizhuo
2019-01-07 16:28 ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).