diff mbox series

[v2] lib: sbi: Fix bug in strncmp function

Message ID 20210728161535.421-1-Dd_nirvana@sjtu.edu.cn
State Accepted
Headers show
Series [v2] lib: sbi: Fix bug in strncmp function | expand

Commit Message

杜东 July 28, 2021, 4:15 p.m. UTC
No need to compare characters when the count turns to 0.
Fix the issue in sbi_strncmp.

Signed-off-by: Dong Du <Dd_nirvana@sjtu.edu.cn>
---
 lib/sbi/sbi_string.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Bin Meng Aug. 5, 2021, 8:39 a.m. UTC | #1
Hi Dong,

On Thu, Jul 29, 2021 at 12:23 AM Dong Du <Dd_nirvana@sjtu.edu.cn> wrote:
>
> No need to compare characters when the count turns to 0.
> Fix the issue in sbi_strncmp.
>
> Signed-off-by: Dong Du <Dd_nirvana@sjtu.edu.cn>
> ---
>  lib/sbi/sbi_string.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c
> index 7805ba4..c87bce9 100644
> --- a/lib/sbi/sbi_string.c
> +++ b/lib/sbi/sbi_string.c
> @@ -33,6 +33,10 @@ int sbi_strncmp(const char *a, const char *b, size_t count)
>         for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
>                 ;
>
> +       /* No difference till the end */
> +       if (!count)
> +               return 0;
> +
>         return *a - *b;
>  }
>

Your original patch is also needed: "strncmp should return 0 when the
count is 0"

Regards,
Bin
Andreas Schwab Aug. 5, 2021, 8:46 a.m. UTC | #2
On Aug 05 2021, Bin Meng wrote:

> Hi Dong,
>
> On Thu, Jul 29, 2021 at 12:23 AM Dong Du <Dd_nirvana@sjtu.edu.cn> wrote:
>>
>> No need to compare characters when the count turns to 0.
>> Fix the issue in sbi_strncmp.
>>
>> Signed-off-by: Dong Du <Dd_nirvana@sjtu.edu.cn>
>> ---
>>  lib/sbi/sbi_string.c | 4 ++++
>>  1 file changed, 4 insertions(+)
>>
>> diff --git a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c
>> index 7805ba4..c87bce9 100644
>> --- a/lib/sbi/sbi_string.c
>> +++ b/lib/sbi/sbi_string.c
>> @@ -33,6 +33,10 @@ int sbi_strncmp(const char *a, const char *b, size_t count)
>>         for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
>>                 ;
>>
>> +       /* No difference till the end */
>> +       if (!count)
>> +               return 0;
>> +
>>         return *a - *b;
>>  }
>>
>
> Your original patch is also needed: "strncmp should return 0 when the
> count is 0"

Which this patch duly implements.

Andreas.
Bin Meng Aug. 5, 2021, 9:18 a.m. UTC | #3
On Thu, Aug 5, 2021 at 4:46 PM Andreas Schwab <schwab@linux-m68k.org> wrote:
>
> On Aug 05 2021, Bin Meng wrote:
>
> > Hi Dong,
> >
> > On Thu, Jul 29, 2021 at 12:23 AM Dong Du <Dd_nirvana@sjtu.edu.cn> wrote:
> >>
> >> No need to compare characters when the count turns to 0.
> >> Fix the issue in sbi_strncmp.
> >>
> >> Signed-off-by: Dong Du <Dd_nirvana@sjtu.edu.cn>
> >> ---
> >>  lib/sbi/sbi_string.c | 4 ++++
> >>  1 file changed, 4 insertions(+)
> >>
> >> diff --git a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c
> >> index 7805ba4..c87bce9 100644
> >> --- a/lib/sbi/sbi_string.c
> >> +++ b/lib/sbi/sbi_string.c
> >> @@ -33,6 +33,10 @@ int sbi_strncmp(const char *a, const char *b, size_t count)
> >>         for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
> >>                 ;
> >>
> >> +       /* No difference till the end */
> >> +       if (!count)
> >> +               return 0;
> >> +
> >>         return *a - *b;
> >>  }
> >>
> >
> > Your original patch is also needed: "strncmp should return 0 when the
> > count is 0"
>
> Which this patch duly implements.

Ah, yes. Maybe we should update the comment to make it clear.

Anyway:
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Anup Patel Aug. 7, 2021, 10:18 a.m. UTC | #4
On Thu, Aug 5, 2021 at 2:49 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> On Thu, Aug 5, 2021 at 4:46 PM Andreas Schwab <schwab@linux-m68k.org> wrote:
> >
> > On Aug 05 2021, Bin Meng wrote:
> >
> > > Hi Dong,
> > >
> > > On Thu, Jul 29, 2021 at 12:23 AM Dong Du <Dd_nirvana@sjtu.edu.cn> wrote:
> > >>
> > >> No need to compare characters when the count turns to 0.
> > >> Fix the issue in sbi_strncmp.
> > >>
> > >> Signed-off-by: Dong Du <Dd_nirvana@sjtu.edu.cn>
> > >> ---
> > >>  lib/sbi/sbi_string.c | 4 ++++
> > >>  1 file changed, 4 insertions(+)
> > >>
> > >> diff --git a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c
> > >> index 7805ba4..c87bce9 100644
> > >> --- a/lib/sbi/sbi_string.c
> > >> +++ b/lib/sbi/sbi_string.c
> > >> @@ -33,6 +33,10 @@ int sbi_strncmp(const char *a, const char *b, size_t count)
> > >>         for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
> > >>                 ;
> > >>
> > >> +       /* No difference till the end */
> > >> +       if (!count)
> > >> +               return 0;
> > >> +
> > >>         return *a - *b;
> > >>  }
> > >>
> > >
> > > Your original patch is also needed: "strncmp should return 0 when the
> > > count is 0"
> >
> > Which this patch duly implements.
>
> Ah, yes. Maybe we should update the comment to make it clear.
>
> Anyway:
> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

Looks good to me.

Reviewed-by: Anup Patel <anup.patel@wdc.com>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

>
> --
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
diff mbox series

Patch

diff --git a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c
index 7805ba4..c87bce9 100644
--- a/lib/sbi/sbi_string.c
+++ b/lib/sbi/sbi_string.c
@@ -33,6 +33,10 @@  int sbi_strncmp(const char *a, const char *b, size_t count)
 	for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
 		;
 
+	/* No difference till the end */
+	if (!count)
+		return 0;
+
 	return *a - *b;
 }