From 55d1a1a79d0db5aad0753a4042e0e607f5e0c014 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 19 Mar 2012 11:24:24 -0400 Subject: [PATCH] Use ecalloc() when allocating structs. --- common/sudo_conf.c | 16 ++++++++-------- plugins/sudoers/alias.c | 4 ++-- plugins/sudoers/defaults.c | 6 ++---- plugins/sudoers/env.c | 6 +++--- plugins/sudoers/gram.c | 32 ++++++++++++++++---------------- plugins/sudoers/gram.y | 32 ++++++++++++++++---------------- plugins/sudoers/interfaces.c | 2 +- plugins/sudoers/ldap.c | 21 +++++++-------------- plugins/sudoers/sudoers.c | 2 +- plugins/sudoers/sudoreplay.c | 13 +++++-------- plugins/sudoers/visudo.c | 8 ++++---- src/exec.c | 12 ++++++------ src/exec_pty.c | 15 +++++++-------- src/hooks.c | 2 +- src/load_plugins.c | 4 ++-- 15 files changed, 81 insertions(+), 94 deletions(-) diff --git a/common/sudo_conf.c b/common/sudo_conf.c index bf34254bc..3b7a0cc4e 100644 --- a/common/sudo_conf.c +++ b/common/sudo_conf.c @@ -242,12 +242,12 @@ set_plugin(const char *entry) pathlen = strlen(path); } - info = emalloc(sizeof(*info)); + info = ecalloc(1, sizeof(*info)); info->symbol_name = estrndup(name, namelen); info->path = estrndup(path, pathlen); info->options = options; info->prev = info; - info->next = NULL; + /* info->next = NULL; */ tq_append(&sudo_conf_data.plugins, info); return true; @@ -350,21 +350,21 @@ sudo_conf_read(void) done: if (tq_empty(&sudo_conf_data.plugins)) { /* Default policy plugin */ - info = emalloc(sizeof(*info)); + info = ecalloc(1, sizeof(*info)); info->symbol_name = "sudoers_policy"; info->path = SUDOERS_PLUGIN; - info->options = NULL; + /* info->options = NULL; */ info->prev = info; - info->next = NULL; + /* info->next = NULL; */ tq_append(&sudo_conf_data.plugins, info); /* Default I/O plugin */ - info = emalloc(sizeof(*info)); + info = ecalloc(1, sizeof(*info)); info->symbol_name = "sudoers_io"; info->path = SUDOERS_PLUGIN; - info->options = NULL; + /* info->options = NULL; */ info->prev = info; - info->next = NULL; + /* info->next = NULL; */ tq_append(&sudo_conf_data.plugins, info); } } diff --git a/plugins/sudoers/alias.c b/plugins/sudoers/alias.c index aaba72881..eb95039f2 100644 --- a/plugins/sudoers/alias.c +++ b/plugins/sudoers/alias.c @@ -116,10 +116,10 @@ alias_add(char *name, int type, struct member *members) struct alias *a; debug_decl(alias_add, SUDO_DEBUG_ALIAS) - a = emalloc(sizeof(*a)); + a = ecalloc(1, sizeof(*a)); a->name = name; a->type = type; - a->seqno = 0; + /* a->seqno = 0; */ list2tq(&a->members, members); if (rbinsert(aliases, a)) { snprintf(errbuf, sizeof(errbuf), _("Alias `%s' already defined"), name); diff --git a/plugins/sudoers/defaults.c b/plugins/sudoers/defaults.c index 5046ec2e7..6cab7e44e 100644 --- a/plugins/sudoers/defaults.c +++ b/plugins/sudoers/defaults.c @@ -790,10 +790,8 @@ list_op(char *val, size_t len, struct sudo_defs_types *def, enum list_ops op) /* Add new node to the head of the list. */ if (op == add) { - cur = emalloc(sizeof(struct list_member)); - cur->value = emalloc(len + 1); - (void) memcpy(cur->value, val, len); - cur->value[len] = '\0'; + cur = ecalloc(1, sizeof(struct list_member)); + cur->value = estrndup(val, len); cur->next = def->sd_un.list; def->sd_un.list = cur; } diff --git a/plugins/sudoers/env.c b/plugins/sudoers/env.c index 3d2af7ffc..de3fcba0d 100644 --- a/plugins/sudoers/env.c +++ b/plugins/sudoers/env.c @@ -986,7 +986,7 @@ init_envtables(void) /* Fill in the "env_delete" list. */ for (p = initial_badenv_table; *p; p++) { - cur = emalloc(sizeof(struct list_member)); + cur = ecalloc(1, sizeof(struct list_member)); cur->value = estrdup(*p); cur->next = def_env_delete; def_env_delete = cur; @@ -994,7 +994,7 @@ init_envtables(void) /* Fill in the "env_check" list. */ for (p = initial_checkenv_table; *p; p++) { - cur = emalloc(sizeof(struct list_member)); + cur = ecalloc(1, sizeof(struct list_member)); cur->value = estrdup(*p); cur->next = def_env_check; def_env_check = cur; @@ -1002,7 +1002,7 @@ init_envtables(void) /* Fill in the "env_keep" list. */ for (p = initial_keepenv_table; *p; p++) { - cur = emalloc(sizeof(struct list_member)); + cur = ecalloc(1, sizeof(struct list_member)); cur->value = estrdup(*p); cur->next = def_env_keep; def_env_keep = cur; diff --git a/plugins/sudoers/gram.c b/plugins/sudoers/gram.c index 6d5771b0c..62d6086b8 100644 --- a/plugins/sudoers/gram.c +++ b/plugins/sudoers/gram.c @@ -645,14 +645,14 @@ new_default(char *var, char *val, int op) struct defaults *d; debug_decl(new_default, SUDO_DEBUG_PARSER) - d = emalloc(sizeof(struct defaults)); + d = ecalloc(1, sizeof(struct defaults)); d->var = var; d->val = val; tq_init(&d->binding); - d->type = 0; + /* d->type = 0; */ d->op = op; d->prev = d; - d->next = NULL; + /* d->next = NULL; */ debug_return_ptr(d); } @@ -663,11 +663,11 @@ new_member(char *name, int type) struct member *m; debug_decl(new_member, SUDO_DEBUG_PARSER) - m = emalloc(sizeof(struct member)); + m = ecalloc(1, sizeof(struct member)); m->name = name; m->type = type; m->prev = m; - m->next = NULL; + /* m->next = NULL; */ debug_return_ptr(m); } @@ -712,11 +712,11 @@ add_userspec(struct member *members, struct privilege *privs) struct userspec *u; debug_decl(add_userspec, SUDO_DEBUG_PARSER) - u = emalloc(sizeof(*u)); + u = ecalloc(1, sizeof(*u)); list2tq(&u->users, members); list2tq(&u->privileges, privs); u->prev = u; - u->next = NULL; + /* u->next = NULL; */ tq_append(&userspecs, u); debug_return; @@ -1155,11 +1155,11 @@ break; case 26: #line 269 "gram.y" { - struct privilege *p = emalloc(sizeof(*p)); + struct privilege *p = ecalloc(1, sizeof(*p)); list2tq(&p->hostlist, yyvsp[-2].member); list2tq(&p->cmndlist, yyvsp[0].cmndspec); p->prev = p; - p->next = NULL; + /* p->next = NULL; */ yyval.privilege = p; } break; @@ -1243,7 +1243,7 @@ break; case 36: #line 339 "gram.y" { - struct cmndspec *cs = emalloc(sizeof(*cs)); + struct cmndspec *cs = ecalloc(1, sizeof(*cs)); if (yyvsp[-3].runas != NULL) { list2tq(&cs->runasuserlist, yyvsp[-3].runas->runasusers); list2tq(&cs->runasgrouplist, yyvsp[-3].runas->runasgroups); @@ -1343,15 +1343,15 @@ break; case 48: #line 415 "gram.y" { - yyval.runas = emalloc(sizeof(struct runascontainer)); + yyval.runas = ecalloc(1, sizeof(struct runascontainer)); yyval.runas->runasusers = yyvsp[0].member; - yyval.runas->runasgroups = NULL; + /* $$->runasgroups = NULL; */ } break; case 49: #line 420 "gram.y" { - yyval.runas = emalloc(sizeof(struct runascontainer)); + yyval.runas = ecalloc(1, sizeof(struct runascontainer)); yyval.runas->runasusers = yyvsp[-2].member; yyval.runas->runasgroups = yyvsp[0].member; } @@ -1359,8 +1359,8 @@ break; case 50: #line 425 "gram.y" { - yyval.runas = emalloc(sizeof(struct runascontainer)); - yyval.runas->runasusers = NULL; + yyval.runas = ecalloc(1, sizeof(struct runascontainer)); + /* $$->runasusers = NULL; */ yyval.runas->runasgroups = yyvsp[0].member; } break; @@ -1446,7 +1446,7 @@ break; case 64: #line 474 "gram.y" { - struct sudo_command *c = emalloc(sizeof(*c)); + struct sudo_command *c = ecalloc(1, sizeof(*c)); c->cmnd = yyvsp[0].command.cmnd; c->args = yyvsp[0].command.args; yyval.member = new_member((char *)c, COMMAND); diff --git a/plugins/sudoers/gram.y b/plugins/sudoers/gram.y index c35571cf0..0315f2437 100644 --- a/plugins/sudoers/gram.y +++ b/plugins/sudoers/gram.y @@ -267,11 +267,11 @@ privileges : privilege ; privilege : hostlist '=' cmndspeclist { - struct privilege *p = emalloc(sizeof(*p)); + struct privilege *p = ecalloc(1, sizeof(*p)); list2tq(&p->hostlist, $1); list2tq(&p->cmndlist, $3); p->prev = p; - p->next = NULL; + /* p->next = NULL; */ $$ = p; } ; @@ -337,7 +337,7 @@ cmndspeclist : cmndspec ; cmndspec : runasspec selinux cmndtag opcmnd { - struct cmndspec *cs = emalloc(sizeof(*cs)); + struct cmndspec *cs = ecalloc(1, sizeof(*cs)); if ($1 != NULL) { list2tq(&cs->runasuserlist, $1->runasusers); list2tq(&cs->runasgrouplist, $1->runasgroups); @@ -413,18 +413,18 @@ runasspec : /* empty */ { ; runaslist : userlist { - $$ = emalloc(sizeof(struct runascontainer)); + $$ = ecalloc(1, sizeof(struct runascontainer)); $$->runasusers = $1; - $$->runasgroups = NULL; + /* $$->runasgroups = NULL; */ } | userlist ':' grouplist { - $$ = emalloc(sizeof(struct runascontainer)); + $$ = ecalloc(1, sizeof(struct runascontainer)); $$->runasusers = $1; $$->runasgroups = $3; } | ':' grouplist { - $$ = emalloc(sizeof(struct runascontainer)); - $$->runasusers = NULL; + $$ = ecalloc(1, sizeof(struct runascontainer)); + /* $$->runasusers = NULL; */ $$->runasgroups = $2; } ; @@ -472,7 +472,7 @@ cmnd : ALL { $$ = new_member($1, ALIAS); } | COMMAND { - struct sudo_command *c = emalloc(sizeof(*c)); + struct sudo_command *c = ecalloc(1, sizeof(*c)); c->cmnd = $1.cmnd; c->args = $1.args; $$ = new_member((char *)c, COMMAND); @@ -614,14 +614,14 @@ new_default(char *var, char *val, int op) struct defaults *d; debug_decl(new_default, SUDO_DEBUG_PARSER) - d = emalloc(sizeof(struct defaults)); + d = ecalloc(1, sizeof(struct defaults)); d->var = var; d->val = val; tq_init(&d->binding); - d->type = 0; + /* d->type = 0; */ d->op = op; d->prev = d; - d->next = NULL; + /* d->next = NULL; */ debug_return_ptr(d); } @@ -632,11 +632,11 @@ new_member(char *name, int type) struct member *m; debug_decl(new_member, SUDO_DEBUG_PARSER) - m = emalloc(sizeof(struct member)); + m = ecalloc(1, sizeof(struct member)); m->name = name; m->type = type; m->prev = m; - m->next = NULL; + /* m->next = NULL; */ debug_return_ptr(m); } @@ -681,11 +681,11 @@ add_userspec(struct member *members, struct privilege *privs) struct userspec *u; debug_decl(add_userspec, SUDO_DEBUG_PARSER) - u = emalloc(sizeof(*u)); + u = ecalloc(1, sizeof(*u)); list2tq(&u->users, members); list2tq(&u->privileges, privs); u->prev = u; - u->next = NULL; + /* u->next = NULL; */ tq_append(&userspecs, u); debug_return; diff --git a/plugins/sudoers/interfaces.c b/plugins/sudoers/interfaces.c index 04d5b6704..624882e27 100644 --- a/plugins/sudoers/interfaces.c +++ b/plugins/sudoers/interfaces.c @@ -71,7 +71,7 @@ set_interfaces(const char *ai) *mask++ = '\0'; /* Parse addr and store in list. */ - ifp = emalloc(sizeof(*ifp)); + ifp = ecalloc(1, sizeof(*ifp)); if (strchr(addr, ':')) { /* IPv6 */ #ifdef HAVE_STRUCT_IN6_ADDR diff --git a/plugins/sudoers/ldap.c b/plugins/sudoers/ldap.c index 126a4858a..e4bfe021d 100644 --- a/plugins/sudoers/ldap.c +++ b/plugins/sudoers/ldap.c @@ -1984,14 +1984,7 @@ sudo_ldap_result_alloc(void) struct ldap_result *result; debug_decl(sudo_ldap_result_alloc, SUDO_DEBUG_LDAP) - result = emalloc(sizeof(*result)); - result->searches = NULL; - result->nentries = 0; - result->entries = NULL; - result->allocated_entries = 0; - result->user_matches = false; - result->host_matches = false; - debug_return_ptr(result); + debug_return_ptr(ecalloc(1, sizeof(*result))); } /* @@ -2030,10 +2023,10 @@ sudo_ldap_result_add_search(struct ldap_result *lres, LDAP *ldap, struct ldap_search_list *s, *news; debug_decl(sudo_ldap_result_add_search, SUDO_DEBUG_LDAP) - news = emalloc(sizeof(struct ldap_search_list)); - news->next = NULL; + news = ecalloc(1, sizeof(struct ldap_search_list)); news->ldap = ldap; news->searchresult = searchresult; + /* news->next = NULL; */ /* Add entry to the end of the chain (XXX - tailq instead?). */ if (lres->searches) { @@ -2200,11 +2193,11 @@ sudo_ldap_open(struct sudo_nss *nss) debug_return_int(-1); /* Create a handle container. */ - handle = emalloc(sizeof(struct sudo_ldap_handle)); + handle = ecalloc(1, sizeof(struct sudo_ldap_handle)); handle->ld = ld; - handle->result = NULL; - handle->username = NULL; - handle->grlist = NULL; + /* handle->result = NULL; */ + /* handle->username = NULL; */ + /* handle->grlist = NULL; */ nss->handle = handle; debug_return_int(0); diff --git a/plugins/sudoers/sudoers.c b/plugins/sudoers/sudoers.c index 7b35d2863..26bb926ed 100644 --- a/plugins/sudoers/sudoers.c +++ b/plugins/sudoers/sudoers.c @@ -884,7 +884,7 @@ set_cmnd(void) /* Resolve the path and return. */ rval = FOUND; - user_stat = emalloc(sizeof(struct stat)); + user_stat = ecalloc(1, sizeof(struct stat)); /* Default value for cmnd, overridden below. */ if (user_cmnd == NULL) diff --git a/plugins/sudoers/sudoreplay.c b/plugins/sudoers/sudoreplay.c index df83a50e0..db98e6cc2 100644 --- a/plugins/sudoers/sudoreplay.c +++ b/plugins/sudoers/sudoreplay.c @@ -200,7 +200,6 @@ extern time_t get_date(char *); extern char *get_timestr(time_t, int); extern int term_raw(int, int); extern int term_restore(int, int); -extern void zero_bytes(volatile void *, size_t); void cleanup(int); static int list_sessions(int, char **, const char *, const char *, const char *); @@ -368,7 +367,7 @@ main(int argc, char *argv[]) fclose(lfile); fflush(stdout); - zero_bytes(&sa, sizeof(sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESETHAND; sa.sa_handler = cleanup; @@ -391,8 +390,7 @@ main(int argc, char *argv[]) if (!term_raw(STDIN_FILENO, 1)) error(1, _("unable to set tty to raw mode")); } - fdsw = (fd_set *)emalloc2(howmany(STDOUT_FILENO + 1, NFDBITS), - sizeof(fd_mask)); + fdsw = ecalloc(howmany(STDOUT_FILENO + 1, NFDBITS), sizeof(fd_mask)); /* * Timing file consists of line of the format: "%f %d\n" @@ -590,11 +588,11 @@ parse_expr(struct search_node **headp, char *argv[]) } /* Allocate new search node */ - newsn = emalloc(sizeof(*newsn)); - newsn->next = NULL; + newsn = ecalloc(1, sizeof(*newsn)); newsn->type = type; newsn->or = or; newsn->negated = not; + /* newsn->next = NULL; */ if (type == ST_EXPR) { av += parse_expr(&newsn->u.expr, av + 1); } else { @@ -871,8 +869,7 @@ check_input(int ttyfd, double *speed) ssize_t n; debug_decl(check_input, SUDO_DEBUG_UTIL) - fdsr = (fd_set *)emalloc2(howmany(ttyfd + 1, NFDBITS), sizeof(fd_mask)); - + fdsr = ecalloc(howmany(ttyfd + 1, NFDBITS), sizeof(fd_mask)); for (;;) { FD_SET(ttyfd, fdsr); tv.tv_sec = 0; diff --git a/plugins/sudoers/visudo.c b/plugins/sudoers/visudo.c index 72cd5bfd9..5aadabb09 100644 --- a/plugins/sudoers/visudo.c +++ b/plugins/sudoers/visudo.c @@ -880,13 +880,13 @@ open_sudoers(const char *path, bool doedit, bool *keepopen) break; } if (entry == NULL) { - entry = emalloc(sizeof(*entry)); + entry = ecalloc(1, sizeof(*entry)); entry->path = estrdup(path); - entry->modified = 0; + /* entry->modified = 0; */ entry->prev = entry; - entry->next = NULL; + /* entry->next = NULL; */ entry->fd = open(entry->path, open_flags, SUDOERS_MODE); - entry->tpath = NULL; + /* entry->tpath = NULL; */ entry->doedit = doedit; if (entry->fd == -1) { warning("%s", entry->path); diff --git a/src/exec.c b/src/exec.c index 80e87177a..b48500649 100644 --- a/src/exec.c +++ b/src/exec.c @@ -320,11 +320,11 @@ sudo_execute(struct command_details *details, struct command_status *cstat) * In the event loop we pass input from user tty to master * and pass output from master to stdout and IO plugin. */ - fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); - fdsw = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); + fdsr = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); + fdsw = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); for (;;) { - zero_bytes(fdsw, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); - zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + memset(fdsw, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + memset(fdsr, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); FD_SET(signal_pipe[0], fdsr); FD_SET(sv[0], fdsr); @@ -582,9 +582,9 @@ schedule_signal(int signo) sudo_debug_printf(SUDO_DEBUG_DIAG, "forwarding signal %d to child", signo); - sigfwd = emalloc(sizeof(*sigfwd)); + sigfwd = ecalloc(1, sizeof(*sigfwd)); sigfwd->prev = sigfwd; - sigfwd->next = NULL; + /* sigfwd->next = NULL; */ sigfwd->signo = signo; tq_append(&sigfwd_list, sigfwd); diff --git a/src/exec_pty.c b/src/exec_pty.c index 07ce0708c..c78e793e5 100644 --- a/src/exec_pty.c +++ b/src/exec_pty.c @@ -405,7 +405,7 @@ io_buf_new(int rfd, int wfd, bool (*action)(const char *, unsigned int), struct io_buffer *iob; debug_decl(io_buf_new, SUDO_DEBUG_EXEC); - iob = emalloc(sizeof(*iob)); + iob = ecalloc(1, sizeof(*iob)); zero_bytes(iob, sizeof(*iob)); iob->rfd = rfd; iob->wfd = wfd; @@ -989,9 +989,8 @@ exec_monitor(struct command_details *details, int backchannel) /* Wait for errno on pipe, signal on backchannel or for SIGCHLD */ maxfd = MAX(MAX(errpipe[0], signal_pipe[0]), backchannel); - fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); - zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); - zero_bytes(&cstat, sizeof(cstat)); + fdsr = ecalloc(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); + memset(&cstat, 0, sizeof(cstat)); tv.tv_sec = 0; tv.tv_usec = 0; for (;;) { @@ -1102,11 +1101,11 @@ flush_output(void) if (maxfd == -1) debug_return; - fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); - fdsw = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); + fdsr = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); + fdsw = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); for (;;) { - zero_bytes(fdsw, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); - zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + memset(fdsw, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + memset(fdsr, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); nwriters = 0; for (iob = iobufs; iob; iob = iob->next) { diff --git a/src/hooks.c b/src/hooks.c index 5e8a395ed..b2daaa95f 100644 --- a/src/hooks.c +++ b/src/hooks.c @@ -171,7 +171,7 @@ register_hook_internal(struct sudo_hook_list **head, struct sudo_hook_list *hook; debug_decl(register_hook_internal, SUDO_DEBUG_HOOKS) - hook = emalloc(sizeof(*hook)); + hook = ecalloc(1, sizeof(*hook)); hook->u.generic_fn = hook_fn; hook->closure = closure; hook->next = *head; diff --git a/src/load_plugins.c b/src/load_plugins.c index 45d63a129..767f8ee70 100644 --- a/src/load_plugins.c +++ b/src/load_plugins.c @@ -134,9 +134,9 @@ sudo_load_plugins(struct plugin_container *policy_plugin, policy_plugin->options = info->options; policy_plugin->u.generic = plugin; } else if (plugin->type == SUDO_IO_PLUGIN) { - container = emalloc(sizeof(*container)); + container = ecalloc(1, sizeof(*container)); container->prev = container; - container->next = NULL; + /* container->next = NULL; */ container->handle = handle; container->name = info->symbol_name; container->options = info->options; -- 2.40.0