From 243350815806e9ee49ff08d1576c1cd5f4deba84 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 10 Dec 2024 16:24:33 +0100 Subject: [PATCH] lib/: Use strisdigit() instead of its pattern 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. Signed-off-by: Alejandro Colomar --- lib/chkname.c | 15 ++++++--------- lib/strtoday.c | 11 +++-------- lib/subordinateio.c | 13 ++----------- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/lib/chkname.c b/lib/chkname.c index 98f791706..aa4dc97a4 100644 --- a/lib/chkname.c +++ b/lib/chkname.c @@ -33,6 +33,7 @@ #include "defines.h" #include "chkname.h" +#include "string/ctype/strisdigit.h" #include "string/strcmp/streq.h" @@ -69,7 +70,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 ('\0' == *name || ('.' == *name && (('.' == name[1] && '\0' == name[2]) || @@ -84,8 +89,6 @@ is_valid_name(const char *name) return false; } - numeric = isdigit(*name); - while (!streq(++name, "")) { if (!((*name >= 'a' && *name <= 'z') || (*name >= 'A' && *name <= 'Z') || @@ -99,12 +102,6 @@ is_valid_name(const char *name) errno = EINVAL; return false; } - numeric &= isdigit(*name); - } - - if (numeric) { - errno = EINVAL; - return false; } return true; diff --git a/lib/strtoday.c b/lib/strtoday.c index 01f2e9b7e..4e38045f5 100644 --- a/lib/strtoday.c +++ b/lib/strtoday.c @@ -14,6 +14,7 @@ #include "atoi/str2i/str2s.h" #include "getdate.h" #include "prototypes.h" +#include "string/ctype/strisdigit.h" #include "string/strchr/stpspn.h" #include "string/strcmp/streq.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; diff --git a/lib/subordinateio.c b/lib/subordinateio.c index bf02328e7..38b1d5bf1 100644 --- a/lib/subordinateio.c +++ b/lib/subordinateio.c @@ -22,6 +22,7 @@ #include "alloc/realloc.h" #include "alloc/reallocf.h" #include "atoi/str2i/str2u.h" +#include "string/ctype/strisdigit.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" @@ -926,22 +927,12 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r 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