/*
- * Copyright (c) 1999-2005, 2007, 2010-2013
+ * Copyright (c) 1999-2005, 2007, 2010-2014
* Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
/*
* If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
* could be signed (as it is on SunOS 4.x). This just means that
- * emalloc2() and erealloc3() cannot allocate huge amounts on such a
+ * emallocarray() and ereallocarray() cannot allocate huge amounts on such a
* platform but that is OK since sudo doesn't need to do so anyway.
*/
#ifndef SIZE_MAX
}
/*
- * emalloc2() allocates nmemb * size bytes and exits with an error
+ * emallocarray() allocates nmemb * size bytes and exits with an error
* if overflow would occur or if the system malloc(3) fails.
*/
void *
-emalloc2(size_t nmemb, size_t size)
+emallocarray(size_t nmemb, size_t size)
{
void *ptr;
if (nmemb == 0 || size == 0)
- fatalx_nodebug(_("internal error, tried to emalloc2(0)"));
+ fatalx_nodebug(_("internal error, tried to emallocarray(0)"));
if (nmemb > SIZE_MAX / size)
- fatalx_nodebug(_("internal error, %s overflow"), "emalloc2");
+ fatalx_nodebug(_("internal error, %s overflow"), "emallocarray");
size *= nmemb;
if ((ptr = malloc(size)) == NULL)
}
/*
- * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
+ * ereallocarray() realloc(3)s nmemb * size bytes and exits with an error
* if overflow would occur or if the system malloc(3)/realloc(3) fails.
* You can call erealloc() with a NULL pointer even if the system realloc(3)
* does not support this.
*/
void *
-erealloc3(void *ptr, size_t nmemb, size_t size)
+ereallocarray(void *ptr, size_t nmemb, size_t size)
{
if (nmemb == 0 || size == 0)
- fatalx_nodebug(_("internal error, tried to erealloc3(0)"));
+ fatalx_nodebug(_("internal error, tried to ereallocarray(0)"));
if (nmemb > SIZE_MAX / size)
- fatalx_nodebug(_("internal error, %s overflow"), "erealloc3");
+ fatalx_nodebug(_("internal error, %s overflow"), "ereallocarray");
size *= nmemb;
ptr = ptr ? realloc(ptr, size) : malloc(size);
/*
- * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2013-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
base->pfd_high = -1;
base->pfd_max = 32;
- base->pfds = erealloc3(NULL, base->pfd_max, sizeof(struct pollfd));
+ base->pfds = ereallocarray(NULL, base->pfd_max, sizeof(struct pollfd));
for (i = 0; i < base->pfd_max; i++) {
base->pfds[i].fd = -1;
}
int i;
base->pfd_max <<= 1;
base->pfds =
- erealloc3(base->pfds, base->pfd_max, sizeof(struct pollfd));
+ ereallocarray(base->pfds, base->pfd_max, sizeof(struct pollfd));
for (i = base->pfd_free; i < base->pfd_max; i++) {
base->pfds[i].fd = -1;
}
ngids++;
/* Allocate and fill in array. */
if (ngids != 0) {
- gids = emalloc2(ngids, sizeof(GETGROUPS_T));
+ gids = emallocarray(ngids, sizeof(GETGROUPS_T));
ngids = 0;
if (basegid != NULL)
gids[ngids++] = *basegid;
while (isblank((unsigned char)*ep))
ep++;
}
- options = emalloc2(nopts + 1, sizeof(*options));
+ options = emallocarray(nopts + 1, sizeof(*options));
/* Fill in options array, there is at least one element. */
for (nopts = 0; (ep = strpbrk(cp, " \t")) != NULL; ) {
options[nopts++] = estrndup(cp, (size_t)(ep - cp));
/*
- * Copyright (c) 2009-2010, 2012-1013
+ * Copyright (c) 2009-2010, 2012-1014
* Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
int evasprintf(char **, const char *, va_list) __printflike(2, 0);
void *ecalloc(size_t, size_t) __malloc_like;
void *emalloc(size_t) __malloc_like;
-void *emalloc2(size_t, size_t) __malloc_like;
+void *emallocarray(size_t, size_t) __malloc_like;
void *erealloc(void *, size_t);
-void *erealloc3(void *, size_t, size_t);
+void *ereallocarray(void *, size_t, size_t);
void *erecalloc(void *, size_t, size_t, size_t);
char *estrdup(const char *) __malloc_like;
char *estrndup(const char *, size_t) __malloc_like;
/*
- * Copyright (c) 1999-2005, 2007, 2010-2013
+ * Copyright (c) 1999-2005, 2007, 2010-2014
* Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
/* Rebuild argv for sia_ses_init() */
sudo_argc = NewArgc + 1;
- sudo_argv = emalloc2(sudo_argc + 1, sizeof(char *));
+ sudo_argv = emallocarray(sudo_argc + 1, sizeof(char *));
sudo_argv[0] = "sudo";
for (i = 0; i < NewArgc; i++)
sudo_argv[i + 1] = NewArgv[i];
/*
- * Copyright (c) 2000-2005, 2007-2013
+ * Copyright (c) 2000-2005, 2007-2014
* Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
/*
* If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
* could be signed (as it is on SunOS 4.x). This just means that
- * emalloc2() and erealloc3() cannot allocate huge amounts on such a
- * platform but that is OK since sudo doesn't need to do so anyway.
+ * we cannot allocate huge amounts on such a platform but that is OK
+ * since sudo doesn't need to do so anyway.
*/
#ifndef SIZE_MAX
# ifdef SIZE_T_MAX
env.env_len = len;
env.env_size = len + 1 + 128;
- env.envp = emalloc2(env.env_size, sizeof(char *));
+ env.envp = emallocarray(env.env_size, sizeof(char *));
#ifdef ENV_DEBUG
memset(env.envp, 0, env.env_size * sizeof(char *));
#endif
env.env_len = 0;
env.env_size = 128;
old_envp = env.envp;
- env.envp = emalloc2(env.env_size, sizeof(char *));
+ env.envp = emallocarray(env.env_size, sizeof(char *));
#ifdef ENV_DEBUG
memset(env.envp, 0, env.env_size * sizeof(char *));
#else
/*
- * Copyright (c) 2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2010-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
}
}
if (ac != 0) {
- argv = emalloc2(ac, sizeof(char *));
+ argv = emallocarray(ac, sizeof(char *));
ac = 0;
for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
argv[ac++] = cp;
*/
if (++lres->nentries > lres->allocated_entries) {
lres->allocated_entries += ALLOCATION_INCREMENT;
- lres->entries = erealloc3(lres->entries, lres->allocated_entries,
+ lres->entries = ereallocarray(lres->entries, lres->allocated_entries,
sizeof(lres->entries[0]));
}
/*
- * Copyright (c) 1996, 1998-2005, 2007-2013
+ * Copyright (c) 1996, 1998-2005, 2007-2014
* Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
} else {
if (sudo_user.max_groups > 0) {
ngids = sudo_user.max_groups;
- gids = emalloc2(ngids, sizeof(GETGROUPS_T));
+ gids = emallocarray(ngids, sizeof(GETGROUPS_T));
(void)getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids);
} else {
#if defined(HAVE_SYSCONF) && defined(_SC_NGROUPS_MAX)
if (ngids < 0)
#endif
ngids = NGROUPS_MAX * 2;
- gids = emalloc2(ngids, sizeof(GETGROUPS_T));
+ gids = emallocarray(ngids, sizeof(GETGROUPS_T));
if (getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids) == -1) {
efree(gids);
- gids = emalloc2(ngids, sizeof(GETGROUPS_T));
+ gids = emallocarray(ngids, sizeof(GETGROUPS_T));
if (getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids) == -1)
ngids = -1;
}
/*
- * Copyright (c) 2003-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2003-2014 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2011 Daniel Kopecek <dkopecek@redhat.com>
*
* This code is derived from software contributed by Aaron Spangler.
dst->name = estrdup(src->name);
dst->num_values = src->num_values;
- dst->values = emalloc2(dst->num_values, sizeof(char *));
+ dst->values = emallocarray(dst->num_values, sizeof(char *));
for (i = 0; i < dst->num_values; ++i)
dst->values[i] = estrdup(src->values[i]);
sudo_debug_printf(SUDO_DEBUG_INFO, "emalloc: cnt=%d", src->num_attrs);
dst->num_attrs = src->num_attrs;
- dst->attrs = emalloc2(dst->num_attrs, sizeof(struct sss_sudo_attr));
+ dst->attrs = emallocarray(dst->num_attrs, sizeof(struct sss_sudo_attr));
for (i = 0; i < dst->num_attrs; ++i)
sudo_sss_attrcpy(dst->attrs + i, src->attrs + i);
out_res = emalloc(sizeof(struct sss_sudo_result));
out_res->rules = in_res->num_rules > 0 ?
- emalloc2(in_res->num_rules, sizeof(struct sss_sudo_rule)) : NULL;
+ emallocarray(in_res->num_rules, sizeof(struct sss_sudo_rule)) : NULL;
out_res->num_rules = 0;
for (i = l = 0; i < in_res->num_rules; ++i) {
in_res->num_rules, l);
if (l > 0) {
out_res->rules =
- erealloc3(out_res->rules, l, sizeof(struct sss_sudo_rule));
+ ereallocarray(out_res->rules, l, sizeof(struct sss_sudo_rule));
} else {
efree(out_res->rules);
out_res->rules = NULL;
*/
if (argc == 0) {
NewArgc = 1;
- NewArgv = emalloc2(NewArgc + 1, sizeof(char *));
+ NewArgv = emallocarray(NewArgc + 1, sizeof(char *));
NewArgv[0] = user_cmnd;
NewArgv[1] = NULL;
} else {
/* Must leave an extra slot before NewArgv for bash's --login */
NewArgc = argc;
- NewArgv = emalloc2(NewArgc + 2, sizeof(char *));
+ NewArgv = emallocarray(NewArgc + 2, sizeof(char *));
memcpy(++NewArgv, argv, argc * sizeof(char *));
NewArgv[NewArgc] = NULL;
if (ISSET(sudo_mode, MODE_LOGIN_SHELL) && runas_pw != NULL)
efree(editor);
debug_return_str(NULL);
}
- nargv = (char **) emalloc2(nargc + 1 + nfiles + 1, sizeof(char *));
+ nargv = (char **) emallocarray(nargc + 1 + nfiles + 1, sizeof(char *));
for (ac = 0; cp != NULL && ac < nargc; ac++) {
nargv[ac] = cp;
cp = strtok(NULL, " \t");
/*
- * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2009-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
/* Store the line in iov followed by \r\n pair. */
if (iovcnt + 3 > iovmax) {
iov = iovmax ?
- erealloc3(iov, iovmax <<= 1, sizeof(*iov)) :
- emalloc2(iovmax = 32, sizeof(*iov));
+ ereallocarray(iov, iovmax <<= 1, sizeof(*iov)) :
+ emallocarray(iovmax = 32, sizeof(*iov));
}
linelen = (size_t)(ep - cp) + 1;
iov[iovcnt].iov_base = cp;
pathbuf[sdlen] = '\0';
/* Store potential session dirs for sorting. */
- sessions = emalloc2(sessions_size, sizeof(char *));
+ sessions = emallocarray(sessions_size, sizeof(char *));
while ((dp = readdir(d)) != NULL) {
/* Skip "." and ".." */
if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
/* Add name to session list. */
if (sessions_len + 1 > sessions_size) {
sessions_size <<= 1;
- sessions = erealloc3(sessions, sessions_size, sizeof(char *));
+ sessions = ereallocarray(sessions, sessions_size, sizeof(char *));
}
sessions[sessions_len++] = estrdup(dp->d_name);
}
/*
- * Copyright (c) 1996, 1998-2005, 2007-2013
+ * Copyright (c) 1996, 1998-2005, 2007-2014
* Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
}
/* Build up argument vector for the command */
- av = emalloc2(ac, sizeof(char *));
+ av = emallocarray(ac, sizeof(char *));
if ((av[0] = strrchr(editor, '/')) != NULL)
av[0]++;
else
/*
- * Copyright (c) 2010, 2012 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2010, 2012, 2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
/* Append at the end if not already found. */
if (!found) {
size_t env_len = (size_t)(ep - environ);
- char **envp = erealloc3(priv_environ, env_len + 2, sizeof(char *));
+ char **envp = ereallocarray(priv_environ, env_len + 2, sizeof(char *));
if (environ != priv_environ)
memcpy(envp, environ, env_len * sizeof(char *));
envp[env_len++] = (char *)string;
/*
- * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2009-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
if (!enabled)
env_size++;
#endif
- nenvp = emalloc2(env_size, sizeof(*envp));
+ nenvp = emallocarray(env_size, sizeof(*envp));
memcpy(nenvp, envp, env_len * sizeof(*envp));
nenvp[env_len] = NULL;
for (argc = 0; argv[argc] != NULL; argc++)
continue;
- nargv = emalloc2(argc + 2, sizeof(char *));
+ nargv = emallocarray(argc + 2, sizeof(char *));
nargv[0] = "sh";
nargv[1] = (char *)path;
memcpy(nargv + 2, argv + 1, argc * sizeof(char *));
/*
- * Copyright (c) 1993-1996, 1998-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1993-1996, 1998-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
int env_size = 32;
debug_decl(parse_args, SUDO_DEBUG_ARGS)
- env_add = emalloc2(env_size, sizeof(char *));
+ env_add = emallocarray(env_size, sizeof(char *));
/* Pass progname to plugin so it can call initprogname() */
sudo_settings[ARG_PROGNAME].value = getprogname();
} else if (!got_end_of_args && is_envar) {
if (nenv == env_size - 2) {
env_size *= 2;
- env_add = erealloc3(env_add, env_size, sizeof(char *));
+ env_add = ereallocarray(env_add, env_size, sizeof(char *));
}
env_add[nenv++] = argv[optind];
size_t cmnd_size = (size_t) (argv[argc - 1] - argv[0]) +
strlen(argv[argc - 1]) + 1;
- cmnd = dst = emalloc2(cmnd_size, 2);
+ cmnd = dst = emallocarray(cmnd_size, 2);
for (av = argv; *av != NULL; av++) {
for (src = *av; *src != '\0'; src++) {
/* quote potential meta characters */
ac += 2; /* -c cmnd */
}
- av = emalloc2(ac + 1, sizeof(char *));
+ av = emallocarray(ac + 1, sizeof(char *));
av[0] = (char *)user_details.shell; /* plugin may override shell */
if (cmnd != NULL) {
av[1] = "-c";
#ifdef _PATH_SUDO_PLUGIN_DIR
sudo_settings[ARG_PLUGIN_DIR].value = sudo_conf_plugin_dir_path();
#endif
- settings = emalloc2(NUM_SETTINGS + 1, sizeof(char *));
+ settings = emallocarray(NUM_SETTINGS + 1, sizeof(char *));
for (i = 0, j = 0; i < NUM_SETTINGS; i++) {
if (sudo_settings[i].value) {
sudo_debug_printf(SUDO_DEBUG_INFO, "settings: %s=%s",
/*
- * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2009-2014 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2008 Dan Walsh <dwalsh@redhat.com>
*
* Borrowed heavily from newrole source code
*/
for (argc = 0; argv[argc] != NULL; argc++)
continue;
- nargv = emalloc2(argc + 2, sizeof(char *));
+ nargv = emallocarray(argc + 2, sizeof(char *));
if (noexec)
nargv[0] = *argv[0] == '-' ? "-sesh-noexec" : "sesh-noexec";
else
/*
- * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2009-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
*/
ud->ngroups = sudo_conf_max_groups();
if (ud->ngroups > 0) {
- ud->groups = emalloc2(ud->ngroups, sizeof(GETGROUPS_T));
+ ud->groups = emallocarray(ud->ngroups, sizeof(GETGROUPS_T));
/* No error on insufficient space if user specified max_groups. */
(void)getgrouplist(ud->username, ud->gid, ud->groups, &ud->ngroups);
rval = 0;
for (tries = 0; tries < 10 && rval == -1; tries++) {
ud->ngroups <<= 1;
efree(ud->groups);
- ud->groups = emalloc2(ud->ngroups, sizeof(GETGROUPS_T));
+ ud->groups = emallocarray(ud->ngroups, sizeof(GETGROUPS_T));
rval = getgrouplist(ud->username, ud->gid, ud->groups, &ud->ngroups);
}
}
if ((ud->ngroups = getgroups(0, NULL)) > 0) {
/* Use groups from kernel if not too many or source is static. */
if (ud->ngroups < maxgroups || group_source == GROUP_SOURCE_STATIC) {
- ud->groups = emalloc2(ud->ngroups, sizeof(GETGROUPS_T));
+ ud->groups = emallocarray(ud->ngroups, sizeof(GETGROUPS_T));
if (getgroups(ud->ngroups, ud->groups) < 0) {
efree(ud->groups);
ud->groups = NULL;
debug_decl(get_user_info, SUDO_DEBUG_UTIL)
/* XXX - bound check number of entries */
- user_info = emalloc2(32, sizeof(char *));
+ user_info = emallocarray(32, sizeof(char *));
ud->pid = getpid();
ud->ppid = getppid();
/*
- * Copyright (c) 2004-2008, 2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2004-2008, 2010-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* For each file specified by the user, make a temporary version
* and copy the contents of the original to it.
*/
- tf = emalloc2(nfiles, sizeof(*tf));
+ tf = emallocarray(nfiles, sizeof(*tf));
memset(tf, 0, nfiles * sizeof(*tf));
for (i = 0, j = 0; i < nfiles; i++) {
rc = -1;
* to create a new argv.
*/
nargc = editor_argc + nfiles;
- nargv = (char **) emalloc2(nargc + 1, sizeof(char *));
+ nargv = (char **) emallocarray(nargc + 1, sizeof(char *));
for (ac = 0; ac < editor_argc; ac++)
nargv[ac] = command_details->argv[ac];
for (i = 0; i < nfiles && ac < nargc; )
/* Add to list of subdirs to search. */
if (num_subdirs + 1 > max_subdirs) {
max_subdirs += 64;
- subdirs = erealloc3(subdirs, max_subdirs, sizeof(char *));
+ subdirs = ereallocarray(subdirs, max_subdirs, sizeof(char *));
}
subdirs[num_subdirs++] = estrdup(pathbuf);
}