[CLEANUP] remove 65 useless NULL checks before free

C specification clearly states that free(NULL) is a no-op.
So remove useless checks before calling free.
This commit is contained in:
Willy Tarreau 2008-08-03 12:19:50 +02:00
parent af72a1d8ec
commit a534fea478
9 changed files with 72 additions and 183 deletions

View File

@ -2078,10 +2078,10 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
}
curproxy->lbprm.algo &= ~BE_LB_ALGO;
curproxy->lbprm.algo |= BE_LB_ALGO_PH;
if (curproxy->url_param_name)
free(curproxy->url_param_name);
free(curproxy->url_param_name);
curproxy->url_param_name = strdup(args[1]);
curproxy->url_param_len = strlen(args[1]);
curproxy->url_param_len = strlen(args[1]);
if (*args[2]) {
if (strcmp(args[2], "check_post")) {
snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");

View File

@ -715,17 +715,15 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
/* FIXME-20070101: we should do this too at the end of the
* config parsing to free all default values.
*/
if (defproxy.check_req) free(defproxy.check_req);
if (defproxy.cookie_name) free(defproxy.cookie_name);
if (defproxy.url_param_name) free(defproxy.url_param_name);
if (defproxy.capture_name) free(defproxy.capture_name);
if (defproxy.monitor_uri) free(defproxy.monitor_uri);
if (defproxy.defbe.name) free(defproxy.defbe.name);
free(defproxy.check_req);
free(defproxy.cookie_name);
free(defproxy.url_param_name);
free(defproxy.capture_name);
free(defproxy.monitor_uri);
free(defproxy.defbe.name);
for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
if (defproxy.errmsg[rc].len)
free(defproxy.errmsg[rc].str);
}
for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
free(defproxy.errmsg[rc].str);
/* we cannot free uri_auth because it might already be used */
init_default_instance();
@ -804,9 +802,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
return -1;
}
if (curproxy->monitor_uri != NULL)
free(curproxy->monitor_uri);
free(curproxy->monitor_uri);
curproxy->monitor_uri_len = strlen(args[1]);
curproxy->monitor_uri = (char *)calloc(1, curproxy->monitor_uri_len + 1);
memcpy(curproxy->monitor_uri, args[1], curproxy->monitor_uri_len);
@ -875,26 +871,17 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
}
else if (!strcmp(args[0], "cookie")) { /* cookie name */
int cur_arg;
// if (curproxy == &defproxy) {
// Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
// return -1;
// }
if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
return 0;
if (curproxy->cookie_name != NULL) {
// Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
// file, linenum);
// return 0;
free(curproxy->cookie_name);
}
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n",
file, linenum, args[0]);
return -1;
}
free(curproxy->cookie_name);
curproxy->cookie_name = strdup(args[1]);
curproxy->cookie_len = strlen(curproxy->cookie_name);
@ -963,27 +950,17 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
}
}/* end else if (!strcmp(args[0], "cookie")) */
else if (!strcmp(args[0], "appsession")) { /* cookie name */
// if (curproxy == &defproxy) {
// Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
// return -1;
// }
if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
return 0;
if (curproxy->appsession_name != NULL) {
// Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
// file, linenum);
// return 0;
free(curproxy->appsession_name);
}
if (*(args[5]) == 0) {
Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n",
file, linenum, args[0]);
return -1;
}
have_appsession = 1;
free(curproxy->appsession_name);
curproxy->appsession_name = strdup(args[1]);
curproxy->appsession_name_len = strlen(curproxy->appsession_name);
curproxy->appsession_len = atoi(args[3]);
@ -1005,23 +982,12 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
return 0;
if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */
// if (curproxy == &defproxy) {
// Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
// return -1;
// }
if (curproxy->capture_name != NULL) {
// Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
// file, linenum, args[0]);
// return 0;
free(curproxy->capture_name);
}
if (*(args[4]) == 0) {
Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n",
file, linenum, args[0]);
return -1;
}
free(curproxy->capture_name);
curproxy->capture_name = strdup(args[2]);
curproxy->capture_namelen = strlen(curproxy->capture_name);
curproxy->capture_len = atol(args[4]);
@ -1378,9 +1344,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
return 0;
/* use HTTP request to check servers' health */
if (curproxy->check_req != NULL) {
free(curproxy->check_req);
}
free(curproxy->check_req);
curproxy->options &= ~PR_O_SSL3_CHK;
curproxy->options &= ~PR_O_SMTP_CHK;
curproxy->options |= PR_O_HTTP_CHK;
@ -1409,18 +1373,14 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[1], NULL))
return 0;
if (curproxy->check_req != NULL) {
free(curproxy->check_req);
}
free(curproxy->check_req);
curproxy->options &= ~PR_O_HTTP_CHK;
curproxy->options &= ~PR_O_SMTP_CHK;
curproxy->options |= PR_O_SSL3_CHK;
}
else if (!strcmp(args[1], "smtpchk")) {
/* use SMTP request to check servers' health */
if (curproxy->check_req != NULL) {
free(curproxy->check_req);
}
free(curproxy->check_req);
curproxy->options &= ~PR_O_HTTP_CHK;
curproxy->options &= ~PR_O_SSL3_CHK;
curproxy->options |= PR_O_SMTP_CHK;
@ -1501,8 +1461,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
Alert("parsing [%s:%d] : '%s' expects a backend name.\n", file, linenum, args[0]);
return -1;
}
if (curproxy->defbe.name)
free(curproxy->defbe.name);
free(curproxy->defbe.name);
curproxy->defbe.name = strdup(args[1]);
}
else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) {
@ -2617,8 +2576,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
if (http_err_codes[rc] == errnum) {
if (curproxy->errmsg[rc].str)
free(curproxy->errmsg[rc].str);
free(curproxy->errmsg[rc].str);
curproxy->errmsg[rc].str = err;
curproxy->errmsg[rc].len = errlen;
break;
@ -2675,8 +2633,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int inv)
errnum = atol(args[1]);
for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
if (http_err_codes[rc] == errnum) {
if (curproxy->errmsg[rc].str)
free(curproxy->errmsg[rc].str);
free(curproxy->errmsg[rc].str);
curproxy->errmsg[rc].str = err;
curproxy->errmsg[rc].len = errlen;
break;
@ -2856,14 +2813,12 @@ int readcfgfile(const char *file)
!strcmp(args[0], "ruleset") ||
!strcmp(args[0], "defaults")) { /* new proxy */
confsect = CFG_LISTEN;
if (cursection)
free(cursection);
free(cursection);
cursection = strdup(args[0]);
}
else if (!strcmp(args[0], "global")) { /* global config */
confsect = CFG_GLOBAL;
if (cursection)
free(cursection);
free(cursection);
cursection = strdup(args[0]);
}
/* else it's a section keyword */
@ -2882,8 +2837,7 @@ int readcfgfile(const char *file)
goto err;
}
}
if (cursection)
free(cursection);
free(cursection);
cursection = NULL;
fclose(f);
@ -3284,14 +3238,12 @@ int readcfgfile(const char *file)
}
}
if (cursection)
free(cursection);
free(cursection);
cursection = NULL;
return 0;
err:
if (cursection)
free(cursection);
free(cursection);
cursection = NULL;
return -1;
}

