Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Trevor Wu (吳文良)" <Trevor.Wu@mediatek.com>
To: "lgirdwood@gmail.com" <lgirdwood@gmail.com>,
	"matthias.bgg@gmail.com" <matthias.bgg@gmail.com>,
	"angelogioacchino.delregno@collabora.com"
	<angelogioacchino.delregno@collabora.com>,
	"broonie@kernel.org" <broonie@kernel.org>,
	"tiwai@suse.com" <tiwai@suse.com>,
	"perex@perex.cz" <perex@perex.cz>
Cc: "linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-mediatek@lists.infradead.org"
	<linux-mediatek@lists.infradead.org>,
	"alsa-devel@alsa-project.org" <alsa-devel@alsa-project.org>
Subject: Re: [PATCH 1/3] ASoC: mediatek: mt8188-mt6359: support dynamic pinctrl
Date: Thu, 27 Jul 2023 02:03:01 +0000	[thread overview]
Message-ID: <72868ec18a5b97f62c70dddb1ea8b529c8dcd087.camel@mediatek.com> (raw)
In-Reply-To: <82fb1e56-b271-0ba9-c23d-0c066dc51ef6@collabora.com>

On Wed, 2023-07-26 at 13:54 +0200, AngeloGioacchino Del Regno wrote:

..snip..

> > > > > > 
> > > > > > @@ -306,6 +312,18 @@ static int
> > > > > > mt8188_mt6359_mtkaif_calibration(struct snd_soc_pcm_runtime
> > > > > > *rtd)
> > > > > >     		return 0;
> > > > > >     	}
> > > > > >     
> > > > > > +	for_each_card_widgets(rtd->card, w) {
> > > > > > +		if (!strcmp(w->name, "MTKAIF_PIN")) {
> > > > > 
> > > > > if (strncmp(w->name, "MTKAIF_PIN", strlen(w->name) == 0) {
> > > > > 	pin_w = w;
> > > > > 	break;
> > > > > }
> > > > > 
> > > > > That's safer.
> > > > > 
> > > > 
> > > > If w->name is MTKAIF, the strncmp expression will return 0.
> > > > However,
> > > > the result is not expected. I prefer to keep strcmp here.
> > > > 
> > > 
> > > You could also do, instead
> > > 
> > > if (strncmp(w->name, "MTKAIF_PIN", strlen("MTKAIF_PIN") == 0))
> > > 
> > > ...solving your concern.
> > > 
> > > 
> > 
> >  From my understanding, strncmp is utilized to determine a string
> > begins
> > with a particular prefix while strcmp is used to compare a whole
> > string. In this scenario, I wish to verify if the widget name is
> > exactly 'MTKAIF_PIN', so I believe using strcmp would be more
> > appropriate.
> > 
> > Using either strlen(w->name) or strlen("MTKAIF_PIN") may lead to
> > incorrect results when w->name is either MTKAIF or MTKAIF_PIN1.
> > 
> > Thanks,
> > Trevor
> 
> strcmp() and strncmp() are the same; except strncmp() compares *at
> most* `n` bytes,
> where `n` is my `strlen("MTKAIF_PIN")`.
> 
>  From Linux man pages....
> 
https://urldefense.com/v3/__https://www.man7.org/linux/man-pages/man3/strcmp.3.html__;!!CTRNKA9wMg0ARbw!n-X-ckbkVYv4cbrhpwb2f7NH6sQkxx1czmCU2-q5BtSOhQU0C68wj9JNcu47YfbYeTpVEzjYQVXGuQb5ulpeWwKjnVMdZpg$ 
>  
> 
> 

Hi Angelo,

My concern is that strncmp() compares at most `n` bytes, where `n` is
the length of the string 'MTKAIF_PIN'. For instance, if both
'MTKAIF_PIN' and 'MTKAIF_PINMUX' exist in the widget list, they will
both enter execute_something() function below when executing this code:
 
if (strncmp(w->name, "MTKAIF_PIN", strlen("MTKAIF_PIN")) == 0) { 	
 execute_something(); 
}. 

This is not my expected scenario. To prevent this problem, we can use
strcmp() instead of strncmp(). strcmp() compares two strings until it
finds a difference or reaches the end of one of them. Therefore, it
will compare the entire string 'MTKAIF_PIN' with w->name and make sure
that only do execute_something() when w->names is the same as
'MTKAIF_PIN'.

Thanks,
Trevor


  reply	other threads:[~2023-07-27  2:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-25  3:53 [PATCH 0/3] ASoC: mt8188-mt6359: add SOF support Trevor Wu
2023-07-25  3:53 ` [PATCH 1/3] ASoC: mediatek: mt8188-mt6359: support dynamic pinctrl Trevor Wu
2023-07-25  7:06   ` AngeloGioacchino Del Regno
2023-07-26  2:19     ` Trevor Wu (吳文良)
2023-07-26  6:43       ` AngeloGioacchino Del Regno
2023-07-26 11:36         ` Trevor Wu (吳文良)
2023-07-26 11:54           ` AngeloGioacchino Del Regno
2023-07-27  2:03             ` Trevor Wu (吳文良) [this message]
2023-07-31 13:35   ` AngeloGioacchino Del Regno
2023-07-25  3:53 ` [PATCH 2/3] ASoC: mediatek: common: revise SOF common code Trevor Wu
2023-07-25  3:53 ` [PATCH 3/3] ASoC: mediatek: mt8188-mt6359: add SOF support Trevor Wu
2023-07-25  7:11   ` AngeloGioacchino Del Regno
2023-07-26  2:25     ` Trevor Wu (吳文良)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=72868ec18a5b97f62c70dddb1ea8b529c8dcd087.camel@mediatek.com \
    --to=trevor.wu@mediatek.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox