fix strverscmp comparison of digit sequence with non-digits
authorRich Felker <dalias@aerifal.cx>
Tue, 8 Nov 2022 03:17:55 +0000 (22:17 -0500)
committerRich Felker <dalias@aerifal.cx>
Tue, 8 Nov 2022 03:33:24 +0000 (22:33 -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.

src/string/strverscmp.c

index 4daf276..16c1da2 100644 (file)
@@ -18,9 +18,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;