diff mbox series

[01/10] of: Fix alias name length calculating error in API of_find_node_opts_by_path()

Message ID 20241206-of_core_fix-v1-1-dc28ed56bec3@quicinc.com
State Changes Requested
Headers show
Series of: fix bugs and improve codes | expand

Checks

Context Check Description
robh/checkpatch warning total: 0 errors, 1 warnings, 13 lines checked
robh/patch-applied fail build log

Commit Message

Zijun Hu Dec. 6, 2024, 12:52 a.m. UTC
From: Zijun Hu <quic_zijuhu@quicinc.com>

Alias name length calculated by of_find_node_opts_by_path() is wrong as
explained below:

Take "alias/serial@llc500:115200n8" as its @patch argument for an example
      ^    ^             ^
      0	   5		 19

The right length of alias 'alias' is 5, but the API results in 19 which is
obvious wrong.

The wrong length will cause finding device node failure for such paths.
Fix by using index of either '/' or ':' as the length who comes earlier.

Fixes: 106937e8ccdc ("of: fix handling of '/' in options for of_find_node_by_path()")
Cc: stable@vger.kernel.org
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
 drivers/of/base.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Rob Herring (Arm) Dec. 9, 2024, 1:24 p.m. UTC | #1
On Thu, Dec 5, 2024 at 6:53 PM Zijun Hu <zijun_hu@icloud.com> wrote:
>
> From: Zijun Hu <quic_zijuhu@quicinc.com>
>
> Alias name length calculated by of_find_node_opts_by_path() is wrong as
> explained below:
>
> Take "alias/serial@llc500:115200n8" as its @patch argument for an example
>       ^    ^             ^
>       0    5             19
>
> The right length of alias 'alias' is 5, but the API results in 19 which is
> obvious wrong.
>
> The wrong length will cause finding device node failure for such paths.
> Fix by using index of either '/' or ':' as the length who comes earlier.

Can you add a test case in the unittest for this.

>
> Fixes: 106937e8ccdc ("of: fix handling of '/' in options for of_find_node_by_path()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
> ---
>  drivers/of/base.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 7dc394255a0a14cd1aed02ec79c2f787a222b44c..9a9313183d1f1b61918fe7e6fa80c2726b099a1c 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -893,10 +893,10 @@ struct device_node *of_find_node_opts_by_path(const char *path, const char **opt
>         /* The path could begin with an alias */
>         if (*path != '/') {
>                 int len;
> -               const char *p = separator;
> +               const char *p = strchrnul(path, '/');
>
> -               if (!p)
> -                       p = strchrnul(path, '/');
> +               if (separator && separator < p)
> +                       p = separator;
>                 len = p - path;
>
>                 /* of_aliases must not be NULL */
>
> --
> 2.34.1
>
Zijun Hu Dec. 9, 2024, 1:31 p.m. UTC | #2
On 2024/12/9 21:24, Rob Herring wrote:
> On Thu, Dec 5, 2024 at 6:53 PM Zijun Hu <zijun_hu@icloud.com> wrote:
>>
>> From: Zijun Hu <quic_zijuhu@quicinc.com>
>>
>> Alias name length calculated by of_find_node_opts_by_path() is wrong as
>> explained below:
>>
>> Take "alias/serial@llc500:115200n8" as its @patch argument for an example
>>       ^    ^             ^
>>       0    5             19
>>
>> The right length of alias 'alias' is 5, but the API results in 19 which is
>> obvious wrong.
>>
>> The wrong length will cause finding device node failure for such paths.
>> Fix by using index of either '/' or ':' as the length who comes earlier.
> 
> Can you add a test case in the unittest for this.

sure. let me try to do it.

> 
>>
>> Fixes: 106937e8ccdc ("of: fix handling of '/' in options for of_find_node_by_path()")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
>> ---
>>  drivers/of/base.c | 6 +++---
>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/of/base.c b/drivers/of/base.c
>> index 7dc394255a0a14cd1aed02ec79c2f787a222b44c..9a9313183d1f1b61918fe7e6fa80c2726b099a1c 100644
>> --- a/drivers/of/base.c
>> +++ b/drivers/of/base.c
>> @@ -893,10 +893,10 @@ struct device_node *of_find_node_opts_by_path(const char *path, const char **opt
>>         /* The path could begin with an alias */
>>         if (*path != '/') {
>>                 int len;
>> -               const char *p = separator;
>> +               const char *p = strchrnul(path, '/');
>>
>> -               if (!p)
>> -                       p = strchrnul(path, '/');
>> +               if (separator && separator < p)
>> +                       p = separator;
>>                 len = p - path;
>>
>>                 /* of_aliases must not be NULL */
>>
>> --
>> 2.34.1
>>
diff mbox series

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 7dc394255a0a14cd1aed02ec79c2f787a222b44c..9a9313183d1f1b61918fe7e6fa80c2726b099a1c 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -893,10 +893,10 @@  struct device_node *of_find_node_opts_by_path(const char *path, const char **opt
 	/* The path could begin with an alias */
 	if (*path != '/') {
 		int len;
-		const char *p = separator;
+		const char *p = strchrnul(path, '/');
 
-		if (!p)
-			p = strchrnul(path, '/');
+		if (separator && separator < p)
+			p = separator;
 		len = p - path;
 
 		/* of_aliases must not be NULL */