Skip to content

Commit

Permalink
lib/if: fix interface name comparison
Browse files Browse the repository at this point in the history
Using strtol() to compare two strings is a bad idea.
Before the patch, if_cmp_name_func() may confuse foo001 and foo1.

PR=79407
Fixes: 106d2fd ("2003-08-01 Cougar <[email protected]>")
Signed-off-by: Nicolas Dichtel <[email protected]>
Tested-by: Aurélien Degeorges <[email protected]>
Acked-by: Philippe Guibert <[email protected]>
  • Loading branch information
NicolasDichtel committed May 30, 2022
1 parent 8bc5979 commit f33abb8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/if.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ int if_cmp_name_func(const char *p1, const char *p2)
int res;

while (*p1 && *p2) {
char *tmp1, *tmp2;

/* look up to any number */
l1 = strcspn(p1, "0123456789");
l2 = strcspn(p2, "0123456789");
Expand Down Expand Up @@ -111,15 +113,25 @@ int if_cmp_name_func(const char *p1, const char *p2)
if (!*p2)
return 1;

x1 = strtol(p1, (char **)&p1, 10);
x2 = strtol(p2, (char **)&p2, 10);
x1 = strtol(p1, (char **)&tmp1, 10);
x2 = strtol(p2, (char **)&tmp2, 10);

/* let's compare numbers now */
if (x1 < x2)
return -1;
if (x1 > x2)
return 1;

/* Compare string if numbers are equal (distinguish foo-1 from foo-001) */
l1 = strspn(p1, "0123456789");
l2 = strspn(p2, "0123456789");
if (l1 != l2)
return (strcmp(p1, p2));

/* Continue to parse the rest of the string */
p1 = (const char *)tmp1;
p2 = (const char *)tmp2;

/* numbers were equal, lets do it again..
(it happens with name like "eth123.456:789") */
}
Expand Down

0 comments on commit f33abb8

Please sign in to comment.