Skip to content

Commit e22bf37

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. Signed-off-by: Alejandro Colomar <[email protected]>
1 parent d5fb6c4 commit e22bf37

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
@@ -33,6 +33,7 @@
3333

3434
#include "defines.h"
3535
#include "chkname.h"
36+
#include "string/ctype/strisascii/strisdigit.h"
3637
#include "string/strcmp/streq.h"
3738

3839

@@ -69,7 +70,11 @@ is_valid_name(const char *name)
6970
*
7071
* Also do not allow fully numeric names or just "." or "..".
7172
*/
72-
int numeric;
73+
74+
if (strisdigit(name)) {
75+
errno = EINVAL;
76+
return false;
77+
}
7378

7479
if ('\0' == *name ||
7580
('.' == *name && (('.' == name[1] && '\0' == name[2]) ||
@@ -84,8 +89,6 @@ is_valid_name(const char *name)
8489
return false;
8590
}
8691

87-
numeric = isdigit(*name);
88-
8992
while (!streq(++name, "")) {
9093
if (!((*name >= 'a' && *name <= 'z') ||
9194
(*name >= 'A' && *name <= 'Z') ||
@@ -99,12 +102,6 @@ is_valid_name(const char *name)
99102
errno = EINVAL;
100103
return false;
101104
}
102-
numeric &= isdigit(*name);
103-
}
104-
105-
if (numeric) {
106-
errno = EINVAL;
107-
return false;
108105
}
109106

110107
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/strchr/stpspn.h"
1819
#include "string/strcmp/streq.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)