]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Use strisdigit() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Tue, 10 Dec 2024 15:24:33 +0000 (16:24 +0100)
committerSerge Hallyn <serge@hallyn.com>
Sun, 16 Feb 2025 22:12:16 +0000 (16:12 -0600)
Note that the old code in

(1)  lib/strtoday.c:strtoday()
(2)  lib/subordinateio.c:append_uids()

was considering an empty string as if it were a number.
strisdigit() does not consider an empty string to be numeric.

I think it will not affect the behavior in either case, as they should
sooner or later result in an error somewhere.  And it seems (IMO)
surprising to treat empty strings as numeric strings, so let's not do
it.

Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/chkname.c
lib/strtoday.c
lib/subordinateio.c

index b2fc0b88ab661eaa33bc07247ab98d7cf8f9426d..57d6d96e76d4b574ffdf151e332903df098b466d 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "defines.h"
 #include "chkname.h"
+#include "string/ctype/strisascii/strisdigit.h"
 #include "string/strcmp/streq.h"
 
 
@@ -71,7 +72,11 @@ is_valid_name(const char *name)
          *
          * Also do not allow fully numeric names or just "." or "..".
          */
-       int numeric;
+
+       if (strisdigit(name)) {
+               errno = EINVAL;
+               return false;
+       }
 
        if (streq(name, "") ||
            streq(name, ".") ||
@@ -86,8 +91,6 @@ is_valid_name(const char *name)
                return false;
        }
 
-       numeric = isdigit(*name);
-
        while (!streq(++name, "")) {
                if (!((*name >= 'a' && *name <= 'z') ||
                      (*name >= 'A' && *name <= 'Z') ||
@@ -101,12 +104,6 @@ is_valid_name(const char *name)
                        errno = EINVAL;
                        return false;
                }
-               numeric &= isdigit(*name);
-       }
-
-       if (numeric) {
-               errno = EINVAL;
-               return false;
        }
 
        return true;
index b9edd193dc72dca925f1abaa4a85c3a3beb25da1..361ad39ed95882ce162635aa3e7218d3b92edf11 100644 (file)
@@ -14,6 +14,7 @@
 #include "atoi/str2i/str2s.h"
 #include "getdate.h"
 #include "prototypes.h"
+#include "string/ctype/strisascii/strisdigit.h"
 #include "string/strcmp/streq.h"
 #include "string/strspn/stpspn.h"
 
@@ -35,7 +36,6 @@
 long strtoday (const char *str)
 {
        time_t t;
-       bool isnum = true;
        const char *s = str;
 
        /*
@@ -54,14 +54,9 @@ long strtoday (const char *str)
                s++;
        }
        s = stpspn(s, " ");
-       while (isnum && !streq(s, "")) {
-               if (!isdigit (*s)) {
-                       isnum = false;
-               }
-               s++;
-       }
-       if (isnum) {
+       if (strisdigit(s)) {
                long retdate;
+
                if (str2sl(&retdate, str) == -1)
                        return -2;
                return retdate;
index bf02328e7977a79e2f6fa7c198ead93ffce3dbca..229f27cb312619ef77ef5e582021c3f4cf00f280 100644 (file)
@@ -22,6 +22,7 @@
 #include "alloc/realloc.h"
 #include "alloc/reallocf.h"
 #include "atoi/str2i/str2u.h"
+#include "string/ctype/strisascii/strisdigit.h"
 #include "string/sprintf/snprintf.h"
 #include "string/strcmp/streq.h"
 
@@ -926,22 +927,12 @@ out:
        return count;
 }
 
-static bool all_digits(const char *str)
-{
-       int i;
-
-       for (i = 0; str[i] != '\0'; i++)
-               if (!isdigit(str[i]))
-                       return false;
-       return true;
-}
-
 static int append_uids(uid_t **uids, const char *owner, int n)
 {
        int    i;
        uid_t  owner_uid;
 
-       if (all_digits(owner)) {
+       if (strisdigit(owner)) {
                i = sscanf(owner, "%d", &owner_uid);
                if (i != 1) {
                        // should not happen