From: Todd C. Miller Date: Tue, 29 Mar 2005 14:29:47 +0000 (+0000) Subject: Add efree() for consistency with emalloc() et al. Allows us to rely X-Git-Tag: SUDO_1_7_0~663 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=304dc46d7f807c0d3d571026f0ef2fa084e56fc8;p=sudo Add efree() for consistency with emalloc() et al. Allows us to rely on C89 behavior (free(NULL) is valid) even on K&R. --- diff --git a/alias.c b/alias.c index 00d327bd0..e0a974cf4 100644 --- a/alias.c +++ b/alias.c @@ -116,7 +116,7 @@ alias_add(name, type, members) a->type = type; a->first_member = members; if (rbinsert(aliases, a)) { - free(a); + efree(a); snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name); return(errbuf); } @@ -156,11 +156,10 @@ alias_free(v) for (m = a->first_member; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } - free(a); + efree(a); } /* diff --git a/alloc.c b/alloc.c index 4db6f2f24..58123fb24 100644 --- a/alloc.c +++ b/alloc.c @@ -211,3 +211,14 @@ evasprintf(ret, format, args) errorx(1, "unable to allocate memory"); return(len); } + +/* + * Wrapper for free(3) so we can depend on C89 semantics. + */ +void +efree(ptr) + VOID *ptr; +{ + if (ptr != NULL) + free(ptr); +} diff --git a/check.c b/check.c index 6cff08d7a..a117d74d2 100644 --- a/check.c +++ b/check.c @@ -100,9 +100,8 @@ check_user(override) } if (status != TS_ERROR) update_timestamp(timestampdir, timestampfile); - free(timestampdir); - if (timestampfile) - free(timestampfile); + efree(timestampdir); + efree(timestampfile); } /* @@ -551,7 +550,6 @@ remove_timestamp(remove) } } - free(timestampdir); - if (timestampfile) - free(timestampfile); + efree(timestampdir); + efree(timestampfile); } diff --git a/defaults.c b/defaults.c index 0279a42c6..ea533089c 100644 --- a/defaults.c +++ b/defaults.c @@ -362,10 +362,8 @@ init_defaults() for (def = sudo_defs_table; def->name; def++) { switch (def->type & T_MASK) { case T_STR: - if (def->sd_un.str) { - free(def->sd_un.str); - def->sd_un.str = NULL; - } + efree(def->sd_un.str); + def->sd_un.str = NULL; break; case T_LIST: list_op(NULL, 0, def, freeall); @@ -621,8 +619,7 @@ store_str(val, def, op) int op; { - if (def->sd_un.str) - free(def->sd_un.str); + efree(def->sd_un.str); if (op == FALSE) def->sd_un.str = NULL; else @@ -771,8 +768,8 @@ list_op(val, len, def, op) for (cur = def->sd_un.list; cur; ) { tmp = cur; cur = tmp->next; - free(tmp->value); - free(tmp); + efree(tmp->value); + efree(tmp); } def->sd_un.list = NULL; return; @@ -789,8 +786,8 @@ list_op(val, len, def, op) prev->next = cur->next; else def->sd_un.list = cur->next; - free(cur->value); - free(cur); + efree(cur->value); + efree(cur); break; } } diff --git a/find_path.c b/find_path.c index a40b594d4..b6f5f0aa4 100644 --- a/find_path.c +++ b/find_path.c @@ -120,7 +120,7 @@ find_path(infile, outfile, sbp, path) path = n + 1; } while (n); - free(origpath); + efree(origpath); /* * Check current dir if dot was in the PATH diff --git a/gram.c b/gram.c index 528b1d09e..5c32568b8 100644 --- a/gram.c +++ b/gram.c @@ -664,35 +664,31 @@ init_parser(path, quiet) for (us = userspecs; us != NULL; us = next) { for (m = us->user; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } for (priv = us->privileges; priv != NULL; priv = next) { for (m = priv->hostlist; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } for (cs = priv->cmndlist; cs != NULL; cs = next) { for (m = cs->runaslist; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } - if (cs->cmnd->name != NULL) - free(cs->cmnd->name); - free(cs->cmnd); + efree(cs->cmnd->name); + efree(cs->cmnd); next = cs->next; - free(cs); + efree(cs); } next = priv->next; - free(priv); + efree(priv); } next = us->next; - free(us); + efree(us); } userspecs = NULL; @@ -701,24 +697,21 @@ init_parser(path, quiet) if (d->binding != lastbinding) { for (m = d->binding; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } lastbinding = d->binding; } next = d->next; - free(d->var); - if (d->val != NULL) - free(d->val); - free(d); + efree(d->var); + efree(d->val); + efree(d); } defaults = NULL; init_aliases(); - if (sudoers != NULL) - free(sudoers); + efree(sudoers); sudoers = estrdup(path); parse_error = FALSE; @@ -726,7 +719,7 @@ init_parser(path, quiet) sudolineno = 1; verbose = !quiet; } -#line 678 "gram.c" +#line 671 "gram.c" /* allocate initial stack or double stack size, up to YYMAXDEPTH */ #if defined(__cplusplus) || defined(__STDC__) static int yygrowstack(void) @@ -1371,7 +1364,7 @@ case 84: NEW_MEMBER(yyval.member, yyvsp[0].string, WORD); } break; -#line 1323 "gram.c" +#line 1316 "gram.c" } yyssp -= yym; yystate = *yyssp; diff --git a/gram.y b/gram.y index f2d3a1e42..c2e3267b3 100644 --- a/gram.y +++ b/gram.y @@ -562,35 +562,31 @@ init_parser(path, quiet) for (us = userspecs; us != NULL; us = next) { for (m = us->user; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } for (priv = us->privileges; priv != NULL; priv = next) { for (m = priv->hostlist; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } for (cs = priv->cmndlist; cs != NULL; cs = next) { for (m = cs->runaslist; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } - if (cs->cmnd->name != NULL) - free(cs->cmnd->name); - free(cs->cmnd); + efree(cs->cmnd->name); + efree(cs->cmnd); next = cs->next; - free(cs); + efree(cs); } next = priv->next; - free(priv); + efree(priv); } next = us->next; - free(us); + efree(us); } userspecs = NULL; @@ -599,24 +595,21 @@ init_parser(path, quiet) if (d->binding != lastbinding) { for (m = d->binding; m != NULL; m = next) { next = m->next; - if (m->name != NULL) - free(m->name); - free(m); + efree(m->name); + efree(m); } lastbinding = d->binding; } next = d->next; - free(d->var); - if (d->val != NULL) - free(d->val); - free(d); + efree(d->var); + efree(d->val); + efree(d); } defaults = NULL; init_aliases(); - if (sudoers != NULL) - free(sudoers); + efree(sudoers); sudoers = estrdup(path); parse_error = FALSE; diff --git a/interfaces.c b/interfaces.c index 008fdb6bf..416020700 100644 --- a/interfaces.c +++ b/interfaces.c @@ -146,7 +146,7 @@ load_interfaces() #ifdef HAVE_FREEIFADDRS freeifaddrs(ifaddrs); #else - free(ifaddrs); + efree(ifaddrs); #endif } @@ -189,7 +189,7 @@ load_interfaces() #else if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0) { #endif /* _ISC */ - free(ifconf_buf); + efree(ifconf_buf); (void) close(sock); return; } @@ -279,9 +279,9 @@ load_interfaces() interfaces = (struct interface *) erealloc3(interfaces, num_interfaces, sizeof(struct interface)); else - free(interfaces); + efree(interfaces); } - free(ifconf_buf); + efree(ifconf_buf); (void) close(sock); } diff --git a/ldap.c b/ldap.c index 0a1c9114b..e4402411b 100644 --- a/ldap.c +++ b/ldap.c @@ -260,8 +260,7 @@ sudo_ldap_check_command(ld, entry) /* Match against ALL ? */ if (!strcasecmp(*p, "ALL")) { ret = TRUE; - if (safe_cmnd) - free(safe_cmnd); + efree(safe_cmnd); safe_cmnd = estrdup(user_cmnd); if (ldap_conf.debug > 1) printf(" MATCH!\n"); @@ -295,7 +294,7 @@ sudo_ldap_check_command(ld, entry) printf(" not\n"); } - free(allowed_cmnd); /* cleanup */ + efree(allowed_cmnd); /* cleanup */ } if (v) @@ -348,7 +347,7 @@ sudo_ldap_parse_options(ld, entry) /* case var Boolean True */ set_default(var, NULL, TRUE); } - free(var); + efree(var); } if (v) @@ -524,7 +523,7 @@ sudo_ldap_read_config() /* The following macros make the code much more readable */ #define MATCH_S(x,y) if (!strcasecmp(keyword,x)) \ - { if (y) free(y); y=estrdup(value); } + { efree(y); y=estrdup(value); } #define MATCH_I(x,y) if (!strcasecmp(keyword,x)) { y=atoi(value); } #define MATCH_B(x,y) if (!strcasecmp(keyword,x)) { y=_atobool(value); } @@ -905,8 +904,7 @@ sudo_ldap_check(v, pwflag) if (ldap_conf.debug) printf("nothing found for '%s'\n", filt); } - if (filt) - free(filt); + efree(filt); /* parse each entry returned from this most recent search */ entry = rc ? NULL : ldap_first_entry(ld, result); diff --git a/logging.c b/logging.c index ea08887a8..2ec1390ba 100644 --- a/logging.c +++ b/logging.c @@ -189,12 +189,12 @@ do_logfile(msg) easprintf(&full_line, "Can't open log file: %s: %s", def_logfile, strerror(errno)); send_mail(full_line); - free(full_line); + efree(full_line); } else if (!lock_file(fileno(fp), SUDO_LOCK)) { easprintf(&full_line, "Can't lock log file: %s: %s", def_logfile, strerror(errno)); send_mail(full_line); - free(full_line); + efree(full_line); } else { if (def_loglinelen == 0) { /* Don't pretty-print long log file lines (hard to grep) */ @@ -259,7 +259,7 @@ do_logfile(msg) beg = NULL; /* exit condition */ } } - free(full_line); + efree(full_line); } (void) fflush(fp); (void) lock_file(fileno(fp), SUDO_UNLOCK); @@ -330,7 +330,7 @@ log_auth(status, inform_user) if (def_logfile) do_logfile(logline); - free(logline); + efree(logline); } void @@ -410,9 +410,9 @@ log_error(flags, fmt, va_alist) if (def_logfile) do_logfile(logline); - free(message); + efree(message); if (logline != message) - free(logline); + efree(logline); if (!ISSET(flags, NO_EXIT)) exit(1); diff --git a/match.c b/match.c index b65ce1e20..22428e300 100644 --- a/match.c +++ b/match.c @@ -293,8 +293,7 @@ command_matches(sudoers_cmnd, sudoers_args) (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) || (sudoers_args && fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) { - if (safe_cmnd) - free(safe_cmnd); + efree(safe_cmnd); safe_cmnd = estrdup(sudoers_cmnd); return(TRUE); } else @@ -331,8 +330,7 @@ command_matches(sudoers_cmnd, sudoers_args) if (user_stat == NULL || (user_stat->st_dev == sudoers_stat.st_dev && user_stat->st_ino == sudoers_stat.st_ino)) { - if (safe_cmnd) - free(safe_cmnd); + efree(safe_cmnd); safe_cmnd = estrdup(*ap); break; } @@ -345,8 +343,7 @@ command_matches(sudoers_cmnd, sudoers_args) (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) || (sudoers_args && fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) { - if (safe_cmnd) - free(safe_cmnd); + efree(safe_cmnd); safe_cmnd = estrdup(user_cmnd); return(TRUE); } else @@ -382,8 +379,7 @@ command_matches(sudoers_cmnd, sudoers_args) (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) || (sudoers_args && fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) { - if (safe_cmnd) - free(safe_cmnd); + efree(safe_cmnd); safe_cmnd = estrdup(sudoers_cmnd); return(TRUE); } else @@ -411,8 +407,7 @@ command_matches(sudoers_cmnd, sudoers_args) continue; if (user_stat->st_dev == sudoers_stat.st_dev && user_stat->st_ino == sudoers_stat.st_ino) { - if (safe_cmnd) - free(safe_cmnd); + efree(safe_cmnd); safe_cmnd = estrdup(buf); break; } @@ -581,7 +576,7 @@ netgr_matches(netgr, lhost, shost, user) if (domain == (char *) -1) { domain = (char *) emalloc(MAXHOSTNAMELEN); if (getdomainname(domain, MAXHOSTNAMELEN) == -1 || *domain == '\0') { - free(domain); + efree(domain); domain = NULL; } } diff --git a/mon_systrace.c b/mon_systrace.c index 7e65f70d8..6c3802d99 100644 --- a/mon_systrace.c +++ b/mon_systrace.c @@ -363,7 +363,7 @@ rm_child(pid) prev->next = cur->next; else children.first = cur->next; - free(cur); + efree(cur); break; } prev = cur; diff --git a/pwutil.c b/pwutil.c index 213818d1d..2bc98b466 100644 --- a/pwutil.c +++ b/pwutil.c @@ -290,11 +290,11 @@ sudo_fakepwuid(uid) /* Store by uid and by name, overwriting cached version. */ if ((node = rbinsert(pwcache_byuid, pw)) != NULL) { - free(node->data); + efree(node->data); node->data = (VOID *) pw; } if ((node = rbinsert(pwcache_byname, pw)) != NULL) { - free(node->data); + efree(node->data); node->data = (VOID *) pw; } return(pw); @@ -320,11 +320,11 @@ sudo_fakepwnam(user) /* Store by uid and by name, overwriting cached version. */ if ((node = rbinsert(pwcache_byuid, pw)) != NULL) { - free(node->data); + efree(node->data); node->data = (VOID *) pw; } if ((node = rbinsert(pwcache_byname, pw)) != NULL) { - free(node->data); + efree(node->data); node->data = (VOID *) pw; } return(pw); @@ -362,9 +362,9 @@ pw_free(v) if (pw->pw_passwd != NULL) { zero_bytes(pw->pw_passwd, strlen(pw->pw_passwd)); - free(pw->pw_passwd); + efree(pw->pw_passwd); } - free(pw); + efree(pw); } /* diff --git a/redblack.c b/redblack.c index 528cfcbec..f6b595a22 100644 --- a/redblack.c +++ b/redblack.c @@ -326,7 +326,7 @@ _rbdestroy(tree, node, destroy) _rbdestroy(tree, node->right, destroy); if (destroy != NULL) destroy(node->data); - free(node); + efree(node); } } @@ -340,7 +340,7 @@ rbdestroy(tree, destroy) void (*destroy)__P((VOID *)); { _rbdestroy(tree, rbfirst(tree), destroy); - free(tree); + efree(tree); } /* @@ -379,7 +379,7 @@ rbdelete(tree, victim) else victim->parent->right = succ; data = victim->data; - free(victim); + efree(victim); } else { pred = victim->left == rbnil(tree) ? victim->right : victim->left; if (victim->parent == rbroot(tree)) { @@ -394,7 +394,7 @@ rbdelete(tree, victim) if (victim->color == black) rbrepair(tree, pred); data = victim->data; - free(victim); + efree(victim); } return(data); } diff --git a/sudo.c b/sudo.c index 1216c1e24..0664e4427 100644 --- a/sudo.c +++ b/sudo.c @@ -1091,8 +1091,8 @@ set_fqdn() "unable to lookup %s via gethostbyname()", user_host); } else { if (user_shost != user_host) - free(user_shost); - free(user_host); + efree(user_shost); + efree(user_host); user_host = estrdup(hp->h_name); } if ((p = strchr(user_host, '.'))) { diff --git a/sudo.h b/sudo.h index 52e54a16e..4769c4da6 100644 --- a/sudo.h +++ b/sudo.h @@ -237,6 +237,7 @@ int easprintf __P((char **, const char *, ...)) __printflike(2, 3); int evasprintf __P((char **, const char *, va_list)) __printflike(2, 0); +void efree __P((VOID *)); void dump_defaults __P((void)); void dump_auth_methods __P((void)); void init_envtables __P((void)); diff --git a/toke.c b/toke.c index 7af1e5bb6..557960e24 100644 --- a/toke.c +++ b/toke.c @@ -2756,8 +2756,7 @@ fill_args(s, len, addspace) (char *) realloc(yylval.command.args, arg_size) : (char *) malloc(arg_size); if (p == NULL) { - if (yylval.command.args != NULL) - free(yylval.command.args); + efree(yylval.command.args); yyerror("unable to allocate memory"); return(FALSE); } else @@ -2833,7 +2832,7 @@ switch_buffer(path) fclose(YY_CURRENT_BUFFER->yy_input_file); yy_delete_buffer(YY_CURRENT_BUFFER); yy_switch_to_buffer(state[depth].bs); - free(sudoers); + efree(sudoers); sudoers = state[depth].path; sudolineno = state[depth].lineno; keepopen = FALSE; diff --git a/toke.l b/toke.l index 7abefb724..68c6bee29 100644 --- a/toke.l +++ b/toke.l @@ -508,8 +508,7 @@ fill_args(s, len, addspace) (char *) realloc(yylval.command.args, arg_size) : (char *) malloc(arg_size); if (p == NULL) { - if (yylval.command.args != NULL) - free(yylval.command.args); + efree(yylval.command.args); yyerror("unable to allocate memory"); return(FALSE); } else @@ -585,7 +584,7 @@ switch_buffer(path) fclose(YY_CURRENT_BUFFER->yy_input_file); yy_delete_buffer(YY_CURRENT_BUFFER); yy_switch_to_buffer(state[depth].bs); - free(sudoers); + efree(sudoers); sudoers = state[depth].path; sudolineno = state[depth].lineno; keepopen = FALSE; diff --git a/visudo.c b/visudo.c index fe79dbbff..662b604b9 100644 --- a/visudo.c +++ b/visudo.c @@ -489,7 +489,7 @@ install_sudoers(sp) * mv(1) in case sp->tpath and sp->path are on different file systems. */ if (rename(sp->tpath, sp->path) == 0) { - free(sp->tpath); + efree(sp->tpath); sp->tpath = NULL; } else { if (errno == EXDEV) { @@ -511,11 +511,11 @@ install_sudoers(sp) warningx("command failed: '%s %s %s', %s unchanged", _PATH_MV, sp->tpath, sp->path, sp->path); (void) unlink(sp->tpath); - free(sp->tpath); + efree(sp->tpath); sp->tpath = NULL; return(FALSE); } - free(sp->tpath); + efree(sp->tpath); sp->tpath = NULL; } else { warning("error renaming %s, %s unchanged", sp->tpath, sp->path); @@ -716,7 +716,7 @@ open_sudoers(path, keepopen) entry->tpath = NULL; if (entry->fd == -1) { warning("%s", entry->path); - free(entry); + efree(entry); return(NULL); } if (!lock_file(entry->fd, SUDO_TLOCK)) @@ -828,8 +828,7 @@ get_editor(args) * find one that exists, is regular, and is executable. */ if (Editor == NULL || *Editor == '\0') { - if (EditorPath != NULL) - free(EditorPath); + efree(EditorPath); EditorPath = estrdup(def_editor); Editor = strtok(EditorPath, ":"); do {