diff mbox series

[v2,2/3] dm: compare full name in uclass_get_by_name

Message ID 20220117164919.v2.2.I85a4f70465cef5ef600f264706dca84c55c7efdb@changeid
State Changes Requested
Delegated to: Simon Glass
Headers show
Series sandbox: test: activate tests for the command LOG | expand

Commit Message

Patrick Delaunay Jan. 17, 2022, 3:49 p.m. UTC
Change uclass_get_by_name to use a strict string compare function
"strcmp" with the parameter 'name'.

This patch avoids issues when strlen(name)<strlen(uc_drv->name) as
the function uclass_get_by_name() no more use uclass_get_by_name_len(),
which limit the check with "strncmp" and length of name.

This problem is detected by the sandbox test for log filter:
in log_get_cat_by_name(), uclass_get_by_name("spi") = UCLASS_SPI_EMUL
for "spi_emul", it is not the expected result = UCLASS_SPI
for a search by name.
But it is the expected result for search with partial name
uclass_get_by_name_len("spi", 3).

Signed-off-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
---

(no changes since v1)

 drivers/core/uclass.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Simon Glass Jan. 27, 2022, 3:05 p.m. UTC | #1
Hi Patrick,

On Mon, 17 Jan 2022 at 08:49, Patrick Delaunay
<patrick.delaunay@foss.st.com> wrote:
>
> Change uclass_get_by_name to use a strict string compare function
> "strcmp" with the parameter 'name'.
>
> This patch avoids issues when strlen(name)<strlen(uc_drv->name) as
> the function uclass_get_by_name() no more use uclass_get_by_name_len(),
> which limit the check with "strncmp" and length of name.
>
> This problem is detected by the sandbox test for log filter:
> in log_get_cat_by_name(), uclass_get_by_name("spi") = UCLASS_SPI_EMUL
> for "spi_emul", it is not the expected result = UCLASS_SPI
> for a search by name.
> But it is the expected result for search with partial name
> uclass_get_by_name_len("spi", 3).
>
> Signed-off-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
> ---
>
> (no changes since v1)
>
>  drivers/core/uclass.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)

To save code space, could we avoid copying the function and instead
use len == -1 to mean to check the whole thing?

>
> diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
> index 336ea8d243..32b6cef167 100644
> --- a/drivers/core/uclass.c
> +++ b/drivers/core/uclass.c
> @@ -196,7 +196,16 @@ enum uclass_id uclass_get_by_name_len(const char *name, int len)
>
>  enum uclass_id uclass_get_by_name(const char *name)
>  {
> -       return uclass_get_by_name_len(name, strlen(name));
> +       int i;
> +
> +       for (i = 0; i < UCLASS_COUNT; i++) {
> +               struct uclass_driver *uc_drv = lists_uclass_lookup(i);
> +
> +               if (uc_drv && !strcmp(uc_drv->name, name))
> +                       return i;
> +       }
> +
> +       return UCLASS_INVALID;
>  }
>
>  int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
> --
> 2.25.1
>

Regards,
Simon
diff mbox series

Patch

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 336ea8d243..32b6cef167 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -196,7 +196,16 @@  enum uclass_id uclass_get_by_name_len(const char *name, int len)
 
 enum uclass_id uclass_get_by_name(const char *name)
 {
-	return uclass_get_by_name_len(name, strlen(name));
+	int i;
+
+	for (i = 0; i < UCLASS_COUNT; i++) {
+		struct uclass_driver *uc_drv = lists_uclass_lookup(i);
+
+		if (uc_drv && !strcmp(uc_drv->name, name))
+			return i;
+	}
+
+	return UCLASS_INVALID;
 }
 
 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)