Commit bcbe69f7 by Markus Elfring Committed by Stéphane Graber

Bug #158: Deletion of unnecessary checks before calls of the function "free"

The function "free" is documented in the way that no action shall occur for a passed null pointer. It is therefore not needed that a function caller repeats a corresponding check. http://stackoverflow.com/questions/18775608/free-a-null-pointer-anyway-or-check-first This issue was fixed by using the software Coccinelle 1.0.0-rc23. Signed-off-by: 's avatarMarkus Elfring <elfring@users.sourceforge.net> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent 5aeb7e8a
...@@ -184,7 +184,6 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid) ...@@ -184,7 +184,6 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
} }
} }
if (line)
free(line); free(line);
fclose(proc_file); fclose(proc_file);
...@@ -205,7 +204,6 @@ out_error: ...@@ -205,7 +204,6 @@ out_error:
static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx) static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx)
{ {
if (ctx->lsm_label)
free(ctx->lsm_label); free(ctx->lsm_label);
if (ctx->container) if (ctx->container)
lxc_container_put(ctx->container); lxc_container_put(ctx->container);
...@@ -520,7 +518,6 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -520,7 +518,6 @@ static char *lxc_attach_getpwshell(uid_t uid)
} }
if (!token) if (!token)
continue; continue;
if (result)
free(result); free(result);
result = strdup(token); result = strdup(token);
......
...@@ -1451,7 +1451,6 @@ out: ...@@ -1451,7 +1451,6 @@ out:
close(fddst); close(fddst);
if (fd != -1) if (fd != -1)
close(fd); close(fd);
if (newfull)
free(newfull); free(newfull);
return ret; return ret;
} }
...@@ -2875,11 +2874,8 @@ static const size_t numbdevs = sizeof(bdevs) / sizeof(struct bdev_type); ...@@ -2875,11 +2874,8 @@ static const size_t numbdevs = sizeof(bdevs) / sizeof(struct bdev_type);
void bdev_put(struct bdev *bdev) void bdev_put(struct bdev *bdev)
{ {
if (bdev->mntopts)
free(bdev->mntopts); free(bdev->mntopts);
if (bdev->src)
free(bdev->src); free(bdev->src);
if (bdev->dest)
free(bdev->dest); free(bdev->dest);
free(bdev); free(bdev);
} }
......
...@@ -2187,7 +2187,6 @@ static bool do_init_cpuset_file(struct cgroup_mount_point *mp, ...@@ -2187,7 +2187,6 @@ static bool do_init_cpuset_file(struct cgroup_mount_point *mp,
SYSERROR("failed writing %s", childfile); SYSERROR("failed writing %s", childfile);
out: out:
if (parentfile)
free(parentfile); free(parentfile);
free(childfile); free(childfile);
return ok; return ok;
...@@ -2248,7 +2247,6 @@ static void cgfs_destroy(void *hdata) ...@@ -2248,7 +2247,6 @@ static void cgfs_destroy(void *hdata)
if (!d) if (!d)
return; return;
if (d->name)
free(d->name); free(d->name);
if (d->info) if (d->info)
lxc_cgroup_process_info_free_and_remove(d->info); lxc_cgroup_process_info_free_and_remove(d->info);
......
...@@ -582,7 +582,6 @@ static void cgm_destroy(void *hdata) ...@@ -582,7 +582,6 @@ static void cgm_destroy(void *hdata)
cgm_remove_cgroup(slist[i], d->cgroup_path); cgm_remove_cgroup(slist[i], d->cgroup_path);
free(d->name); free(d->name);
if (d->cgroup_path)
free(d->cgroup_path); free(d->cgroup_path);
free(d); free(d);
cgm_dbus_disconnect(); cgm_dbus_disconnect();
......
...@@ -3008,9 +3008,8 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -3008,9 +3008,8 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
out_delete: out_delete:
lxc_netdev_delete_by_name(veth1); lxc_netdev_delete_by_name(veth1);
if (!netdev->priv.veth_attr.pair && veth1) if (!netdev->priv.veth_attr.pair)
free(veth1); free(veth1);
if(veth2)
free(veth2); free(veth2);
return -1; return -1;
} }
...@@ -3525,7 +3524,6 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid) ...@@ -3525,7 +3524,6 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
break; break;
} }
if (buf)
free(buf); free(buf);
return ret; return ret;
} }
...@@ -4044,7 +4042,6 @@ void remount_all_slave(void) ...@@ -4044,7 +4042,6 @@ void remount_all_slave(void)
} }
} }
fclose(f); fclose(f);
if (line)
free(line); free(line);
} }
...@@ -4289,21 +4286,14 @@ static void lxc_remove_nic(struct lxc_list *it) ...@@ -4289,21 +4286,14 @@ static void lxc_remove_nic(struct lxc_list *it)
lxc_list_del(it); lxc_list_del(it);
if (netdev->link)
free(netdev->link); free(netdev->link);
if (netdev->name)
free(netdev->name); free(netdev->name);
if (netdev->type == LXC_NET_VETH && netdev->priv.veth_attr.pair) if (netdev->type == LXC_NET_VETH)
free(netdev->priv.veth_attr.pair); free(netdev->priv.veth_attr.pair);
if (netdev->upscript)
free(netdev->upscript); free(netdev->upscript);
if (netdev->hwaddr)
free(netdev->hwaddr); free(netdev->hwaddr);
if (netdev->mtu)
free(netdev->mtu); free(netdev->mtu);
if (netdev->ipv4_gateway)
free(netdev->ipv4_gateway); free(netdev->ipv4_gateway);
if (netdev->ipv6_gateway)
free(netdev->ipv6_gateway); free(netdev->ipv6_gateway);
lxc_list_for_each_safe(it2, &netdev->ipv4, next) { lxc_list_for_each_safe(it2, &netdev->ipv4, next) {
lxc_list_del(it2); lxc_list_del(it2);
...@@ -4367,41 +4357,27 @@ int lxc_clear_nic(struct lxc_conf *c, const char *key) ...@@ -4367,41 +4357,27 @@ int lxc_clear_nic(struct lxc_conf *c, const char *key)
free(it2); free(it2);
} }
} else if (strcmp(p1, ".link") == 0) { } else if (strcmp(p1, ".link") == 0) {
if (netdev->link) {
free(netdev->link); free(netdev->link);
netdev->link = NULL; netdev->link = NULL;
}
} else if (strcmp(p1, ".name") == 0) { } else if (strcmp(p1, ".name") == 0) {
if (netdev->name) {
free(netdev->name); free(netdev->name);
netdev->name = NULL; netdev->name = NULL;
}
} else if (strcmp(p1, ".script.up") == 0) { } else if (strcmp(p1, ".script.up") == 0) {
if (netdev->upscript) {
free(netdev->upscript); free(netdev->upscript);
netdev->upscript = NULL; netdev->upscript = NULL;
}
} else if (strcmp(p1, ".hwaddr") == 0) { } else if (strcmp(p1, ".hwaddr") == 0) {
if (netdev->hwaddr) {
free(netdev->hwaddr); free(netdev->hwaddr);
netdev->hwaddr = NULL; netdev->hwaddr = NULL;
}
} else if (strcmp(p1, ".mtu") == 0) { } else if (strcmp(p1, ".mtu") == 0) {
if (netdev->mtu) {
free(netdev->mtu); free(netdev->mtu);
netdev->mtu = NULL; netdev->mtu = NULL;
}
} else if (strcmp(p1, ".ipv4.gateway") == 0) { } else if (strcmp(p1, ".ipv4.gateway") == 0) {
if (netdev->ipv4_gateway) {
free(netdev->ipv4_gateway); free(netdev->ipv4_gateway);
netdev->ipv4_gateway = NULL; netdev->ipv4_gateway = NULL;
}
} else if (strcmp(p1, ".ipv6.gateway") == 0) { } else if (strcmp(p1, ".ipv6.gateway") == 0) {
if (netdev->ipv6_gateway) {
free(netdev->ipv6_gateway); free(netdev->ipv6_gateway);
netdev->ipv6_gateway = NULL; netdev->ipv6_gateway = NULL;
} }
}
else return -1; else return -1;
return 0; return 0;
...@@ -4551,32 +4527,19 @@ void lxc_conf_free(struct lxc_conf *conf) ...@@ -4551,32 +4527,19 @@ void lxc_conf_free(struct lxc_conf *conf)
{ {
if (!conf) if (!conf)
return; return;
if (conf->console.log_path)
free(conf->console.log_path); free(conf->console.log_path);
if (conf->console.path)
free(conf->console.path); free(conf->console.path);
if (conf->rootfs.mount)
free(conf->rootfs.mount); free(conf->rootfs.mount);
if (conf->rootfs.options)
free(conf->rootfs.options); free(conf->rootfs.options);
if (conf->rootfs.path)
free(conf->rootfs.path); free(conf->rootfs.path);
if (conf->rootfs.pivot)
free(conf->rootfs.pivot); free(conf->rootfs.pivot);
if (conf->logfile)
free(conf->logfile); free(conf->logfile);
if (conf->utsname)
free(conf->utsname); free(conf->utsname);
if (conf->ttydir)
free(conf->ttydir); free(conf->ttydir);
if (conf->fstab)
free(conf->fstab); free(conf->fstab);
if (conf->rcfile)
free(conf->rcfile); free(conf->rcfile);
lxc_clear_config_network(conf); lxc_clear_config_network(conf);
if (conf->lsm_aa_profile)
free(conf->lsm_aa_profile); free(conf->lsm_aa_profile);
if (conf->lsm_se_context)
free(conf->lsm_se_context); free(conf->lsm_se_context);
lxc_seccomp_free(conf); lxc_seccomp_free(conf);
lxc_clear_config_caps(conf); lxc_clear_config_caps(conf);
...@@ -4848,7 +4811,6 @@ void suggest_default_idmap(void) ...@@ -4848,7 +4811,6 @@ void suggest_default_idmap(void)
} }
fclose(f); fclose(f);
if (line)
free(line); free(line);
if (!urange || !grange) { if (!urange || !grange) {
......
...@@ -287,7 +287,6 @@ static int config_string_item(char **conf_item, const char *value) ...@@ -287,7 +287,6 @@ static int config_string_item(char **conf_item, const char *value)
char *new_value; char *new_value;
if (!value || strlen(value) == 0) { if (!value || strlen(value) == 0) {
if (*conf_item)
free(*conf_item); free(*conf_item);
*conf_item = NULL; *conf_item = NULL;
return 0; return 0;
...@@ -299,7 +298,6 @@ static int config_string_item(char **conf_item, const char *value) ...@@ -299,7 +298,6 @@ static int config_string_item(char **conf_item, const char *value)
return -1; return -1;
} }
if (*conf_item)
free(*conf_item); free(*conf_item);
*conf_item = new_value; *conf_item = new_value;
return 0; return 0;
...@@ -1326,14 +1324,11 @@ static int config_cgroup(const char *key, const char *value, ...@@ -1326,14 +1324,11 @@ static int config_cgroup(const char *key, const char *value,
return 0; return 0;
out: out:
if (cglist)
free(cglist); free(cglist);
if (cgelem) { if (cgelem) {
if (cgelem->subsystem)
free(cgelem->subsystem); free(cgelem->subsystem);
if (cgelem->value)
free(cgelem->value); free(cgelem->value);
free(cgelem); free(cgelem);
...@@ -1394,7 +1389,6 @@ static int config_idmap(const char *key, const char *value, struct lxc_conf *lxc ...@@ -1394,7 +1389,6 @@ static int config_idmap(const char *key, const char *value, struct lxc_conf *lxc
return 0; return 0;
out: out:
if (idmaplist)
free(idmaplist); free(idmaplist);
if (idmap) { if (idmap) {
...@@ -1683,7 +1677,6 @@ static int config_utsname(const char *key, const char *value, ...@@ -1683,7 +1677,6 @@ static int config_utsname(const char *key, const char *value,
} }
strcpy(utsname->nodename, value); strcpy(utsname->nodename, value);
if (lxc_conf->utsname)
free(lxc_conf->utsname); free(lxc_conf->utsname);
lxc_conf->utsname = utsname; lxc_conf->utsname = utsname;
......
...@@ -77,7 +77,6 @@ again: ...@@ -77,7 +77,6 @@ again:
f = fopen(path, "r"); f = fopen(path, "r");
if (!f) { if (!f) {
SYSERROR("opening %s", path); SYSERROR("opening %s", path);
if (buf)
free(buf); free(buf);
return NULL; return NULL;
} }
...@@ -115,7 +114,6 @@ static int apparmor_am_unconfined(void) ...@@ -115,7 +114,6 @@ static int apparmor_am_unconfined(void)
int ret = 0; int ret = 0;
if (!p || strcmp(p, "unconfined") == 0) if (!p || strcmp(p, "unconfined") == 0)
ret = 1; ret = 1;
if (p)
free(p); free(p);
return ret; return ret;
} }
......
...@@ -499,7 +499,6 @@ int main(int argc, char *argv[]) ...@@ -499,7 +499,6 @@ int main(int argc, char *argv[])
} }
} }
if ( c_groups_lists )
free(c_groups_lists); free(c_groups_lists);
if ( cmd_groups_list ) { if ( cmd_groups_list ) {
......
...@@ -72,7 +72,6 @@ static void print_file(char *path) ...@@ -72,7 +72,6 @@ static void print_file(char *path)
while (getline(&line, &sz, f) != -1) { while (getline(&line, &sz, f) != -1) {
printf("%s", line); printf("%s", line);
} }
if (line)
free(line); free(line);
fclose(f); fclose(f);
} }
......
...@@ -89,7 +89,6 @@ static int ensure_path(char **confpath, const char *path) ...@@ -89,7 +89,6 @@ static int ensure_path(char **confpath, const char *path)
err = 0; err = 0;
err: err:
if (fullpath)
free(fullpath); free(fullpath);
return err; return err;
} }
......
...@@ -133,7 +133,6 @@ static int get_alloted(char *me, char *intype, char *link) ...@@ -133,7 +133,6 @@ static int get_alloted(char *me, char *intype, char *link)
return n; return n;
} }
fclose(fin); fclose(fin);
if (line)
free(line); free(line);
return -1; return -1;
} }
......
...@@ -244,7 +244,6 @@ static int read_default_map(char *fnam, int which, char *username) ...@@ -244,7 +244,6 @@ static int read_default_map(char *fnam, int which, char *username)
break; break;
} }
if (line)
free(line); free(line);
fclose(fin); fclose(fin);
return 0; return 0;
......
...@@ -221,14 +221,10 @@ static void lxc_container_free(struct lxc_container *c) ...@@ -221,14 +221,10 @@ static void lxc_container_free(struct lxc_container *c)
if (!c) if (!c)
return; return;
if (c->configfile) {
free(c->configfile); free(c->configfile);
c->configfile = NULL; c->configfile = NULL;
}
if (c->error_string) {
free(c->error_string); free(c->error_string);
c->error_string = NULL; c->error_string = NULL;
}
if (c->slock) { if (c->slock) {
lxc_putlock(c->slock); lxc_putlock(c->slock);
c->slock = NULL; c->slock = NULL;
...@@ -237,18 +233,14 @@ static void lxc_container_free(struct lxc_container *c) ...@@ -237,18 +233,14 @@ static void lxc_container_free(struct lxc_container *c)
lxc_putlock(c->privlock); lxc_putlock(c->privlock);
c->privlock = NULL; c->privlock = NULL;
} }
if (c->name) {
free(c->name); free(c->name);
c->name = NULL; c->name = NULL;
}
if (c->lxc_conf) { if (c->lxc_conf) {
lxc_conf_free(c->lxc_conf); lxc_conf_free(c->lxc_conf);
c->lxc_conf = NULL; c->lxc_conf = NULL;
} }
if (c->config_path) {
free(c->config_path); free(c->config_path);
c->config_path = NULL; c->config_path = NULL;
}
free(c); free(c);
} }
...@@ -950,7 +942,6 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet ...@@ -950,7 +942,6 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
exit(1); exit(1);
} }
} else { // TODO come up with a better way here! } else { // TODO come up with a better way here!
if (bdev->dest)
free(bdev->dest); free(bdev->dest);
bdev->dest = strdup(bdev->src); bdev->dest = strdup(bdev->src);
} }
...@@ -1382,7 +1373,6 @@ out: ...@@ -1382,7 +1373,6 @@ out:
if (!ret && c) if (!ret && c)
lxcapi_destroy(c); lxcapi_destroy(c);
free_tpath: free_tpath:
if (tpath)
free(tpath); free(tpath);
return ret; return ret;
} }
...@@ -1981,8 +1971,8 @@ static void mod_all_rdeps(struct lxc_container *c, bool inc) ...@@ -1981,8 +1971,8 @@ static void mod_all_rdeps(struct lxc_container *c, bool inc)
lxc_container_put(p); lxc_container_put(p);
} }
out: out:
if (lxcpath) free(lxcpath); free(lxcpath);
if (lxcname) free(lxcname); free(lxcname);
fclose(f); fclose(f);
} }
...@@ -2173,7 +2163,6 @@ static bool set_config_filename(struct lxc_container *c) ...@@ -2173,7 +2163,6 @@ static bool set_config_filename(struct lxc_container *c)
return false; return false;
} }
if (c->configfile)
free(c->configfile); free(c->configfile);
c->configfile = newpath; c->configfile = newpath;
...@@ -2213,7 +2202,6 @@ static bool lxcapi_set_config_path(struct lxc_container *c, const char *path) ...@@ -2213,7 +2202,6 @@ static bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
oldpath = NULL; oldpath = NULL;
} }
err: err:
if (oldpath)
free(oldpath); free(oldpath);
container_mem_unlock(c); container_mem_unlock(c);
return b; return b;
...@@ -2571,7 +2559,6 @@ static int clone_update_rootfs(struct clone_update_data *data) ...@@ -2571,7 +2559,6 @@ static int clone_update_rootfs(struct clone_update_data *data)
return -1; return -1;
} }
} else { // TODO come up with a better way } else { // TODO come up with a better way
if (bdev->dest)
free(bdev->dest); free(bdev->dest);
bdev->dest = strdup(bdev->src); bdev->dest = strdup(bdev->src);
} }
...@@ -2975,13 +2962,9 @@ static int lxcapi_snapshot(struct lxc_container *c, const char *commentfile) ...@@ -2975,13 +2962,9 @@ static int lxcapi_snapshot(struct lxc_container *c, const char *commentfile)
static void lxcsnap_free(struct lxc_snapshot *s) static void lxcsnap_free(struct lxc_snapshot *s)
{ {
if (s->name)
free(s->name); free(s->name);
if (s->comment_pathname)
free(s->comment_pathname); free(s->comment_pathname);
if (s->timestamp)
free(s->timestamp); free(s->timestamp);
if (s->lxcpath)
free(s->lxcpath); free(s->lxcpath);
} }
...@@ -3683,7 +3666,6 @@ free_ct_name: ...@@ -3683,7 +3666,6 @@ free_ct_name:
} }
out: out:
if (line)
free(line); free(line);
fclose(f); fclose(f);
...@@ -3756,15 +3738,12 @@ free_ct_list: ...@@ -3756,15 +3738,12 @@ free_ct_list:
for (i = 0; i < ct_list_cnt; i++) { for (i = 0; i < ct_list_cnt; i++) {
lxc_container_put(ct_list[i]); lxc_container_put(ct_list[i]);
} }
if (ct_list)
free(ct_list); free(ct_list);
free_active_name: free_active_name:
for (i = 0; i < active_cnt; i++) { for (i = 0; i < active_cnt; i++) {
if (active_name[i])
free(active_name[i]); free(active_name[i]);
} }
if (active_name)
free(active_name); free(active_name);
free_ct_name: free_ct_name:
......
...@@ -305,10 +305,8 @@ void lxc_putlock(struct lxc_lock *l) ...@@ -305,10 +305,8 @@ void lxc_putlock(struct lxc_lock *l)
close(l->u.f.fd); close(l->u.f.fd);
l->u.f.fd = -1; l->u.f.fd = -1;
} }
if (l->u.f.fname) {
free(l->u.f.fname); free(l->u.f.fname);
l->u.f.fname = NULL; l->u.f.fname = NULL;
}
break; break;
} }
free(l); free(l);
......
...@@ -59,7 +59,6 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data) ...@@ -59,7 +59,6 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
} }
} }
if (line)
free(line); free(line);
fclose(f); fclose(f);
return err; return err;
......
...@@ -581,10 +581,8 @@ int lxc_seccomp_load(struct lxc_conf *conf) ...@@ -581,10 +581,8 @@ int lxc_seccomp_load(struct lxc_conf *conf)
} }
void lxc_seccomp_free(struct lxc_conf *conf) { void lxc_seccomp_free(struct lxc_conf *conf) {
if (conf->seccomp) {
free(conf->seccomp); free(conf->seccomp);
conf->seccomp = NULL; conf->seccomp = NULL;
}
#if HAVE_SCMP_FILTER_CTX #if HAVE_SCMP_FILTER_CTX
if (conf->seccomp_ctx) { if (conf->seccomp_ctx) {
seccomp_release(conf->seccomp_ctx); seccomp_release(conf->seccomp_ctx);
......
...@@ -237,7 +237,6 @@ void lxc_attach_free_options(lxc_attach_options_t *options) ...@@ -237,7 +237,6 @@ void lxc_attach_free_options(lxc_attach_options_t *options)
int i; int i;
if (!options) if (!options)
return; return;
if (options->initial_cwd)
free(options->initial_cwd); free(options->initial_cwd);
if (options->extra_env_vars) { if (options->extra_env_vars) {
for (i = 0; options->extra_env_vars[i]; i++) for (i = 0; options->extra_env_vars[i]; i++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment