public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] fix strverscmp comparison of digit sequence with non-digits
@ 2024-01-08 13:56 Brian Inglis
  2024-01-08 16:02 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: Brian Inglis @ 2024-01-08 13:56 UTC (permalink / raw)
  To: newlib

>From: Rich Felker <dalias@aerifal.cx>
Date: Mon, 7 Nov 2022 22:17:55 -0500

the rule that longest digit sequence not beginning with a zero is
greater only applies when both sequences being compared are
non-degenerate. this is spelled out explicitly in the man page, which
may be deemed authoritative for this nonstandard function: "If one or
both of these is empty, then return what strcmp(3) would have
returned..."

we were wrongly treating any sequence of digits not beginning with a
zero as greater than a non-digit in the other string.

Signed-off-by: Brian Inglis <Brian.Inglis@SystematicSW.ab.ca>
---
 newlib/libc/string/strverscmp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/newlib/libc/string/strverscmp.c b/newlib/libc/string/strverscmp.c
index 04aa7213ad98..0117421c9e93 100644
--- a/newlib/libc/string/strverscmp.c
+++ b/newlib/libc/string/strverscmp.c
@@ -76,9 +76,9 @@ int strverscmp(const char *l0, const char *r0)
 		else if (c!='0') z=0;
 	}
 
-	if (l[dp]!='0' && r[dp]!='0') {
-		/* If we're not looking at a digit sequence that began
-		 * with a zero, longest digit string is greater. */
+	if (l[dp]-'1'<9U && r[dp]-'1'<9U) {
+		/* If we're looking at non-degenerate digit sequences starting
+		 * with nonzero digits, longest digit string is greater. */
 		for (j=i; isdigit(l[j]); j++)
 			if (!isdigit(r[j])) return 1;
 		if (isdigit(r[j])) return -1;
-- 
2.42.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] fix strverscmp comparison of digit sequence with non-digits
  2024-01-08 13:56 [PATCH] fix strverscmp comparison of digit sequence with non-digits Brian Inglis
@ 2024-01-08 16:02 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2024-01-08 16:02 UTC (permalink / raw)
  To: newlib

On Jan  8 06:56, Brian Inglis wrote:
> >From: Rich Felker <dalias@aerifal.cx>
> Date: Mon, 7 Nov 2022 22:17:55 -0500
> 
> the rule that longest digit sequence not beginning with a zero is
> greater only applies when both sequences being compared are
> non-degenerate. this is spelled out explicitly in the man page, which
> may be deemed authoritative for this nonstandard function: "If one or
> both of these is empty, then return what strcmp(3) would have
> returned..."
> 
> we were wrongly treating any sequence of digits not beginning with a
> zero as greater than a non-digit in the other string.
> 
> Signed-off-by: Brian Inglis <Brian.Inglis@SystematicSW.ab.ca>
> ---
>  newlib/libc/string/strverscmp.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)

Pushed.

Thanks,
Corinna


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-08 16:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-08 13:56 [PATCH] fix strverscmp comparison of digit sequence with non-digits Brian Inglis
2024-01-08 16:02 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).