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...
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