Skip to content

Commit

Permalink
Avoid multiple calls to sysonf() via the MAX macro.
Browse files Browse the repository at this point in the history
The expansion of MAX would result in multiple calls to sysconf().
It is less error-prone to store the result of sysconf() in a long.
  • Loading branch information
millert committed Nov 17, 2024
1 parent 3d85f2e commit da20cce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
10 changes: 6 additions & 4 deletions plugins/sudoers/cvtsudoers_pwutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ cvtsudoers_make_grlist_item(const struct passwd *pw, char * const *unused1)
struct cache_item_grlist *grlitem;
struct sudoers_string *s;
struct group_list *grlist;
size_t groupname_len;
long groupname_len;
debug_decl(cvtsudoers_make_grlist_item, SUDOERS_DEBUG_NSS);

/*
Expand All @@ -421,15 +421,17 @@ cvtsudoers_make_grlist_item(const struct passwd *pw, char * const *unused1)
}

#ifdef _SC_LOGIN_NAME_MAX
groupname_len = (size_t)MAX(sysconf(_SC_LOGIN_NAME_MAX), 32);
groupname_len = sysconf(_SC_LOGIN_NAME_MAX);
if (groupname_len < 32)
groupname_len = 32;
#else
groupname_len = MAX(LOGIN_NAME_MAX, 32);
#endif

/* Allocate in one big chunk for easy freeing. */
nsize = strlen(pw->pw_name) + 1;
total = sizeof(*grlitem) + nsize;
total += groupname_len * ngroups;
total += (size_t)groupname_len * ngroups;

again:
if ((grlitem = calloc(1, total)) == NULL) {
Expand Down Expand Up @@ -470,7 +472,7 @@ cvtsudoers_make_grlist_item(const struct passwd *pw, char * const *unused1)
}
len = strlen(s->str) + 1;
if ((size_t)(cp - (char *)grlitem) + len > total) {
total += len + groupname_len;
total += len + (size_t)groupname_len;
free(grlitem);
goto again;
}
Expand Down
13 changes: 8 additions & 5 deletions plugins/sudoers/pwutil_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,13 @@ PREFIX(make_gidlist_item)(const struct passwd *pw, int ngids, GETGROUPS_T *gids,
struct cache_item *
PREFIX(make_grlist_item)(const struct passwd *pw, char * const *unused1)
{
char *cp;
size_t groupname_len, len, ngroups, nsize, total;
size_t len, ngroups, nsize, total;
struct cache_item_grlist *grlitem;
struct group_list *grlist;
struct gid_list *gidlist;
struct group *grp = NULL;
long groupname_len;
char *cp;
int i;
debug_decl(sudo_make_grlist_item, SUDOERS_DEBUG_NSS);

Expand All @@ -381,7 +382,9 @@ PREFIX(make_grlist_item)(const struct passwd *pw, char * const *unused1)
}

#ifdef _SC_LOGIN_NAME_MAX
groupname_len = (size_t)MAX(sysconf(_SC_LOGIN_NAME_MAX), 32);
groupname_len = sysconf(_SC_LOGIN_NAME_MAX);
if (groupname_len < 32)
groupname_len = 32;
#else
groupname_len = MAX(LOGIN_NAME_MAX, 32);
#endif
Expand All @@ -390,7 +393,7 @@ PREFIX(make_grlist_item)(const struct passwd *pw, char * const *unused1)
nsize = strlen(pw->pw_name) + 1;
total = sizeof(*grlitem) + nsize;
total += sizeof(char *) * (size_t)gidlist->ngids;
total += groupname_len * (size_t)gidlist->ngids;
total += (size_t)(groupname_len * gidlist->ngids);

again:
if ((grlitem = calloc(1, total)) == NULL) {
Expand Down Expand Up @@ -429,7 +432,7 @@ PREFIX(make_grlist_item)(const struct passwd *pw, char * const *unused1)
if ((grp = sudo_getgrgid(gidlist->gids[i])) != NULL) {
len = strlen(grp->gr_name) + 1;
if ((size_t)(cp - (char *)grlitem) + len > total) {
total += len + groupname_len;
total += len + (size_t)groupname_len;
free(grlitem);
sudo_gr_delref(grp);
goto again;
Expand Down

0 comments on commit da20cce

Please sign in to comment.