From: Alejandro Colomar Date: Thu, 4 Jul 2024 13:42:17 +0000 (+0200) Subject: lib/list.c: comma_to_list(): Use strchrcnt() instead of its pattern X-Git-Tag: 4.17.0-rc1~32 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=99a3ca17dfa066c570a852c99b1b1cc3dfb3ac32;p=thirdparty%2Fshadow.git lib/list.c: comma_to_list(): Use strchrcnt() instead of its pattern Signed-off-by: Alejandro Colomar --- diff --git a/lib/list.c b/lib/list.c index 1132c5c7e..fbc0e14a4 100644 --- a/lib/list.c +++ b/lib/list.c @@ -15,6 +15,7 @@ #include "alloc/x/xmalloc.h" #include "prototypes.h" #include "defines.h" +#include "string/strchr/strchrcnt.h" #include "string/strdup/xstrdup.h" @@ -179,7 +180,8 @@ bool is_on_list (char *const *list, const char *member) * comma_to_list - convert comma-separated list to (char *) array */ -/*@only@*/char **comma_to_list (const char *comma) +/*@only@*/char ** +comma_to_list(const char *comma) { char *members; char **array; @@ -195,30 +197,12 @@ bool is_on_list (char *const *list, const char *member) members = xstrdup (comma); - /* - * Count the number of commas in the list - */ - - for (cp = members, i = 0;; i++) { - cp2 = strchr (cp, ','); - if (NULL != cp2) { - cp = cp2 + 1; - } else { - break; - } - } - - /* - * Add 2 - one for the ending NULL, the other for the last item - */ - - i += 2; - /* * Allocate the array we're going to store the pointers into. + * n: number of delimiters + last element + NULL */ - array = XMALLOC(i, char *); + array = XMALLOC(strchrcnt(members, ',') + 2, char *); /* * Empty list is special - 0 members, not 1 empty member. --marekm