diff mbox series

[02/13] selftests: ublk: fix ublk_find_tgt()

Message ID 20250407131526.1927073-3-ming.lei@redhat.com (mailing list archive)
State New
Headers show
Series ublk: one driver bug fix and selftest change | expand

Commit Message

Ming Lei April 7, 2025, 1:15 p.m. UTC
Bounds check for iterator variable `i` is missed, so add it and fix
ublk_find_tgt().

Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 tools/testing/selftests/ublk/kublk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Johannes Thumshirn April 8, 2025, 6:05 a.m. UTC | #1
On 07.04.25 15:18, Ming Lei wrote:
> Bounds check for iterator variable `i` is missed, so add it and fix
> ublk_find_tgt().
> 
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
>   tools/testing/selftests/ublk/kublk.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests/ublk/kublk.c
> index 91c282bc7674..5c03c776426f 100644
> --- a/tools/testing/selftests/ublk/kublk.c
> +++ b/tools/testing/selftests/ublk/kublk.c
> @@ -14,13 +14,12 @@ static const struct ublk_tgt_ops *tgt_ops_list[] = {
>   
>   static const struct ublk_tgt_ops *ublk_find_tgt(const char *name)
>   {
> -	const struct ublk_tgt_ops *ops;
>   	int i;
>   
>   	if (name == NULL)
>   		return NULL;
>   
> -	for (i = 0; sizeof(tgt_ops_list) / sizeof(ops); i++)
> +	for (i = 0; i < sizeof(tgt_ops_list) / sizeof(tgt_ops_list[0]); i++)

Why not

	for (i=0; i < ARRAY_SIZE(tgt_ops_list); i++)
Ming Lei April 12, 2025, 1:25 a.m. UTC | #2
On Tue, Apr 8, 2025 at 2:05 PM Johannes Thumshirn
<Johannes.Thumshirn@wdc.com> wrote:
>
> On 07.04.25 15:18, Ming Lei wrote:
> > Bounds check for iterator variable `i` is missed, so add it and fix
> > ublk_find_tgt().
> >
> > Signed-off-by: Ming Lei <ming.lei@redhat.com>
> > ---
> >   tools/testing/selftests/ublk/kublk.c | 3 +--
> >   1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests/ublk/kublk.c
> > index 91c282bc7674..5c03c776426f 100644
> > --- a/tools/testing/selftests/ublk/kublk.c
> > +++ b/tools/testing/selftests/ublk/kublk.c
> > @@ -14,13 +14,12 @@ static const struct ublk_tgt_ops *tgt_ops_list[] = {
> >
> >   static const struct ublk_tgt_ops *ublk_find_tgt(const char *name)
> >   {
> > -     const struct ublk_tgt_ops *ops;
> >       int i;
> >
> >       if (name == NULL)
> >               return NULL;
> >
> > -     for (i = 0; sizeof(tgt_ops_list) / sizeof(ops); i++)
> > +     for (i = 0; i < sizeof(tgt_ops_list) / sizeof(tgt_ops_list[0]); i++)
>
> Why not
>
>         for (i=0; i < ARRAY_SIZE(tgt_ops_list); i++)

It is because ARRAY_SIZE isn't defined as one generic helper for
test code.

But it is more readable, so I will add it in ublk header.

Thanks,
diff mbox series

Patch

diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests/ublk/kublk.c
index 91c282bc7674..5c03c776426f 100644
--- a/tools/testing/selftests/ublk/kublk.c
+++ b/tools/testing/selftests/ublk/kublk.c
@@ -14,13 +14,12 @@  static const struct ublk_tgt_ops *tgt_ops_list[] = {
 
 static const struct ublk_tgt_ops *ublk_find_tgt(const char *name)
 {
-	const struct ublk_tgt_ops *ops;
 	int i;
 
 	if (name == NULL)
 		return NULL;
 
-	for (i = 0; sizeof(tgt_ops_list) / sizeof(ops); i++)
+	for (i = 0; i < sizeof(tgt_ops_list) / sizeof(tgt_ops_list[0]); i++)
 		if (strcmp(tgt_ops_list[i]->name, name) == 0)
 			return tgt_ops_list[i];
 	return NULL;