From 77eb67d4a83600febf326b4551949ce5e71e5a34 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. Reviewed-by: Serge Hallyn 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 b2fc0b88a..57d6d96e7 100644 --- a/lib/chkname.c +++ b/lib/chkname.c @@ -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; diff --git a/lib/strtoday.c b/lib/strtoday.c index b9edd193d..361ad39ed 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/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; diff --git a/lib/subordinateio.c b/lib/subordinateio.c index bf02328e7..229f27cb3 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/strisascii/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