Skip to content

Commit

Permalink
lib/: Use strisdigit() instead of its pattern
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
alejandro-colomar committed Dec 11, 2024
1 parent 40c9035 commit 2433508
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 28 deletions.
15 changes: 6 additions & 9 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "defines.h"
#include "chkname.h"
#include "string/ctype/strisdigit.h"
#include "string/strcmp/streq.h"


Expand Down Expand Up @@ -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]) ||
Expand All @@ -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') ||
Expand All @@ -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;
Expand Down
11 changes: 3 additions & 8 deletions lib/strtoday.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -35,7 +36,6 @@
long strtoday (const char *str)
{
time_t t;
bool isnum = true;
const char *s = str;

/*
Expand All @@ -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;
Expand Down
13 changes: 2 additions & 11 deletions lib/subordinateio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2433508

Please sign in to comment.