Use *array() allocation functions where appropriate
This prevents overflow from multiplication. Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
parent
727275a027
commit
191f04f7dc
@ -635,7 +635,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
entries = malloc (n * sizeof (struct commonio_entry *));
|
entries = mallocarray (n, sizeof (struct commonio_entry *));
|
||||||
if (entries == NULL) {
|
if (entries == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@
|
|||||||
for (i = 0; grent->gr_mem[i]; i++);
|
for (i = 0; grent->gr_mem[i]; i++);
|
||||||
|
|
||||||
/*@-mustfreeonly@*/
|
/*@-mustfreeonly@*/
|
||||||
gr->gr_mem = (char **) malloc ((i + 1) * sizeof (char *));
|
gr->gr_mem = (char **) mallocarray (i + 1, sizeof (char *));
|
||||||
/*@=mustfreeonly@*/
|
/*@=mustfreeonly@*/
|
||||||
if (NULL == gr->gr_mem) {
|
if (NULL == gr->gr_mem) {
|
||||||
gr_free(gr);
|
gr_free(gr);
|
||||||
|
@ -117,7 +117,7 @@ void endsgent (void)
|
|||||||
size_t len = strlen (string) + 1;
|
size_t len = strlen (string) + 1;
|
||||||
|
|
||||||
if (len > sgrbuflen) {
|
if (len > sgrbuflen) {
|
||||||
char *buf = (char *) realloc (sgrbuf, sizeof (char) * len);
|
char *buf = (char *) reallocarray (sgrbuf, len, sizeof (char));
|
||||||
if (NULL == buf) {
|
if (NULL == buf) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ static char **list (char *s)
|
|||||||
member name, or terminating NULL). */
|
member name, or terminating NULL). */
|
||||||
if (i >= size) {
|
if (i >= size) {
|
||||||
size = i + 100; /* at least: i + 1 */
|
size = i + 100; /* at least: i + 1 */
|
||||||
members = reallocf (members, size * sizeof (char *));
|
members = reallocarrayf (members, size, sizeof(char *));
|
||||||
if (!members)
|
if (!members)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@
|
|||||||
|
|
||||||
for (i = 0; NULL != sgent->sg_adm[i]; i++);
|
for (i = 0; NULL != sgent->sg_adm[i]; i++);
|
||||||
/*@-mustfreeonly@*/
|
/*@-mustfreeonly@*/
|
||||||
sg->sg_adm = (char **) malloc ((i + 1) * sizeof (char *));
|
sg->sg_adm = (char **) mallocarray (i + 1, sizeof (char *));
|
||||||
/*@=mustfreeonly@*/
|
/*@=mustfreeonly@*/
|
||||||
if (NULL == sg->sg_adm) {
|
if (NULL == sg->sg_adm) {
|
||||||
free (sg->sg_passwd);
|
free (sg->sg_passwd);
|
||||||
@ -74,7 +74,7 @@
|
|||||||
|
|
||||||
for (i = 0; NULL != sgent->sg_mem[i]; i++);
|
for (i = 0; NULL != sgent->sg_mem[i]; i++);
|
||||||
/*@-mustfreeonly@*/
|
/*@-mustfreeonly@*/
|
||||||
sg->sg_mem = (char **) malloc ((i + 1) * sizeof (char *));
|
sg->sg_mem = (char **) mallocarray (i + 1, sizeof (char *));
|
||||||
/*@=mustfreeonly@*/
|
/*@=mustfreeonly@*/
|
||||||
if (NULL == sg->sg_mem) {
|
if (NULL == sg->sg_mem) {
|
||||||
for (i = 0; NULL != sg->sg_adm[i]; i++) {
|
for (i = 0; NULL != sg->sg_adm[i]; i++) {
|
||||||
|
@ -319,7 +319,7 @@ static bool append_range(struct subid_range **ranges, const struct subordinate_r
|
|||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
struct subid_range *alloced;
|
struct subid_range *alloced;
|
||||||
alloced = realloc(*ranges, (n + 1) * (sizeof(struct subid_range)));
|
alloced = reallocarray(*ranges, n + 1, sizeof(struct subid_range));
|
||||||
if (!alloced)
|
if (!alloced)
|
||||||
return false;
|
return false;
|
||||||
*ranges = alloced;
|
*ranges = alloced;
|
||||||
@ -911,7 +911,7 @@ static int append_uids(uid_t **uids, const char *owner, int n)
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = realloc(*uids, (n + 1) * sizeof(uid_t));
|
ret = reallocarray(*uids, n + 1, sizeof(uid_t));
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
free(*uids);
|
free(*uids);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -46,7 +46,7 @@ int add_groups (const char *list)
|
|||||||
|
|
||||||
i = 16;
|
i = 16;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
grouplist = (gid_t *) malloc (i * sizeof (GETGROUPS_T));
|
grouplist = (gid_t *) mallocarray (i, sizeof (GETGROUPS_T));
|
||||||
if (NULL == grouplist) {
|
if (NULL == grouplist) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ int add_groups (const char *list)
|
|||||||
fputs (_("Warning: too many groups\n"), shadow_logfd);
|
fputs (_("Warning: too many groups\n"), shadow_logfd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
tmp = (gid_t *) realloc (grouplist, (size_t)(ngroups + 1) * sizeof (GETGROUPS_T));
|
tmp = (gid_t *) reallocarray (grouplist, (size_t)ngroups + 1, sizeof (GETGROUPS_T));
|
||||||
if (NULL == tmp) {
|
if (NULL == tmp) {
|
||||||
free (grouplist);
|
free (grouplist);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -60,7 +60,7 @@ static const char *const noslash[] = {
|
|||||||
*/
|
*/
|
||||||
void initenv (void)
|
void initenv (void)
|
||||||
{
|
{
|
||||||
newenvp = (char **) xmalloc (NEWENVP_STEP * sizeof (char *));
|
newenvp = (char **) xmallocarray (NEWENVP_STEP, sizeof (char *));
|
||||||
*newenvp = NULL;
|
*newenvp = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,7 +231,7 @@ int find_new_uid(bool sys_user,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* Create an array to hold all of the discovered UIDs */
|
/* Create an array to hold all of the discovered UIDs */
|
||||||
used_uids = malloc (sizeof (bool) * (uid_max +1));
|
used_uids = mallocarray (uid_max + 1, sizeof (bool));
|
||||||
if (NULL == used_uids) {
|
if (NULL == used_uids) {
|
||||||
fprintf (log_get_logfd(),
|
fprintf (log_get_logfd(),
|
||||||
_("%s: failed to allocate memory: %s\n"),
|
_("%s: failed to allocate memory: %s\n"),
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
* old entries, and the new entries as well.
|
* old entries, and the new entries as well.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
tmp = (char **) xmalloc ((i + 2) * sizeof member);
|
tmp = (char **) xmallocarray (i + 2, sizeof member);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the original list to the new list, then append the
|
* Copy the original list to the new list, then append the
|
||||||
@ -98,7 +98,7 @@
|
|||||||
* old entries.
|
* old entries.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
tmp = (char **) xmalloc ((j + 1) * sizeof member);
|
tmp = (char **) xmallocarray (j + 1, sizeof member);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the original list except the deleted members to the
|
* Copy the original list except the deleted members to the
|
||||||
@ -133,7 +133,7 @@
|
|||||||
|
|
||||||
for (i = 0; NULL != list[i]; i++);
|
for (i = 0; NULL != list[i]; i++);
|
||||||
|
|
||||||
tmp = (char **) xmalloc ((i + 1) * sizeof (char *));
|
tmp = (char **) xmallocarray (i + 1, sizeof (char *));
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (NULL != *list) {
|
while (NULL != *list) {
|
||||||
@ -210,7 +210,7 @@ bool is_on_list (char *const *list, const char *member)
|
|||||||
* Allocate the array we're going to store the pointers into.
|
* Allocate the array we're going to store the pointers into.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
array = (char **) xmalloc (sizeof (char *) * i);
|
array = (char **) xmallocarray (i, sizeof (char *));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Empty list is special - 0 members, not 1 empty member. --marekm
|
* Empty list is special - 0 members, not 1 empty member. --marekm
|
||||||
|
@ -834,7 +834,7 @@ static void get_group (struct group *gr)
|
|||||||
|
|
||||||
sg->sg_mem = dup_list (gr->gr_mem);
|
sg->sg_mem = dup_list (gr->gr_mem);
|
||||||
|
|
||||||
sg->sg_adm = (char **) xmalloc (sizeof (char *) * 2);
|
sg->sg_adm = (char **) xmallocarray (2, sizeof (char *));
|
||||||
#ifdef FIRST_MEMBER_IS_ADMIN
|
#ifdef FIRST_MEMBER_IS_ADMIN
|
||||||
if (sg->sg_mem[0]) {
|
if (sg->sg_mem[0]) {
|
||||||
sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
|
sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
|
||||||
|
@ -88,7 +88,7 @@ int main (int argc, char **argv)
|
|||||||
GETGROUPS_T *groups;
|
GETGROUPS_T *groups;
|
||||||
|
|
||||||
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
||||||
groups = (GETGROUPS_T *) malloc (sizeof (GETGROUPS_T) * sys_ngroups);
|
groups = (GETGROUPS_T *) mallocarray (sys_ngroups, sizeof (GETGROUPS_T));
|
||||||
|
|
||||||
(void) setlocale (LC_ALL, "");
|
(void) setlocale (LC_ALL, "");
|
||||||
(void) bindtextdomain (PACKAGE, LOCALEDIR);
|
(void) bindtextdomain (PACKAGE, LOCALEDIR);
|
||||||
|
2
src/id.c
2
src/id.c
@ -63,7 +63,7 @@ static void usage (void)
|
|||||||
* work if the system library is recompiled.
|
* work if the system library is recompiled.
|
||||||
*/
|
*/
|
||||||
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
||||||
groups = (GETGROUPS_T *) malloc (sizeof (GETGROUPS_T) * sys_ngroups);
|
groups = (GETGROUPS_T *) mallocarray (sys_ngroups, sizeof (GETGROUPS_T));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* See if the -a flag has been given to print out the concurrent
|
* See if the -a flag has been given to print out the concurrent
|
||||||
|
@ -531,7 +531,7 @@ int main (int argc, char **argv)
|
|||||||
/* don't use getgroups(0, 0) - it doesn't work on some systems */
|
/* don't use getgroups(0, 0) - it doesn't work on some systems */
|
||||||
i = 16;
|
i = 16;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
grouplist = (GETGROUPS_T *) xmalloc (i * sizeof (GETGROUPS_T));
|
grouplist = (GETGROUPS_T *) xmallocarray (i, sizeof (GETGROUPS_T));
|
||||||
ngroups = getgroups (i, grouplist);
|
ngroups = getgroups (i, grouplist);
|
||||||
if (i > ngroups && !(ngroups == -1 && errno == EINVAL)) {
|
if (i > ngroups && !(ngroups == -1 && errno == EINVAL)) {
|
||||||
break;
|
break;
|
||||||
|
@ -1200,9 +1200,9 @@ int main (int argc, char **argv)
|
|||||||
#ifdef USE_PAM
|
#ifdef USE_PAM
|
||||||
/* keep the list of user/password for later update by PAM */
|
/* keep the list of user/password for later update by PAM */
|
||||||
nusers++;
|
nusers++;
|
||||||
lines = reallocf (lines, sizeof (lines[0]) * nusers);
|
lines = reallocf (lines, nusers, sizeof (lines[0]));
|
||||||
usernames = reallocf (usernames, sizeof (usernames[0]) * nusers);
|
usernames = reallocf (usernames, nusers, sizeof (usernames[0]));
|
||||||
passwords = reallocf (passwords, sizeof (passwords[0]) * nusers);
|
passwords = reallocf (passwords, nusers, sizeof (passwords[0]));
|
||||||
if (lines == NULL || usernames == NULL || passwords == NULL) {
|
if (lines == NULL || usernames == NULL || passwords == NULL) {
|
||||||
fprintf (stderr,
|
fprintf (stderr,
|
||||||
_("%s: line %d: %s\n"),
|
_("%s: line %d: %s\n"),
|
||||||
|
2
src/su.c
2
src/su.c
@ -238,7 +238,7 @@ static void execve_shell (const char *shellname,
|
|||||||
while (NULL != args[n_args]) {
|
while (NULL != args[n_args]) {
|
||||||
n_args++;
|
n_args++;
|
||||||
}
|
}
|
||||||
targs = (char **) xmalloc ((n_args + 3) * sizeof (args[0]));
|
targs = (char **) xmallocarray (n_args + 3, sizeof (args[0]));
|
||||||
targs[0] = "sh";
|
targs[0] = "sh";
|
||||||
targs[1] = "-";
|
targs[1] = "-";
|
||||||
targs[2] = xstrdup (shellname);
|
targs[2] = xstrdup (shellname);
|
||||||
|
@ -2539,7 +2539,7 @@ int main (int argc, char **argv)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
||||||
user_groups = (char **) xmalloc ((1 + sys_ngroups) * sizeof (char *));
|
user_groups = (char **) xmallocarray (1 + sys_ngroups, sizeof (char *));
|
||||||
/*
|
/*
|
||||||
* Initialize the list to be empty
|
* Initialize the list to be empty
|
||||||
*/
|
*/
|
||||||
|
@ -2150,7 +2150,7 @@ int main (int argc, char **argv)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
||||||
user_groups = (char **) malloc (sizeof (char *) * (1 + sys_ngroups));
|
user_groups = (char **) mallocarray (sys_ngroups + 1, sizeof (char *));
|
||||||
user_groups[0] = NULL;
|
user_groups[0] = NULL;
|
||||||
|
|
||||||
is_shadow_pwd = spw_file_present ();
|
is_shadow_pwd = spw_file_present ();
|
||||||
|
Loading…
Reference in New Issue
Block a user