From 6446f80a0229b6dc51f269c13bfa24774922ff2b Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sat, 27 Aug 2022 10:29:11 -0400 Subject: [PATCH] privs: Don't use libc isdigit() Avoid locale dependence and UB risk from integer promotion --- nk/privs.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nk/privs.c b/nk/privs.c index 6fb6f08..449c6f1 100644 --- a/nk/privs.c +++ b/nk/privs.c @@ -6,8 +6,8 @@ #include #include +#include #include -#include #include #include #include @@ -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;