From: Todd C. Miller Date: Fri, 30 Mar 2012 19:26:01 +0000 (-0400) Subject: Use ecalloc() when allocating structs. X-Git-Tag: SUDO_1_7_9p1~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=81a83b8948c6c1e017f3d66c49b92e2fbe07c283;p=sudo Use ecalloc() when allocating structs. --HG-- branch : 1.7 --- diff --git a/alias.c b/alias.c index 417715364..4c9920274 100644 --- a/alias.c +++ b/alias.c @@ -119,10 +119,10 @@ alias_add(name, type, members) static char errbuf[512]; struct alias *a; - 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/defaults.c b/defaults.c index c1f0afa76..dfe668d5f 100644 --- a/defaults.c +++ b/defaults.c @@ -833,7 +833,7 @@ list_op(val, len, def, op) /* Add new node to the head of the list. */ if (op == add) { - cur = emalloc(sizeof(struct list_member)); + cur = ecalloc(1, sizeof(struct list_member)); cur->value = emalloc(len + 1); (void) memcpy(cur->value, val, len); cur->value[len] = '\0'; diff --git a/env.c b/env.c index 8a7ba5315..05f7e7411 100644 --- a/env.c +++ b/env.c @@ -1021,7 +1021,7 @@ init_envtables() /* 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; @@ -1029,7 +1029,7 @@ init_envtables() /* 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; @@ -1037,7 +1037,7 @@ init_envtables() /* 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/exec.c b/exec.c index 18266378e..965f89d75 100644 --- a/exec.c +++ b/exec.c @@ -351,11 +351,11 @@ sudo_execve(path, argv, envp, uid, cstat, dowait, bgmode) * 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); @@ -619,9 +619,9 @@ schedule_signal(signo) { struct sigforward *sigfwd; - 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/exec_pty.c b/exec_pty.c index 7a47ccb70..4020b3dc5 100644 --- a/exec_pty.c +++ b/exec_pty.c @@ -261,8 +261,7 @@ io_buf_new(rfd, wfd, action, head) { struct io_buffer *iob; - iob = emalloc(sizeof(*iob)); - zero_bytes(iob, sizeof(*iob)); + iob = ecalloc(1, sizeof(*iob)); iob->rfd = rfd; iob->wfd = wfd; iob->action = action; @@ -822,8 +821,7 @@ exec_monitor(path, argv, envp, backchannel, rbac) /* 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)); + fdsr = ecalloc(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); zero_bytes(&cstat, sizeof(cstat)); tv.tv_sec = 0; tv.tv_usec = 0; @@ -932,11 +930,11 @@ flush_output() if (maxfd == -1) 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/ldap.c b/ldap.c index 9698c2ca1..5af0c82ad 100644 --- a/ldap.c +++ b/ldap.c @@ -1996,16 +1996,7 @@ sudo_ldap_set_options(ld) static struct ldap_result * sudo_ldap_result_alloc() { - struct ldap_result *result; - - 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; - return result; + return ecalloc(1, sizeof(struct ldap_result)); } /* @@ -2044,10 +2035,10 @@ sudo_ldap_result_add_search(lres, ldap, searchresult) { struct ldap_search_list *s, *news; - 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) { @@ -2212,11 +2203,11 @@ sudo_ldap_open(nss) return -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->groups = NULL; + /* handle->result = NULL; */ + /* handle->username = NULL; * + /* handle->groups = NULL; * nss->handle = handle; return 0; diff --git a/pwutil.c b/pwutil.c index 985b73f92..9c2a8b471 100644 --- a/pwutil.c +++ b/pwutil.c @@ -254,10 +254,10 @@ sudo_getpwuid(uid) errorx(1, "unable to cache uid %u (%s), already exists", (unsigned int) uid, item->d.pw->pw_name); } else { - item = emalloc(sizeof(*item)); + item = ecalloc(1, sizeof(*item)); item->refcnt = 1; item->k.uid = uid; - item->d.pw = NULL; + /* item->d.pw = NULL; */ if (rbinsert(pwcache_byuid, item) != NULL) errorx(1, "unable to cache uid %u, already exists", (unsigned int) uid); @@ -299,11 +299,11 @@ sudo_getpwnam(name) errorx(1, "unable to cache user %s, already exists", name); } else { len = strlen(name) + 1; - item = emalloc(sizeof(*item) + len); + item = ecalloc(1, sizeof(*item) + len); item->refcnt = 1; item->k.name = (char *) item + sizeof(*item); memcpy(item->k.name, name, len); - item->d.pw = NULL; + /* item->d.pw = NULL; */ if (rbinsert(pwcache_byname, item) != NULL) errorx(1, "unable to cache user %s, already exists", name); } @@ -333,8 +333,7 @@ sudo_fakepwnamid(user, uid, gid) sizeof("/") /* pw_dir */ + sizeof(_PATH_BSHELL); for (i = 0; i < 2; i++) { - item = emalloc(len); - zero_bytes(item, sizeof(*item) + sizeof(*pw)); + item = ecalloc(1, len); pw = (struct passwd *) ((char *)item + sizeof(*item)); pw->pw_uid = uid; pw->pw_gid = gid; @@ -558,10 +557,10 @@ sudo_getgrgid(gid) errorx(1, "unable to cache gid %u (%s), already exists", (unsigned int) gid, key.d.gr->gr_name); } else { - item = emalloc(sizeof(*item)); + item = ecalloc(1, sizeof(*item)); item->refcnt = 1; item->k.gid = gid; - item->d.gr = NULL; + /* item->d.gr = NULL; */ if (rbinsert(grcache_bygid, item) != NULL) errorx(1, "unable to cache gid %u, already exists", (unsigned int) gid); @@ -596,11 +595,11 @@ sudo_getgrnam(name) errorx(1, "unable to cache group %s, already exists", name); } else { len = strlen(name) + 1; - item = emalloc(sizeof(*item) + len); + item = ecalloc(1, sizeof(*item) + len); item->refcnt = 1; item->k.name = (char *) item + sizeof(*item); memcpy(item->k.name, name, len); - item->d.gr = NULL; + /* item->d.gr = NULL; */ if (rbinsert(grcache_byname, item) != NULL) errorx(1, "unable to cache group %s, already exists", name); } @@ -626,8 +625,7 @@ sudo_fakegrnam(group) len = sizeof(*item) + sizeof(*gr) + namelen + 1; for (i = 0; i < 2; i++) { - item = emalloc(len); - zero_bytes(item, sizeof(*item) + sizeof(*gr)); + item = ecalloc(1, len); gr = (struct group *) ((char *)item + sizeof(*item)); gr->gr_gid = (gid_t) atoi(group + 1); gr->gr_name = (char *)gr + sizeof(struct group); diff --git a/sudo.c b/sudo.c index 4aa774266..88a8d86ab 100644 --- a/sudo.c +++ b/sudo.c @@ -797,7 +797,7 @@ set_cmnd(sudo_mode) /* Resolve the path and return. */ rval = FOUND; - user_stat = emalloc(sizeof(struct stat)); + user_stat = ecalloc(1, sizeof(struct stat)); if (sudo_mode & (MODE_RUN | MODE_EDIT | MODE_CHECK)) { if (ISSET(sudo_mode, MODE_RUN | MODE_CHECK)) { if (def_secure_path && !user_is_exempt()) diff --git a/sudoreplay.c b/sudoreplay.c index 191a672b9..7d43faf90 100644 --- a/sudoreplay.c +++ b/sudoreplay.c @@ -192,7 +192,6 @@ extern time_t get_date __P((char *)); extern char *get_timestr __P((time_t, int)); extern int term_raw __P((int, int)); extern int term_restore __P((int, int)); -extern void zero_bytes __P((volatile void *, size_t)); RETSIGTYPE cleanup __P((int)); static int list_sessions __P((int, char **, const char *, const char *, const char *)); @@ -333,7 +332,7 @@ main(argc, 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; @@ -356,8 +355,7 @@ main(argc, argv) if (!term_raw(STDIN_FILENO, 1)) error(1, "cannot 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" @@ -554,11 +552,11 @@ parse_expr(headp, 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 { @@ -846,7 +844,7 @@ check_input(ttyfd, speed) char ch; ssize_t n; - 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); diff --git a/visudo.c b/visudo.c index 7755d9a35..f7c36523f 100644 --- a/visudo.c +++ b/visudo.c @@ -846,13 +846,13 @@ open_sudoers(path, doedit, 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);