libs/libc/grp and pwd: Revisit file looks. Use uintptr_t vs void* to incode mixed pointers and integer values.

This commit is contained in:
Gregory Nutt 2019-08-11 11:25:47 -06:00
parent e649d6c21e
commit eb5a8c7cea
2 changed files with 34 additions and 16 deletions

View File

@ -52,7 +52,7 @@
****************************************************************************/
typedef CODE int (grp_foreach_match_t)(FAR const struct group *entry,
FAR void *arg);
uintptr_t arg);
/****************************************************************************
* Private Functions
@ -76,7 +76,7 @@ typedef CODE int (grp_foreach_match_t)(FAR const struct group *entry,
*
****************************************************************************/
static int grp_match_name(FAR const struct group *entry, FAR void *arg)
static int grp_match_name(FAR const struct group *entry, uintptr_t arg)
{
FAR const char *gname = (FAR const char *)arg;
return strcmp(entry->gr_name, gname) == 0 ? 1 : 0;
@ -100,9 +100,9 @@ static int grp_match_name(FAR const struct group *entry, FAR void *arg)
*
****************************************************************************/
static int grp_match_gid(FAR const struct group *entry, FAR void *arg)
static int grp_match_gid(FAR const struct group *entry, uintptr_t arg)
{
int match_gid = (int)((uintptr_t)arg);
int match_gid = (int)arg;
return match_gid == entry->gr_gid ? 1 : 0;
}
@ -126,7 +126,7 @@ static int grp_match_gid(FAR const struct group *entry, FAR void *arg)
*
****************************************************************************/
static int grp_foreach(grp_foreach_match_t match, FAR void *arg,
static int grp_foreach(grp_foreach_match_t match, uintptr_t arg,
FAR struct group *entry, FAR char *buffer,
size_t buflen)
{
@ -312,7 +312,7 @@ static int grp_foreach(grp_foreach_match_t match, FAR void *arg,
int grp_findby_name(FAR const char *gname, FAR struct group *entry,
FAR char *buffer, size_t buflen)
{
return grp_foreach(grp_match_name, (FAR void *)gname, entry, buffer, buflen);
return grp_foreach(grp_match_name, (uintptr_t)gname, entry, buffer, buflen);
}
/****************************************************************************
@ -337,6 +337,15 @@ int grp_findby_name(FAR const char *gname, FAR struct group *entry,
int grp_findby_gid(gid_t gid, FAR struct group *entry, FAR char *buffer,
size_t buflen)
{
return grp_foreach(grp_match_gid, (FAR void *)((uintptr_t)gid), entry,
buffer, buflen);
/* Verify that the GID is in the valid range of 0 through INT16_MAX.
* OpenGroup.org does not specify a GID_MAX or GID_MIN. Instead we use a
* priori knowledge that gid_t is type int16_t.
*/
if ((uint16_t)gid > INT16_MAX)
{
return -EINVAL;
}
return grp_foreach(grp_match_gid, (uintptr_t)gid, entry, buffer, buflen);
}

View File

@ -52,7 +52,7 @@
****************************************************************************/
typedef CODE int (pwd_foreach_match_t)(FAR const struct passwd *entry,
FAR void *arg);
uintptr_t arg);
/****************************************************************************
* Private Functions
@ -76,7 +76,7 @@ typedef CODE int (pwd_foreach_match_t)(FAR const struct passwd *entry,
*
****************************************************************************/
static int pwd_match_name(FAR const struct passwd *entry, FAR void *arg)
static int pwd_match_name(FAR const struct passwd *entry, uintptr_t arg)
{
FAR const char *uname = (FAR const char *)arg;
return strcmp(entry->pw_name, uname) == 0 ? 1 : 0;
@ -100,9 +100,9 @@ static int pwd_match_name(FAR const struct passwd *entry, FAR void *arg)
*
****************************************************************************/
static int pwd_match_uid(FAR const struct passwd *entry, FAR void *arg)
static int pwd_match_uid(FAR const struct passwd *entry, uintptr_t arg)
{
int match_uid = (int)((uintptr_t)arg);
int match_uid = (int)arg;
return match_uid == entry->pw_uid ? 1 : 0;
}
@ -126,7 +126,7 @@ static int pwd_match_uid(FAR const struct passwd *entry, FAR void *arg)
*
****************************************************************************/
static int pwd_foreach(pwd_foreach_match_t match, FAR void *arg,
static int pwd_foreach(pwd_foreach_match_t match, uintptr_t arg,
FAR struct passwd *entry, FAR char *buffer,
size_t buflen)
{
@ -289,7 +289,7 @@ static int pwd_foreach(pwd_foreach_match_t match, FAR void *arg,
int pwd_findby_name(FAR const char *uname, FAR struct passwd *entry,
FAR char *buffer, size_t buflen)
{
return pwd_foreach(pwd_match_name, (FAR void *)uname, entry, buffer, buflen);
return pwd_foreach(pwd_match_name, (uintptr_t)uname, entry, buffer, buflen);
}
/****************************************************************************
@ -314,6 +314,15 @@ int pwd_findby_name(FAR const char *uname, FAR struct passwd *entry,
int pwd_findby_uid(uid_t uid, FAR struct passwd *entry, FAR char *buffer,
size_t buflen)
{
return pwd_foreach(pwd_match_uid, (FAR void *)((uintptr_t)uid), entry,
buffer, buflen);
/* Verify that the UID is in the valid range of 0 through INT16_MAX.
* OpenGroup.org does not specify a UID_MAX or UID_MIN. Instead we use a
* priori knowledge that uid_t is type int16_t.
*/
if ((uint16_t)uid > INT16_MAX)
{
return -EINVAL;
}
return pwd_foreach(pwd_match_uid, (uintptr_t)uid, entry, buffer, buflen);
}