privs: Don't use libc isdigit()

Avoid locale dependence and UB risk from integer promotion
This commit is contained in:
Nicholas J. Kain 2022-08-27 10:29:11 -04:00
parent 7fb968e861
commit 6446f80a02
No known key found for this signature in database

View File

@ -6,8 +6,8 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
@ -20,6 +20,8 @@
#include "nk/privs.h"
#include "nk/log.h"
static inline bool nk_isdigit(int c) { return c >= '0' && c <= '9'; }
void nk_set_chroot(const char *chroot_dir)
{
if (chroot(chroot_dir))
@ -143,7 +145,7 @@ uid_t nk_uidgidbyname(const char *username, uid_t *uid, gid_t *gid)
struct passwd *pws = getpwnam(username);
if (!pws) {
for (size_t i = 0; username[i]; ++i) {
if (!isdigit(username[i]))
if (!nk_isdigit(username[i]))
return (uid_t)-1;
}
char *p;
@ -172,7 +174,7 @@ gid_t nk_gidbyname(const char *groupname, gid_t *gid)
struct group *grp = getgrnam(groupname);
if (!grp) {
for (size_t i = 0; groupname[i]; ++i) {
if (!isdigit(groupname[i]))
if (!nk_isdigit(groupname[i]))
return (gid_t)-1;
}
char *p;