Use *array() allocation functions where appropriate

This prevents overflow from multiplication.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-02-04 21:47:01 +01:00
committed by Serge Hallyn
parent 727275a027
commit 191f04f7dc
18 changed files with 26 additions and 26 deletions

View File

@@ -46,7 +46,7 @@ int add_groups (const char *list)
i = 16;
for (;;) {
grouplist = (gid_t *) malloc (i * sizeof (GETGROUPS_T));
grouplist = (gid_t *) mallocarray (i, sizeof (GETGROUPS_T));
if (NULL == grouplist) {
return -1;
}
@@ -88,7 +88,7 @@ int add_groups (const char *list)
fputs (_("Warning: too many groups\n"), shadow_logfd);
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) {
free (grouplist);
return -1;

View File

@@ -60,7 +60,7 @@ static const char *const noslash[] = {
*/
void initenv (void)
{
newenvp = (char **) xmalloc (NEWENVP_STEP * sizeof (char *));
newenvp = (char **) xmallocarray (NEWENVP_STEP, sizeof (char *));
*newenvp = NULL;
}

View File

@@ -231,7 +231,7 @@ int find_new_uid(bool sys_user,
*/
/* 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) {
fprintf (log_get_logfd(),
_("%s: failed to allocate memory: %s\n"),

View File

@@ -44,7 +44,7 @@
* 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
@@ -98,7 +98,7 @@
* 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
@@ -133,7 +133,7 @@
for (i = 0; NULL != list[i]; i++);
tmp = (char **) xmalloc ((i + 1) * sizeof (char *));
tmp = (char **) xmallocarray (i + 1, sizeof (char *));
i = 0;
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.
*/
array = (char **) xmalloc (sizeof (char *) * i);
array = (char **) xmallocarray (i, sizeof (char *));
/*
* Empty list is special - 0 members, not 1 empty member. --marekm