View File

@ -325,14 +325,10 @@ REGPRM1 static void _do_term(struct poller *p)
{
fd_flush_changes();
if (chg_ptr)
free(chg_ptr);
if (chg_list)
free(chg_list);
if (fd_evts)
free(fd_evts);
if (epoll_events)
free(epoll_events);
free(chg_ptr);
free(chg_list);
free(fd_evts);
free(epoll_events);
close(epoll_fd);
epoll_fd = 0;

View File

@ -199,12 +199,9 @@ REGPRM1 static int _do_init(struct poller *p)
*/
REGPRM1 static void _do_term(struct poller *p)
{
if (fd_evts[DIR_WR])
free(fd_evts[DIR_WR]);
if (fd_evts[DIR_RD])
free(fd_evts[DIR_RD]);
if (kev)
free(kev);
free(fd_evts[DIR_WR]);
free(fd_evts[DIR_RD]);
free(kev);
close(kqueue_fd);
kqueue_fd = 0;

View File

@ -207,12 +207,9 @@ REGPRM1 static int _do_init(struct poller *p)
*/
REGPRM1 static void _do_term(struct poller *p)
{
if (fd_evts[DIR_WR])
free(fd_evts[DIR_WR]);
if (fd_evts[DIR_RD])
free(fd_evts[DIR_RD]);
if (poll_events)
free(poll_events);
free(fd_evts[DIR_WR]);
free(fd_evts[DIR_RD]);
free(poll_events);
p->private = NULL;
p->pref = 0;
}

View File

@ -202,14 +202,10 @@ REGPRM1 static int _do_init(struct poller *p)
*/
REGPRM1 static void _do_term(struct poller *p)
{
if (fd_evts[DIR_WR])
free(fd_evts[DIR_WR]);
if (fd_evts[DIR_RD])
free(fd_evts[DIR_RD]);
if (tmp_evts[DIR_WR])
free(tmp_evts[DIR_WR]);
if (tmp_evts[DIR_RD])
free(tmp_evts[DIR_RD]);
free(fd_evts[DIR_WR]);
free(fd_evts[DIR_RD]);
free(tmp_evts[DIR_WR]);
free(tmp_evts[DIR_RD]);
p->private = NULL;
p->pref = 0;
}

View File

@ -541,12 +541,9 @@ REGPRM1 static int _do_init(struct poller *p)
*/
REGPRM1 static void _do_term(struct poller *p)
{
if (fd_list)
free(fd_list);
if (spec_list)
free(spec_list);
if (epoll_events)
free(epoll_events);
free(fd_list);
free(spec_list);
free(epoll_events);
close(epoll_fd);
epoll_fd = 0;

View File

@ -547,8 +547,7 @@ void init(int argc, char **argv)
global.maxconn = cfg_maxconn;
if (cfg_pidfile) {
if (global.pidfile)
free(global.pidfile);
free(global.pidfile);
global.pidfile = strdup(cfg_pidfile);
}
@ -645,41 +644,22 @@ void deinit(void)
int i;
while (p) {
if (p->id)
free(p->id);
free(p->id);
free(p->check_req);
free(p->cookie_name);
free(p->cookie_domain);
free(p->url_param_name);
free(p->capture_name);
free(p->monitor_uri);
if (p->check_req)
free(p->check_req);
for (i = 0; i < HTTP_ERR_SIZE; i++)
free(p->errmsg[i].str);
if (p->cookie_name)
free(p->cookie_name);
for (i = 0; i < p->nb_reqadd; i++)
free(p->req_add[i]);
if (p->cookie_domain)
free(p->cookie_domain);
if (p->url_param_name)
free(p->url_param_name);
if (p->capture_name)
free(p->capture_name);
if (p->monitor_uri)
free(p->monitor_uri);
for (i = 0; i < HTTP_ERR_SIZE; i++) {
if (p->errmsg[i].len)
free(p->errmsg[i].str);
}
for (i = 0; i < p->nb_reqadd; i++) {
if (p->req_add[i])
free(p->req_add[i]);
}
for (i = 0; i < p->nb_rspadd; i++) {
if (p->rsp_add[i])
free(p->rsp_add[i]);
}
for (i = 0; i < p->nb_rspadd; i++)
free(p->rsp_add[i]);
list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
LIST_DEL(&cond->list);
@ -743,10 +723,8 @@ void deinit(void)
list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
LIST_DEL(&rule->list);
prune_acl_cond(rule->cond);
free(rule->cond);
free(rule);
}
@ -758,14 +736,12 @@ void deinit(void)
free(rdr);
}
if (p->appsession_name)
free(p->appsession_name);
free(p->appsession_name);
h = p->req_cap;
while (h) {
h_next = h->next;
if (h->name)
free(h->name);
free(h->name);
pool_destroy2(h->pool);
free(h);
h = h_next;
@ -774,9 +750,7 @@ void deinit(void)
h = p->rsp_cap;
while (h) {
h_next = h->next;
if (h->name)
free(h->name);
free(h->name);
pool_destroy2(h->pool);
free(h);
h = h_next;
@ -791,12 +765,8 @@ void deinit(void)
task_free(s->check);
}
if (s->id)
free(s->id);
if (s->cookie)
free(s->cookie);
free(s->id);
free(s->cookie);
free(s);
s = s_next;
}/* end while(s) */
@ -820,36 +790,24 @@ void deinit(void)
uap = ua;
ua = ua->next;
if (uap->uri_prefix)
free(uap->uri_prefix);
if (uap->auth_realm)
free(uap->auth_realm);
free(uap->uri_prefix);
free(uap->auth_realm);
while (uap->users) {
user = uap->users;
uap->users = uap->users->next;
free(user->user_pwd);
free(user);
}
free(uap);
}
protocol_unbind_all();
if (global.chroot) free(global.chroot);
global.chroot = NULL;
if (global.pidfile) free(global.pidfile);
global.pidfile = NULL;
if (fdtab) free(fdtab);
fdtab = NULL;
if (oldpids)
free(oldpids);
free(global.chroot); global.chroot = NULL;
free(global.pidfile); global.pidfile = NULL;
free(fdtab); fdtab = NULL;
free(oldpids); oldpids = NULL;
pool_destroy2(pool2_session);
pool_destroy2(pool2_buffer);

View File

@ -73,11 +73,9 @@ struct uri_auth *stats_set_uri(struct uri_auth **root, char *uri)
if ((u = stats_check_init_uri_auth(root)) == NULL)
goto out_u;
if (u->uri_prefix)
free(u->uri_prefix);
u->uri_len = uri_len;
free(u->uri_prefix);
u->uri_prefix = uri_copy;
u->uri_len = uri_len;
return u;
out_u:
@ -101,9 +99,7 @@ struct uri_auth *stats_set_realm(struct uri_auth **root, char *realm)
if ((u = stats_check_init_uri_auth(root)) == NULL)
goto out_u;
if (u->auth_realm)
free(u->auth_realm);
free(u->auth_realm);
u->auth_realm = realm_copy;
return u;