From: Todd C. Miller Date: Mon, 29 Mar 1999 02:59:34 +0000 (+0000) Subject: Use emalloc/erealloc/estrdup X-Git-Tag: SUDO_1_5_9~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=382ef75493fb59674d3456237239934ddbcf1c22;p=sudo Use emalloc/erealloc/estrdup --- diff --git a/Makefile.in b/Makefile.in index 87fdf91b8..f17bb4183 100644 --- a/Makefile.in +++ b/Makefile.in @@ -96,9 +96,9 @@ PROGS = @PROGS@ SRCS = check.c getspwuid.c find_path.c logging.c parse.c sudo.c secureware.c \ goodpath.c sudo_setenv.c parse.yacc parse.lex visudo.c interfaces.c \ - check_sia.c + check_sia.c alloc.c -PARSEOBJS = sudo.tab.o lex.yy.o +PARSEOBJS = sudo.tab.o lex.yy.o alloc.o SUDOBJS = check.o getspwuid.o find_path.o logging.o parse.o sudo.o \ secureware.o goodpath.o sudo_setenv.o interfaces.o \ @@ -132,7 +132,7 @@ VERSIONFILES = emul/utime.h check.c compat.h config.h.in dce_pwent.c \ ins_classic.h ins_csops.h ins_goons.h insults.h interfaces.c \ logging.c parse.c parse.lex parse.yacc pathnames.h.in \ putenv.c strdup.c sudo.c sudo.h sudo_setenv.c testsudoers.c \ - tgetpass.c utime.c visudo.c secureware.c check_sia.c + tgetpass.c utime.c visudo.c secureware.c check_sia.c alloc.c all: $(PROGS) diff --git a/check.c b/check.c index 1a5373a98..1133261e6 100644 --- a/check.c +++ b/check.c @@ -818,11 +818,7 @@ static int sudo_krb5_validate_user(pw, pass) return -1; krb5_get_init_creds_opt_init(&opts); - princ_name = malloc(strlen(pw->pw_name) + strlen(realm) + 2); - if (!princ_name) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + princ_name = emalloc(strlen(pw->pw_name) + strlen(realm) + 2); sprintf(princ_name, "%s@%s", pw->pw_name, realm); if (retval = krb5_parse_name(sudo_context, princ_name, &princ)) @@ -955,23 +951,19 @@ static int PAM_conv(num_msg, msg, resp, appdata_ptr) int replies = 0; struct pam_response *reply = NULL; -/* XXX - replace with estrdup() */ -#define COPY_STRING(s) (s) ? strdup(s) : NULL - - reply = malloc(sizeof(struct pam_response) * num_msg); - if (reply == NULL) + if ((reply = malloc(sizeof(struct pam_response) * num_msg)) == NULL) return(PAM_CONV_ERR); for (replies = 0; replies < num_msg; replies++) { switch (msg[replies]->msg_style) { case PAM_PROMPT_ECHO_ON: reply[replies].resp_retcode = PAM_SUCCESS; - reply[replies].resp = COPY_STRING(PAM_username); + reply[replies].resp = estrdup(PAM_username); /* PAM frees resp */ break; case PAM_PROMPT_ECHO_OFF: reply[replies].resp_retcode = PAM_SUCCESS; - reply[replies].resp = COPY_STRING(PAM_password); + reply[replies].resp = estrdup(PAM_password); /* PAM frees resp */ break; case PAM_TEXT_INFO: @@ -1102,19 +1094,12 @@ static char *sudo_skeyprompt(user_skey, p) if (new_prompt == NULL) { /* allocate space for new prompt */ np_size = op_len + strlen(challenge) + 7; - if (!(new_prompt = (char *) malloc(np_size))) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + new_prompt = (char *) emalloc(np_size); } else { /* already have space allocated, is it enough? */ if (np_size < op_len + strlen(challenge) + 7) { np_size = op_len + strlen(challenge) + 7; - if (!(new_prompt = (char *) realloc(new_prompt, np_size))) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", - Argv[0]); - exit(1); - } + new_prompt = (char *) erealloc(new_prompt, np_size); } } @@ -1175,19 +1160,12 @@ static char *sudo_opieprompt(user_opie, p) if (new_prompt == NULL) { /* allocate space for new prompt */ np_size = op_len + strlen(challenge) + 7; - if (!(new_prompt = (char *) malloc(np_size))) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + new_prompt = (char *) emalloc(np_size); } else { /* already have space allocated, is it enough? */ if (np_size < op_len + strlen(challenge) + 7) { np_size = op_len + strlen(challenge) + 7; - if (!(new_prompt = (char *) realloc(new_prompt, np_size))) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", - Argv[0]); - exit(1); - } + new_prompt = (char *) erealloc(new_prompt, np_size); } } @@ -1288,10 +1266,7 @@ static char *expand_prompt(old_prompt, user, host) } if (subst) { - if ((new_prompt = (char *) malloc(len + 1)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + new_prompt = (char *) emalloc(len + 1); for (p = prompt, np = new_prompt; *p; p++) { if (lastchar == '%' && (*p == 'h' || *p == 'u' || *p == '%')) { /* substiture user/host name */ diff --git a/find_path.c b/find_path.c index 4fad5bfb0..84a8b38ac 100644 --- a/find_path.c +++ b/find_path.c @@ -26,7 +26,7 @@ * ocommand contain the resolved and unresolved pathnames respectively. * NOTE: if "." or "" exists in PATH it will be searched last. * - * Todd C. Miller (millert@colorado.edu) Sat Mar 25 21:50:36 MST 1995 + * Todd C. Miller Sat Mar 25 21:50:36 MST 1995 */ #include "config.h" @@ -44,9 +44,6 @@ #ifdef HAVE_STRINGS_H #include #endif /* HAVE_STRINGS_H */ -#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) -#include -#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #include #include #include @@ -55,18 +52,12 @@ #include "sudo.h" #ifndef STDC_HEADERS -#ifndef __GNUC__ /* gcc has its own malloc */ -extern char *malloc __P((size_t)); -#endif /* __GNUC__ */ extern char *getenv __P((const char *)); extern char *strcpy __P((char *, const char *)); extern int fprintf __P((FILE *, const char *, ...)); extern ssize_t readlink __P((const char *, VOID *, size_t)); extern int stat __P((const char *, struct stat *)); extern int lstat __P((const char *, struct stat *)); -#ifdef HAVE_STRDUP -extern char *strdup __P((const char *)); -#endif /* HAVE_STRDUP */ #endif /* !STDC_HEADERS */ #ifndef _S_IFMT @@ -129,10 +120,7 @@ int find_path(infile, outfile) if ((path = getenv("PATH")) == NULL) return(NOT_FOUND); - if ((path = (char *) strdup(path)) == NULL) { - (void) fprintf(stderr, "%s: out of memory!\n", Argv[0]); - exit(1); - } + path = estrdup(path); origpath = path; /* XXX use strtok() */ diff --git a/getspwuid.c b/getspwuid.c index 8df28411b..f81964523 100644 --- a/getspwuid.c +++ b/getspwuid.c @@ -34,9 +34,6 @@ #ifdef STDC_HEADERS #include #endif /* STDC_HEADERS */ -#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) -#include -#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #ifdef HAVE_STRING_H #include #endif /* HAVE_STRING_H */ @@ -75,13 +72,7 @@ static const char rcsid[] = "$Sudo$"; #endif /* lint */ #ifndef STDC_HEADERS -#ifndef __GNUC__ /* gcc has its own malloc */ -extern char *malloc __P((size_t)); -#endif /* __GNUC__ */ extern char *getenv __P((const char *)); -#ifdef HAVE_STRDUP -extern char *strdup __P((const char *)); -#endif /* HAVE_STRDUP */ #endif /* !STDC_HEADERS */ /* @@ -218,42 +209,20 @@ struct passwd *sudo_getpwuid(uid) return(NULL); /* allocate space for a local copy of pw */ - local_pw = (struct passwd *) malloc(sizeof(struct passwd)); - if (local_pw == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + local_pw = (struct passwd *) emalloc(sizeof(struct passwd)); /* * Copy the struct passwd and the interesting strings... */ (void) memcpy(local_pw, pw, sizeof(struct passwd)); - - local_pw->pw_name = (char *) strdup(pw->pw_name); - if (local_pw->pw_name == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - - local_pw->pw_dir = (char *) strdup(pw->pw_dir); - if (local_pw->pw_dir == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + local_pw->pw_name = estrdup(pw->pw_name); + local_pw->pw_dir = estrdup(pw->pw_dir); /* pw_shell is a special case since we overide with $SHELL */ - local_pw->pw_shell = (char *) strdup(sudo_getshell(pw)); - if (local_pw->pw_shell == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + local_pw->pw_shell = estrdup(sudo_getshell(pw)); /* pw_passwd gets a shadow password if applicable */ - local_pw->pw_passwd = (char *) strdup(sudo_getepw(pw)); - if (local_pw->pw_passwd == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + local_pw->pw_passwd = estrdup(sudo_getepw(pw)); return(local_pw); } diff --git a/goodpath.c b/goodpath.c index f4c165919..a9f3f0375 100644 --- a/goodpath.c +++ b/goodpath.c @@ -26,7 +26,7 @@ * if the path is stat(2)'able, a regular file, and executable by * root. The string's size should be <= MAXPATHLEN. * - * Todd C. Miller (millert@colorado.edu) Sat Mar 25 21:58:17 MST 1995 + * Todd C. Miller Sat Mar 25 21:58:17 MST 1995 */ #include "config.h" diff --git a/interfaces.c b/interfaces.c index ce2021dfc..d6b5dbc22 100644 --- a/interfaces.c +++ b/interfaces.c @@ -42,9 +42,6 @@ #ifdef HAVE_STRINGS_H #include #endif /* HAVE_STRINGS_H */ -#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) -#include -#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #include #include #include @@ -75,11 +72,6 @@ #include "sudo.h" #include "version.h" -#if !defined(STDC_HEADERS) && !defined(__GNUC__) -extern char *malloc __P((size_t)); -extern char *realloc __P((VOID *, size_t)); -#endif /* !STDC_HEADERS && !__GNUC__ */ - #ifndef lint static const char rcsid[] = "$Sudo$"; #endif /* lint */ @@ -124,11 +116,7 @@ void load_interfaces() * get interface configuration or return (leaving interfaces NULL) */ for (;;) { - ifconf_buf = ifconf_buf ? realloc(ifconf_buf, len) : malloc(len); - if (ifconf_buf == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + ifconf_buf = erealloc(ifconf_buf, len); ifconf = (struct ifconf *) ifconf_buf; ifconf->ifc_len = len - sizeof(struct ifconf); ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf)); @@ -157,13 +145,9 @@ void load_interfaces() n = ifconf->ifc_len / sizeof(struct ifreq); /* - * malloc() space for interfaces array + * allocate space for interfaces array */ - interfaces = (struct interface *) malloc(sizeof(struct interface) * n); - if (interfaces == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + interfaces = (struct interface *) emalloc(sizeof(struct interface) * n); /* * for each interface, store the ip address and netmask @@ -243,17 +227,11 @@ void load_interfaces() /* if there were bogus entries, realloc the array */ if (n != num_interfaces) { /* it is unlikely that num_interfaces will be 0 but who knows... */ - if (num_interfaces != 0) { - interfaces = (struct interface *) realloc(interfaces, + if (num_interfaces != 0) + interfaces = (struct interface *) erealloc(interfaces, sizeof(struct interface) * num_interfaces); - if (interfaces == NULL) { - perror("realloc"); - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - } else { + else (void) free(interfaces); - } } (void) free(ifconf_buf); (void) close(sock); diff --git a/lex.yy.c b/lex.yy.c index 4afc4cd26..23b9405c5 100644 --- a/lex.yy.c +++ b/lex.yy.c @@ -2029,9 +2029,11 @@ static void fill_args(s, len, addspace) while (new_len >= (arg_size += COMMANDARGINC)) ; - yylval.command.args = (char *) realloc(yylval.command.args, arg_size); - if (yylval.command.args == NULL) + if ((p = (char *) realloc(yylval.command.args, arg_size)) == NULL) { + (void) free(yylval.command.args); yyerror("unable to allocate memory"); + } else + yylval.command.args = p; } } diff --git a/logging.c b/logging.c index 3cfc85ab0..f22af1d2c 100644 --- a/logging.c +++ b/logging.c @@ -52,9 +52,6 @@ #ifdef HAVE_STRINGS_H #include #endif /* HAVE_STRINGS_H */ -#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) -#include -#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #include #include #include @@ -158,12 +155,7 @@ void log_error(code) strlen(runas_user); if (cmnd_args) count += strlen(cmnd_args); - - logline = (char *) malloc(count); - if (logline == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + logline = (char *) emalloc(count); /* * we will skip this stuff when using syslog(3) but it is diff --git a/parse.c b/parse.c index 0eb53c0f4..3662830f5 100644 --- a/parse.c +++ b/parse.c @@ -47,9 +47,6 @@ # include "emul/fnmatch.h" # endif /* HAVE_FNMATCH */ #endif /* HAVE_FNMATCH_H */ -#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) -# include -#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #ifdef HAVE_NETGROUP_H # include #endif /* HAVE_NETGROUP_H */ @@ -410,11 +407,7 @@ int netgr_matches(netgr, host, user) #ifdef HAVE_GETDOMAINNAME /* get the domain name (if any) */ if (domain == (char *) -1) { - if ((domain = (char *) malloc(MAXHOSTNAMELEN)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - + domain = (char *) emalloc(MAXHOSTNAMELEN); if (getdomainname(domain, MAXHOSTNAMELEN) != 0 || *domain == '\0') { (void) free(domain); domain = NULL; diff --git a/parse.lex b/parse.lex index 661958b38..53b2df553 100644 --- a/parse.lex +++ b/parse.lex @@ -323,9 +323,11 @@ static void fill_args(s, len, addspace) while (new_len >= (arg_size += COMMANDARGINC)) ; - yylval.command.args = (char *) realloc(yylval.command.args, arg_size); - if (yylval.command.args == NULL) + if ((p = (char *) realloc(yylval.command.args, arg_size)) == NULL) { + (void) free(yylval.command.args); yyerror("unable to allocate memory"); + } else + yylval.command.args = p; } } diff --git a/parse.yacc b/parse.yacc index 7835d1b6e..53343be38 100644 --- a/parse.yacc +++ b/parse.yacc @@ -92,11 +92,7 @@ int top = 0, stacksize = 0; { \ if (top >= stacksize) { \ while ((stacksize += STACKINCREMENT) < top); \ - match = (struct matchstack *) realloc(match, sizeof(struct matchstack) * stacksize); \ - if (match == NULL) { \ - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); \ - exit(1); \ - } \ + match = (struct matchstack *) erealloc(match, sizeof(struct matchstack) * stacksize); \ } \ match[top].user = -1; \ match[top].cmnd = -1; \ @@ -522,11 +518,7 @@ cmndalias : ALIAS { in_alias = TRUE; /* Allocate space for ga_list if necesary. */ expand_ga_list(); - if (!(ga_list[ga_list_len-1].alias = (char *) strdup($1))){ - (void) fprintf(stderr, - "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + ga_list[ga_list_len-1].alias = estrdup($1); } } '=' cmndlist { if (cmnd_matches == TRUE && @@ -555,11 +547,7 @@ runasalias : ALIAS { in_alias = TRUE; /* Allocate space for ga_list if necesary. */ expand_ga_list(); - if (!(ga_list[ga_list_len-1].alias = (char *) strdup($1))){ - (void) fprintf(stderr, - "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + ga_list[ga_list_len-1].alias = estrdup($1); } } '=' runaslist { if ($4 > 0 && add_alias($1, RUNAS_ALIAS) == FALSE) @@ -857,6 +845,8 @@ void list_matches() } (void) free(cm_list); cm_list = NULL; + cm_list_len = 0; + cm_list_size = 0; } @@ -879,11 +869,7 @@ static void append(src, dstp, dst_len, dst_size, separator) /* Assumes dst will be NULL if not set. */ if (dst == NULL) { - if ((dst = (char *) malloc(BUFSIZ)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - + dst = (char *) emalloc(BUFSIZ); *dst_size = BUFSIZ; *dst_len = 0; *dstp = dst; @@ -894,10 +880,7 @@ static void append(src, dstp, dst_len, dst_size, separator) while (*dst_size <= *dst_len + src_len) *dst_size += BUFSIZ; - if (!(dst = (char *) realloc(dst, *dst_size))) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + dst = (char *) erealloc(dst, *dst_size); *dstp = dst; } @@ -920,8 +903,10 @@ static void append(src, dstp, dst_len, dst_size, separator) void reset_aliases() { - if (aliases) + if (aliases) { (void) free(aliases); + aliases = NULL; + } naliases = nslots = 0; } @@ -936,20 +921,10 @@ void reset_aliases() static void expand_ga_list() { if (++ga_list_len >= ga_list_size) { - while ((ga_list_size += STACKINCREMENT) < ga_list_len); - if (ga_list == NULL) { - if ((ga_list = (struct generic_alias *) - malloc(sizeof(struct generic_alias) * ga_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - } else { - if ((ga_list = (struct generic_alias *) realloc(ga_list, - sizeof(struct generic_alias) * ga_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - } + while ((ga_list_size += STACKINCREMENT) < ga_list_len) + ; + ga_list = (struct generic_alias *) + erealloc(ga_list, sizeof(struct generic_alias) * ga_list_size); } ga_list[ga_list_len - 1].entries = NULL; @@ -966,21 +941,10 @@ static void expand_ga_list() static void expand_match_list() { if (++cm_list_len >= cm_list_size) { - while ((cm_list_size += STACKINCREMENT) < cm_list_len); - if (cm_list == NULL) { - if ((cm_list = (struct command_match *) - malloc(sizeof(struct command_match) * cm_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - cm_list_len = 0; - } else { - if ((cm_list = (struct command_match *) realloc(cm_list, - sizeof(struct command_match) * cm_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - } + while ((cm_list_size += STACKINCREMENT) < cm_list_len) + ; + cm_list = (struct command_match *) + erealloc(cm_list, sizeof(struct command_match) * cm_list_size); } cm_list[cm_list_len].runas = cm_list[cm_list_len].cmnd = NULL; @@ -1010,11 +974,7 @@ void init_parser() /* Allocate space for the matching stack. */ stacksize = STACKINCREMENT; - match = (struct matchstack *) malloc(sizeof(struct matchstack) * stacksize); - if (match == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + match = (struct matchstack *) emalloc(sizeof(struct matchstack) * stacksize); /* Allocate space for the match list (for `sudo -l'). */ if (printmatches == TRUE) diff --git a/putenv.c b/putenv.c index b9adbeef7..3228133d4 100644 --- a/putenv.c +++ b/putenv.c @@ -22,7 +22,7 @@ * * This module contains putenv(3) for those systems that lack it. * - * Todd C. Miller (millert@colorado.edu) Sun Aug 7 20:30:17 MDT 1994 + * Todd C. Miller Sun Aug 7 20:30:17 MDT 1994 */ #include "config.h" @@ -44,7 +44,7 @@ #include "compat.h" #if !defined(STDC_HEADERS) && !defined(__GNUC__) -extern char *malloc __P((size_t)); +extern VOID *malloc __P((size_t)); #endif /* !STDC_HEADERS && !gcc */ #ifndef lint diff --git a/secureware.c b/secureware.c index a2372c453..b3b527613 100644 --- a/secureware.c +++ b/secureware.c @@ -22,7 +22,7 @@ * * secureware.c -- check a user's password when using SecureWare C2 * - * Todd C. Miller (millert@colorado.edu) Sat Oct 17 14:42:44 MDT 1998 + * Todd C. Miller Sat Oct 17 14:42:44 MDT 1998 */ #include "config.h" diff --git a/strdup.c b/strdup.c index 4df39aa43..1c59e8885 100644 --- a/strdup.c +++ b/strdup.c @@ -53,7 +53,7 @@ #ifndef STDC_HEADERS #ifndef __GNUC__ /* gcc has its own malloc */ -extern char *malloc __P((size_t)); +extern VOID *malloc __P((size_t)); #endif /* __GNUC__ */ extern char *strcpy __P((char *, const char *)); #endif /* !STDC_HEADERS */ diff --git a/sudo.c b/sudo.c index 718ef6156..3d6cc807b 100644 --- a/sudo.c +++ b/sudo.c @@ -68,9 +68,6 @@ #ifdef HAVE_STRINGS_H #include #endif /* HAVE_STRINGS_H */ -#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) -#include -#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #include #include #include @@ -90,12 +87,6 @@ #include "version.h" #ifndef STDC_HEADERS -#ifndef __GNUC__ /* gcc has its own malloc */ -extern char *malloc __P((size_t)); -#endif /* __GNUC__ */ -#ifdef HAVE_STRDUP -extern char *strdup __P((const char *)); -#endif /* HAVE_STRDUP */ extern char *getenv __P((char *)); #endif /* STDC_HEADERS */ @@ -271,11 +262,7 @@ int main(argc, argv) if ((sudo_mode & MODE_SHELL)) { char **dst, **src = NewArgv; - NewArgv = (char **) malloc (sizeof(char *) * (++NewArgc + 1)); - if (NewArgv == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + NewArgv = (char **) emalloc (sizeof(char *) * (++NewArgc + 1)); /* add the shell as argv[0] */ if (user_shell && *user_shell) { @@ -488,11 +475,7 @@ static void load_globals(sudo_mode) realm = lrealm; if (!arg_prompt) { - p = malloc(strlen(user_name) + strlen(realm) + 17); - if (p == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + p = emalloc(strlen(user_name) + strlen(realm) + 17); sprintf(p, "Password for %s@%s: ", user_name, realm); prompt = p; } @@ -512,10 +495,7 @@ static void load_globals(sudo_mode) if ((p = (char *) ttyname(0)) || (p = (char *) ttyname(1))) { if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) p += sizeof(_PATH_DEV) - 1; - if ((tty = (char *) strdup(p)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + tty = estrdup(p); } #ifdef SUDO_UMASK @@ -567,10 +547,7 @@ static void load_globals(sudo_mode) */ if ((p = strchr(host, '.'))) { *p = '\0'; - if ((shost = (char *) strdup(host)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + shost = estrdup(host); *p = '.'; } else { shost = &host[0]; @@ -768,10 +745,7 @@ static void add_env(contiguous) size += strlen(*from) + 1; } - if ((buf = (char *) malloc(size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + buf = (char *) emalloc(size); /* * Copy the command and it's arguments info buf diff --git a/sudo.h b/sudo.h index 231df000e..32ddd8ac6 100644 --- a/sudo.h +++ b/sudo.h @@ -229,9 +229,6 @@ struct generic_alias { */ #define YY_DECL int yylex __P((void)) -#ifndef HAVE_STRDUP -char *strdup __P((const char *)); -#endif #ifndef HAVE_GETCWD char *getcwd __P((char *, size_t size)); #endif @@ -253,6 +250,9 @@ int check_secureware __P((char *)); void sia_attempt_auth __P((void)); int yyparse __P((void)); void pass_warn __P((FILE *)); +VOID *emalloc __P((size_t)); +VOID *erealloc __P((VOID *, size_t)); +VOID *estrdup __P((char *)); YY_DECL; diff --git a/sudo.tab.c b/sudo.tab.c index 4957debdb..fe8210244 100644 --- a/sudo.tab.c +++ b/sudo.tab.c @@ -110,11 +110,7 @@ int top = 0, stacksize = 0; { \ if (top >= stacksize) { \ while ((stacksize += STACKINCREMENT) < top); \ - match = (struct matchstack *) realloc(match, sizeof(struct matchstack) * stacksize); \ - if (match == NULL) { \ - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); \ - exit(1); \ - } \ + match = (struct matchstack *) erealloc(match, sizeof(struct matchstack) * stacksize); \ } \ match[top].user = -1; \ match[top].cmnd = -1; \ @@ -175,14 +171,14 @@ void yyerror(s) #endif parse_error = TRUE; } -#line 162 "parse.yacc" +#line 158 "parse.yacc" typedef union { char *string; int BOOLEAN; struct sudo_command command; int tok; } YYSTYPE; -#line 186 "sudo.tab.c" +#line 182 "sudo.tab.c" #define ALIAS 257 #define NTWKADDR 258 #define FQHOST 259 @@ -467,7 +463,7 @@ short *yyss; short *yysslim; YYSTYPE *yyvs; int yystacksize; -#line 621 "parse.yacc" +#line 609 "parse.yacc" typedef struct { @@ -707,6 +703,8 @@ void list_matches() } (void) free(cm_list); cm_list = NULL; + cm_list_len = 0; + cm_list_size = 0; } @@ -729,11 +727,7 @@ static void append(src, dstp, dst_len, dst_size, separator) /* Assumes dst will be NULL if not set. */ if (dst == NULL) { - if ((dst = (char *) malloc(BUFSIZ)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - + dst = (char *) emalloc(BUFSIZ); *dst_size = BUFSIZ; *dst_len = 0; *dstp = dst; @@ -744,10 +738,7 @@ static void append(src, dstp, dst_len, dst_size, separator) while (*dst_size <= *dst_len + src_len) *dst_size += BUFSIZ; - if (!(dst = (char *) realloc(dst, *dst_size))) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + dst = (char *) erealloc(dst, *dst_size); *dstp = dst; } @@ -770,8 +761,10 @@ static void append(src, dstp, dst_len, dst_size, separator) void reset_aliases() { - if (aliases) + if (aliases) { (void) free(aliases); + aliases = NULL; + } naliases = nslots = 0; } @@ -786,20 +779,10 @@ void reset_aliases() static void expand_ga_list() { if (++ga_list_len >= ga_list_size) { - while ((ga_list_size += STACKINCREMENT) < ga_list_len); - if (ga_list == NULL) { - if ((ga_list = (struct generic_alias *) - malloc(sizeof(struct generic_alias) * ga_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - } else { - if ((ga_list = (struct generic_alias *) realloc(ga_list, - sizeof(struct generic_alias) * ga_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - } + while ((ga_list_size += STACKINCREMENT) < ga_list_len) + ; + ga_list = (struct generic_alias *) + erealloc(ga_list, sizeof(struct generic_alias) * ga_list_size); } ga_list[ga_list_len - 1].entries = NULL; @@ -816,21 +799,10 @@ static void expand_ga_list() static void expand_match_list() { if (++cm_list_len >= cm_list_size) { - while ((cm_list_size += STACKINCREMENT) < cm_list_len); - if (cm_list == NULL) { - if ((cm_list = (struct command_match *) - malloc(sizeof(struct command_match) * cm_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - cm_list_len = 0; - } else { - if ((cm_list = (struct command_match *) realloc(cm_list, - sizeof(struct command_match) * cm_list_size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - } + while ((cm_list_size += STACKINCREMENT) < cm_list_len) + ; + cm_list = (struct command_match *) + erealloc(cm_list, sizeof(struct command_match) * cm_list_size); } cm_list[cm_list_len].runas = cm_list[cm_list_len].cmnd = NULL; @@ -860,17 +832,13 @@ void init_parser() /* Allocate space for the matching stack. */ stacksize = STACKINCREMENT; - match = (struct matchstack *) malloc(sizeof(struct matchstack) * stacksize); - if (match == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + match = (struct matchstack *) emalloc(sizeof(struct matchstack) * stacksize); /* Allocate space for the match list (for `sudo -l'). */ if (printmatches == TRUE) expand_match_list(); } -#line 874 "sudo.tab.c" +#line 842 "sudo.tab.c" /* allocate initial stack or double stack size, up to YYMAXDEPTH */ #if defined(__cplusplus) || __STDC__ static int yygrowstack(void) @@ -1051,19 +1019,19 @@ yyreduce: switch (yyn) { case 3: -#line 203 "parse.yacc" +#line 199 "parse.yacc" { ; } break; case 4: -#line 205 "parse.yacc" +#line 201 "parse.yacc" { yyerrok; } break; case 5: -#line 206 "parse.yacc" +#line 202 "parse.yacc" { push; } break; case 6: -#line 206 "parse.yacc" +#line 202 "parse.yacc" { while (top && user_matches != TRUE) { pop; @@ -1071,23 +1039,23 @@ case 6: } break; case 7: -#line 212 "parse.yacc" +#line 208 "parse.yacc" { ; } break; case 8: -#line 214 "parse.yacc" +#line 210 "parse.yacc" { ; } break; case 9: -#line 216 "parse.yacc" +#line 212 "parse.yacc" { ; } break; case 10: -#line 218 "parse.yacc" +#line 214 "parse.yacc" { ; } break; case 13: -#line 226 "parse.yacc" +#line 222 "parse.yacc" { if (user_matches == TRUE) { push; @@ -1099,13 +1067,13 @@ case 13: } break; case 14: -#line 237 "parse.yacc" +#line 233 "parse.yacc" { host_matches = TRUE; } break; case 15: -#line 240 "parse.yacc" +#line 236 "parse.yacc" { if (addr_matches(yyvsp[0].string)) host_matches = TRUE; @@ -1113,7 +1081,7 @@ case 15: } break; case 16: -#line 245 "parse.yacc" +#line 241 "parse.yacc" { if (netgr_matches(yyvsp[0].string, host, NULL)) host_matches = TRUE; @@ -1121,7 +1089,7 @@ case 16: } break; case 17: -#line 250 "parse.yacc" +#line 246 "parse.yacc" { if (strcasecmp(shost, yyvsp[0].string) == 0) host_matches = TRUE; @@ -1129,7 +1097,7 @@ case 17: } break; case 18: -#line 255 "parse.yacc" +#line 251 "parse.yacc" { if (strcasecmp(host, yyvsp[0].string) == 0) host_matches = TRUE; @@ -1137,7 +1105,7 @@ case 18: } break; case 19: -#line 260 "parse.yacc" +#line 256 "parse.yacc" { /* could be an all-caps hostname */ if (find_alias(yyvsp[0].string, HOST_ALIAS) == TRUE || @@ -1147,7 +1115,7 @@ case 19: } break; case 22: -#line 273 "parse.yacc" +#line 269 "parse.yacc" { /* Push a new entry onto the stack if needed */ if (user_matches == TRUE && host_matches == TRUE && cmnd_matches == TRUE && runas_matches == TRUE) { @@ -1162,7 +1130,7 @@ case 22: } break; case 23: -#line 284 "parse.yacc" +#line 280 "parse.yacc" { if (yyvsp[-2].BOOLEAN > 0 && yyvsp[0].BOOLEAN == TRUE) { runas_matches = TRUE; @@ -1176,11 +1144,11 @@ case 23: } break; case 24: -#line 297 "parse.yacc" +#line 293 "parse.yacc" { ; } break; case 25: -#line 298 "parse.yacc" +#line 294 "parse.yacc" { if (printmatches == TRUE && host_matches == TRUE && user_matches == TRUE) { @@ -1196,7 +1164,7 @@ case 25: } break; case 26: -#line 310 "parse.yacc" +#line 306 "parse.yacc" { int cmnd_matched = cmnd_matches; pop; @@ -1208,31 +1176,31 @@ case 26: } break; case 27: -#line 321 "parse.yacc" +#line 317 "parse.yacc" { yyval.BOOLEAN = (strcmp(RUNAS_DEFAULT, runas_user) == 0); } break; case 28: -#line 324 "parse.yacc" +#line 320 "parse.yacc" { yyval.BOOLEAN = yyvsp[0].BOOLEAN; } break; case 29: -#line 329 "parse.yacc" +#line 325 "parse.yacc" { yyval.BOOLEAN = yyvsp[0].BOOLEAN; } break; case 30: -#line 332 "parse.yacc" +#line 328 "parse.yacc" { yyval.BOOLEAN = yyvsp[-2].BOOLEAN + yyvsp[0].BOOLEAN; } break; case 31: -#line 338 "parse.yacc" +#line 334 "parse.yacc" { yyval.BOOLEAN = (strcmp(yyvsp[0].string, runas_user) == 0); if (printmatches == TRUE && in_alias == TRUE) @@ -1248,7 +1216,7 @@ case 31: } break; case 32: -#line 351 "parse.yacc" +#line 347 "parse.yacc" { yyval.BOOLEAN = usergr_matches(yyvsp[0].string, runas_user); if (printmatches == TRUE && in_alias == TRUE) @@ -1268,7 +1236,7 @@ case 32: } break; case 33: -#line 368 "parse.yacc" +#line 364 "parse.yacc" { yyval.BOOLEAN = netgr_matches(yyvsp[0].string, NULL, runas_user); if (printmatches == TRUE && in_alias == TRUE) @@ -1288,7 +1256,7 @@ case 33: } break; case 34: -#line 385 "parse.yacc" +#line 381 "parse.yacc" { /* could be an all-caps username */ if (find_alias(yyvsp[0].string, RUNAS_ALIAS) == TRUE || @@ -1309,7 +1277,7 @@ case 34: } break; case 35: -#line 403 "parse.yacc" +#line 399 "parse.yacc" { yyval.BOOLEAN = TRUE; if (printmatches == TRUE && in_alias == TRUE) @@ -1324,13 +1292,13 @@ case 35: } break; case 36: -#line 417 "parse.yacc" +#line 413 "parse.yacc" { yyval.BOOLEAN = FALSE; } break; case 37: -#line 420 "parse.yacc" +#line 416 "parse.yacc" { yyval.BOOLEAN = TRUE; if (printmatches == TRUE && host_matches == TRUE && @@ -1339,7 +1307,7 @@ case 37: } break; case 38: -#line 428 "parse.yacc" +#line 424 "parse.yacc" { if (printmatches == TRUE && in_alias == TRUE) { append("ALL", &ga_list[ga_list_len-1].entries, @@ -1359,7 +1327,7 @@ case 38: } break; case 39: -#line 445 "parse.yacc" +#line 441 "parse.yacc" { if (printmatches == TRUE && in_alias == TRUE) { append(yyvsp[0].string, &ga_list[ga_list_len-1].entries, @@ -1381,7 +1349,7 @@ case 39: } break; case 40: -#line 464 "parse.yacc" +#line 460 "parse.yacc" { if (printmatches == TRUE && in_alias == TRUE) { append(yyvsp[0].command.cmnd, &ga_list[ga_list_len-1].entries, @@ -1417,11 +1385,11 @@ case 40: } break; case 43: -#line 503 "parse.yacc" +#line 499 "parse.yacc" { push; } break; case 44: -#line 503 "parse.yacc" +#line 499 "parse.yacc" { if (host_matches == TRUE && add_alias(yyvsp[-3].string, HOST_ALIAS) == FALSE) @@ -1430,23 +1398,19 @@ case 44: } break; case 49: -#line 519 "parse.yacc" +#line 515 "parse.yacc" { push; if (printmatches == TRUE) { in_alias = TRUE; /* Allocate space for ga_list if necesary. */ expand_ga_list(); - if (!(ga_list[ga_list_len-1].alias = (char *) strdup(yyvsp[0].string))){ - (void) fprintf(stderr, - "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + ga_list[ga_list_len-1].alias = estrdup(yyvsp[0].string); } } break; case 50: -#line 531 "parse.yacc" +#line 523 "parse.yacc" { if (cmnd_matches == TRUE && add_alias(yyvsp[-3].string, CMND_ALIAS) == FALSE) @@ -1459,27 +1423,23 @@ case 50: } break; case 51: -#line 544 "parse.yacc" +#line 536 "parse.yacc" { ; } break; case 55: -#line 552 "parse.yacc" +#line 544 "parse.yacc" { push; if (printmatches == TRUE) { in_alias = TRUE; /* Allocate space for ga_list if necesary. */ expand_ga_list(); - if (!(ga_list[ga_list_len-1].alias = (char *) strdup(yyvsp[0].string))){ - (void) fprintf(stderr, - "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + ga_list[ga_list_len-1].alias = estrdup(yyvsp[0].string); } } break; case 56: -#line 564 "parse.yacc" +#line 552 "parse.yacc" { if (yyvsp[0].BOOLEAN > 0 && add_alias(yyvsp[-3].string, RUNAS_ALIAS) == FALSE) YYERROR; @@ -1491,11 +1451,11 @@ case 56: } break; case 59: -#line 579 "parse.yacc" +#line 567 "parse.yacc" { push; } break; case 60: -#line 579 "parse.yacc" +#line 567 "parse.yacc" { if (user_matches == TRUE && add_alias(yyvsp[-3].string, USER_ALIAS) == FALSE) @@ -1505,11 +1465,11 @@ case 60: } break; case 61: -#line 589 "parse.yacc" +#line 577 "parse.yacc" { ; } break; case 63: -#line 593 "parse.yacc" +#line 581 "parse.yacc" { if (strcmp(yyvsp[0].string, user_name) == 0) user_matches = TRUE; @@ -1517,7 +1477,7 @@ case 63: } break; case 64: -#line 598 "parse.yacc" +#line 586 "parse.yacc" { if (usergr_matches(yyvsp[0].string, user_name)) user_matches = TRUE; @@ -1525,7 +1485,7 @@ case 64: } break; case 65: -#line 603 "parse.yacc" +#line 591 "parse.yacc" { if (netgr_matches(yyvsp[0].string, NULL, user_name)) user_matches = TRUE; @@ -1533,7 +1493,7 @@ case 65: } break; case 66: -#line 608 "parse.yacc" +#line 596 "parse.yacc" { /* could be an all-caps username */ if (find_alias(yyvsp[0].string, USER_ALIAS) == TRUE || @@ -1543,12 +1503,12 @@ case 66: } break; case 67: -#line 615 "parse.yacc" +#line 603 "parse.yacc" { user_matches = TRUE; } break; -#line 1552 "sudo.tab.c" +#line 1512 "sudo.tab.c" } yyssp -= yym; yystate = *yyssp; diff --git a/sudo_setenv.c b/sudo_setenv.c index 2da0bc181..d79f40960 100644 --- a/sudo_setenv.c +++ b/sudo_setenv.c @@ -23,7 +23,7 @@ * This module contains sudo_setenv(). * sudo_setenv(3) adds a string of the form "var=val" to the environment. * - * Todd C. Miller (millert@colorado.edu) Fri Jun 3 18:32:19 MDT 1994 + * Todd C. Miller Fri Jun 3 18:32:19 MDT 1994 */ #include "config.h" diff --git a/testsudoers.c b/testsudoers.c index d1b4c2ea3..970229d28 100644 --- a/testsudoers.c +++ b/testsudoers.c @@ -47,9 +47,6 @@ # include "emul/fnmatch.h" # endif /* HAVE_FNMATCH */ #endif /* HAVE_FNMATCH_H */ -#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) -# include -#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #ifdef HAVE_NETGROUP_H # include #endif /* HAVE_NETGROUP_H */ @@ -228,10 +225,7 @@ int netgr_matches(netgr, host, user) #ifdef HAVE_GETDOMAINNAME /* get the domain name (if any) */ if (domain == (char *) -1) { - if ((domain = (char *) malloc(MAXHOSTNAMELEN)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + domain = (char *) emalloc(MAXHOSTNAMELEN); if (getdomainname(domain, MAXHOSTNAMELEN) != 0 || *domain == '\0') { (void) free(domain); @@ -293,10 +287,7 @@ int main(argc, argv) if ((p = strchr(host, '.'))) { *p = '\0'; - if ((shost = (char *) strdup(host)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + shost = estrdup(host); *p = '.'; } else { shost = &host[0]; @@ -309,10 +300,7 @@ int main(argc, argv) size = (size_t) NewArgv[NewArgc-1] + strlen(NewArgv[NewArgc-1]) - (size_t) NewArgv[1] + 1; - if ((cmnd_args = (char *) malloc(size)) == NULL) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + cmnd_args = (char *) emalloc(size); for (to = cmnd_args, from = &NewArgv[1]; *from; from++) { *to++ = ' '; (void) strcpy(to, *from); diff --git a/utime.c b/utime.c index 786e76bb1..597665b93 100644 --- a/utime.c +++ b/utime.c @@ -24,7 +24,7 @@ * lack utime(2). * utime(3) sets the access and mod times of the named file. * - * Todd C. Miller (millert@colorado.edu) Sat Jun 17 16:42:41 MDT 1995 + * Todd C. Miller Sat Jun 17 16:42:41 MDT 1995 */ #include "config.h" diff --git a/visudo.c b/visudo.c index d0a4e440e..e2a515fd8 100644 --- a/visudo.c +++ b/visudo.c @@ -23,7 +23,7 @@ * visudo.c -- locks the sudoers file for safe editing and check * for parse errors. * - * Todd C. Miller (millert@colorado.edu) Sat Mar 25 21:50:36 MST 1995 + * Todd C. Miller Sat Mar 25 21:50:36 MST 1995 */ #include "config.h"