Skip to content

Commit 7388c36

Browse files
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 <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]>
1 parent b54bb7e commit 7388c36

File tree

3 files changed

+11
-28
lines changed

3 files changed

+11
-28
lines changed

lib/chkname.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "defines.h"
3333
#include "chkname.h"
34+
#include "string/ctype/strisascii/strisdigit.h"
3435
#include "string/strcmp/streq.h"
3536

3637

@@ -71,7 +72,11 @@ is_valid_name(const char *name)
7172
*
7273
* Also do not allow fully numeric names or just "." or "..".
7374
*/
74-
int numeric;
75+
76+
if (strisdigit(name)) {
77+
errno = EINVAL;
78+
return false;
79+
}
7580

7681
if (streq(name, "") ||
7782
streq(name, ".") ||
@@ -86,8 +91,6 @@ is_valid_name(const char *name)
8691
return false;
8792
}
8893

89-
numeric = isdigit(*name);
90-
9194
while (!streq(++name, "")) {
9295
if (!((*name >= 'a' && *name <= 'z') ||
9396
(*name >= 'A' && *name <= 'Z') ||
@@ -101,12 +104,6 @@ is_valid_name(const char *name)
101104
errno = EINVAL;
102105
return false;
103106
}
104-
numeric &= isdigit(*name);
105-
}
106-
107-
if (numeric) {
108-
errno = EINVAL;
109-
return false;
110107
}
111108

112109
return true;

lib/strtoday.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "atoi/str2i/str2s.h"
1515
#include "getdate.h"
1616
#include "prototypes.h"
17+
#include "string/ctype/strisascii/strisdigit.h"
1718
#include "string/strcmp/streq.h"
1819
#include "string/strspn/stpspn.h"
1920

@@ -35,7 +36,6 @@
3536
long strtoday (const char *str)
3637
{
3738
time_t t;
38-
bool isnum = true;
3939
const char *s = str;
4040

4141
/*
@@ -54,14 +54,9 @@ long strtoday (const char *str)
5454
s++;
5555
}
5656
s = stpspn(s, " ");
57-
while (isnum && !streq(s, "")) {
58-
if (!isdigit (*s)) {
59-
isnum = false;
60-
}
61-
s++;
62-
}
63-
if (isnum) {
57+
if (strisdigit(s)) {
6458
long retdate;
59+
6560
if (str2sl(&retdate, str) == -1)
6661
return -2;
6762
return retdate;

lib/subordinateio.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "alloc/realloc.h"
2323
#include "alloc/reallocf.h"
2424
#include "atoi/str2i/str2u.h"
25+
#include "string/ctype/strisascii/strisdigit.h"
2526
#include "string/sprintf/snprintf.h"
2627
#include "string/strcmp/streq.h"
2728

@@ -926,22 +927,12 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
926927
return count;
927928
}
928929

929-
static bool all_digits(const char *str)
930-
{
931-
int i;
932-
933-
for (i = 0; str[i] != '\0'; i++)
934-
if (!isdigit(str[i]))
935-
return false;
936-
return true;
937-
}
938-
939930
static int append_uids(uid_t **uids, const char *owner, int n)
940931
{
941932
int i;
942933
uid_t owner_uid;
943934

944-
if (all_digits(owner)) {
935+
if (strisdigit(owner)) {
945936
i = sscanf(owner, "%d", &owner_uid);
946937
if (i != 1) {
947938
// should not happen

0 commit comments

Comments
 (0)