This prevents overflow from multiplication.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
return 0;
}
- entries = malloc (n * sizeof (struct commonio_entry *));
+ entries = mallocarray (n, sizeof (struct commonio_entry *));
if (entries == NULL) {
return -1;
}
for (i = 0; grent->gr_mem[i]; i++);
/*@-mustfreeonly@*/
- gr->gr_mem = (char **) malloc ((i + 1) * sizeof (char *));
+ gr->gr_mem = (char **) mallocarray (i + 1, sizeof (char *));
/*@=mustfreeonly@*/
if (NULL == gr->gr_mem) {
gr_free(gr);
size_t len = strlen (string) + 1;
if (len > sgrbuflen) {
- char *buf = (char *) realloc (sgrbuf, sizeof (char) * len);
+ char *buf = (char *) reallocarray (sgrbuf, len, sizeof (char));
if (NULL == buf) {
return NULL;
}
member name, or terminating NULL). */
if (i >= size) {
size = i + 100; /* at least: i + 1 */
- members = reallocf (members, size * sizeof (char *));
+ members = reallocarrayf (members, size, sizeof(char *));
if (!members)
return NULL;
}
for (i = 0; NULL != sgent->sg_adm[i]; i++);
/*@-mustfreeonly@*/
- sg->sg_adm = (char **) malloc ((i + 1) * sizeof (char *));
+ sg->sg_adm = (char **) mallocarray (i + 1, sizeof (char *));
/*@=mustfreeonly@*/
if (NULL == sg->sg_adm) {
free (sg->sg_passwd);
for (i = 0; NULL != sgent->sg_mem[i]; i++);
/*@-mustfreeonly@*/
- sg->sg_mem = (char **) malloc ((i + 1) * sizeof (char *));
+ sg->sg_mem = (char **) mallocarray (i + 1, sizeof (char *));
/*@=mustfreeonly@*/
if (NULL == sg->sg_mem) {
for (i = 0; NULL != sg->sg_adm[i]; i++) {
return false;
} else {
struct subid_range *alloced;
- alloced = realloc(*ranges, (n + 1) * (sizeof(struct subid_range)));
+ alloced = reallocarray(*ranges, n + 1, sizeof(struct subid_range));
if (!alloced)
return false;
*ranges = alloced;
return n;
}
- ret = realloc(*uids, (n + 1) * sizeof(uid_t));
+ ret = reallocarray(*uids, n + 1, sizeof(uid_t));
if (!ret) {
free(*uids);
return -1;
i = 16;
for (;;) {
- grouplist = (gid_t *) malloc (i * sizeof (GETGROUPS_T));
+ grouplist = (gid_t *) mallocarray (i, sizeof (GETGROUPS_T));
if (NULL == grouplist) {
return -1;
}
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;
*/
void initenv (void)
{
- newenvp = (char **) xmalloc (NEWENVP_STEP * sizeof (char *));
+ newenvp = (char **) xmallocarray (NEWENVP_STEP, sizeof (char *));
*newenvp = NULL;
}
*/
/* 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"),
* 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
* 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
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) {
* 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
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
if (sg->sg_mem[0]) {
sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
GETGROUPS_T *groups;
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) bindtextdomain (PACKAGE, LOCALEDIR);
* work if the system library is recompiled.
*/
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
/* don't use getgroups(0, 0) - it doesn't work on some systems */
i = 16;
for (;;) {
- grouplist = (GETGROUPS_T *) xmalloc (i * sizeof (GETGROUPS_T));
+ grouplist = (GETGROUPS_T *) xmallocarray (i, sizeof (GETGROUPS_T));
ngroups = getgroups (i, grouplist);
if (i > ngroups && !(ngroups == -1 && errno == EINVAL)) {
break;
#ifdef USE_PAM
/* keep the list of user/password for later update by PAM */
nusers++;
- lines = reallocf (lines, sizeof (lines[0]) * nusers);
- usernames = reallocf (usernames, sizeof (usernames[0]) * nusers);
- passwords = reallocf (passwords, sizeof (passwords[0]) * nusers);
+ lines = reallocf (lines, nusers, sizeof (lines[0]));
+ usernames = reallocf (usernames, nusers, sizeof (usernames[0]));
+ passwords = reallocf (passwords, nusers, sizeof (passwords[0]));
if (lines == NULL || usernames == NULL || passwords == NULL) {
fprintf (stderr,
_("%s: line %d: %s\n"),
while (NULL != 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[1] = "-";
targs[2] = xstrdup (shellname);
#endif
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
*/
#endif
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;
is_shadow_pwd = spw_file_present ();