]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Bug #158: Deletion of unnecessary checks before calls of the function "free"
authorMarkus Elfring <elfring@users.sourceforge.net>
Sat, 24 Jan 2015 18:55:36 +0000 (19:55 +0100)
committerStéphane Graber <stgraber@ubuntu.com>
Sat, 24 Jan 2015 21:43:53 +0000 (16:43 -0500)
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: Markus Elfring <elfring@users.sourceforge.net>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
19 files changed:
src/lxc/attach.c
src/lxc/bdev.c
src/lxc/cgfs.c
src/lxc/cgmanager.c
src/lxc/conf.c
src/lxc/confile.c
src/lxc/lsm/apparmor.c
src/lxc/lxc_autostart.c
src/lxc/lxc_snapshot.c
src/lxc/lxc_start.c
src/lxc/lxc_top.c
src/lxc/lxc_user_nic.c
src/lxc/lxc_usernsexec.c
src/lxc/lxccontainer.c
src/lxc/lxclock.c
src/lxc/network.c
src/lxc/parse.c
src/lxc/seccomp.c
src/python-lxc/lxc.c

index 0486cec18e7733f753206a032535b4c58bee9874..497e0d27da95c8f8085f0a405c3641e6dded5991 100644 (file)
@@ -108,8 +108,7 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
                }
        }
 
-       if (line)
-               free(line);
+       free(line);
        fclose(proc_file);
 
        if (!found) {
@@ -129,8 +128,7 @@ out_error:
 
 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)
                lxc_container_put(ctx->container);
        free(ctx);
@@ -444,8 +442,7 @@ static char *lxc_attach_getpwshell(uid_t uid)
                        }
                        if (!token)
                                continue;
-                       if (result)
-                               free(result);
+                       free(result);
                        result = strdup(token);
 
                        /* sanity check that there are no fields after that */
index c0cc050d13e65e9da7aea08eec78ba3155f8fe6a..6cfba3a8738e6a93be40e067688721daedf29ed0 100644 (file)
@@ -1439,8 +1439,7 @@ out:
                close(fddst);
        if (fd != -1)
                close(fd);
-       if (newfull)
-               free(newfull);
+       free(newfull);
        return ret;
 }
 
@@ -3119,12 +3118,9 @@ static const size_t numbdevs = sizeof(bdevs) / sizeof(struct bdev_type);
 
 void bdev_put(struct bdev *bdev)
 {
-       if (bdev->mntopts)
-               free(bdev->mntopts);
-       if (bdev->src)
-               free(bdev->src);
-       if (bdev->dest)
-               free(bdev->dest);
+       free(bdev->mntopts);
+       free(bdev->src);
+       free(bdev->dest);
        free(bdev);
 }
 
index c9adacded652f1a41f0f2f3ef3b62f7c00b5a864..e63978abafe65e9e01703ed10ce0290911b8f6fd 100644 (file)
@@ -2187,8 +2187,7 @@ static bool do_init_cpuset_file(struct cgroup_mount_point *mp,
                SYSERROR("failed writing %s", childfile);
 
 out:
-       if (parentfile)
-               free(parentfile);
+       free(parentfile);
        free(childfile);
        return ok;
 }
@@ -2248,8 +2247,7 @@ static void cgfs_destroy(void *hdata)
 
        if (!d)
                return;
-       if (d->name)
-               free(d->name);
+       free(d->name);
        if (d->info)
                lxc_cgroup_process_info_free_and_remove(d->info);
        if (d->meta)
index c61d2de9b0e6239c920c73bdeb0132ad4fdc4752..0932d96ccd485b955f82a65aa67b69779c66d040 100644 (file)
@@ -577,8 +577,7 @@ static void cgm_destroy(void *hdata)
                cgm_remove_cgroup(slist[i], d->cgroup_path);
 
        free(d->name);
-       if (d->cgroup_path)
-               free(d->cgroup_path);
+       free(d->cgroup_path);
        free(d);
        cgm_dbus_disconnect();
 }
index 98ee63b787401527cc2d522d130b90295cf0175c..4acc3540002fe055f225a4ecde585f442b12c4c6 100644 (file)
@@ -2639,10 +2639,9 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
 
 out_delete:
        lxc_netdev_delete_by_name(veth1);
-       if (!netdev->priv.veth_attr.pair && veth1)
+       if (!netdev->priv.veth_attr.pair)
                free(veth1);
-       if(veth2)
-               free(veth2);
+       free(veth2);
        return -1;
 }
 
@@ -3156,8 +3155,7 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
                        break;
        }
 
-       if (buf)
-               free(buf);
+       free(buf);
        return ret;
 }
 
@@ -3599,8 +3597,7 @@ void remount_all_slave(void)
                }
        }
        fclose(f);
-       if (line)
-               free(line);
+       free(line);
 }
 
 void lxc_execute_bind_init(struct lxc_conf *conf)
@@ -3886,22 +3883,15 @@ static void lxc_remove_nic(struct lxc_list *it)
 
        lxc_list_del(it);
 
-       if (netdev->link)
-               free(netdev->link);
-       if (netdev->name)
-               free(netdev->name);
-       if (netdev->type == LXC_NET_VETH && netdev->priv.veth_attr.pair)
+       free(netdev->link);
+       free(netdev->name);
+       if (netdev->type == LXC_NET_VETH)
                free(netdev->priv.veth_attr.pair);
-       if (netdev->upscript)
-               free(netdev->upscript);
-       if (netdev->hwaddr)
-               free(netdev->hwaddr);
-       if (netdev->mtu)
-               free(netdev->mtu);
-       if (netdev->ipv4_gateway)
-               free(netdev->ipv4_gateway);
-       if (netdev->ipv6_gateway)
-               free(netdev->ipv6_gateway);
+       free(netdev->upscript);
+       free(netdev->hwaddr);
+       free(netdev->mtu);
+       free(netdev->ipv4_gateway);
+       free(netdev->ipv6_gateway);
        lxc_list_for_each_safe(it2, &netdev->ipv4, next) {
                lxc_list_del(it2);
                free(it2->elem);
@@ -3964,40 +3954,26 @@ int lxc_clear_nic(struct lxc_conf *c, const char *key)
                        free(it2);
                }
        } else if (strcmp(p1, ".link") == 0) {
-               if (netdev->link) {
-                       free(netdev->link);
-                       netdev->link = NULL;
-               }
+               free(netdev->link);
+               netdev->link = NULL;
        } else if (strcmp(p1, ".name") == 0) {
-               if (netdev->name) {
-                       free(netdev->name);
-                       netdev->name = NULL;
-               }
+               free(netdev->name);
+               netdev->name = NULL;
        } else if (strcmp(p1, ".script.up") == 0) {
-               if (netdev->upscript) {
-                       free(netdev->upscript);
-                       netdev->upscript = NULL;
-               }
+               free(netdev->upscript);
+               netdev->upscript = NULL;
        } else if (strcmp(p1, ".hwaddr") == 0) {
-               if (netdev->hwaddr) {
-                       free(netdev->hwaddr);
-                       netdev->hwaddr = NULL;
-               }
+               free(netdev->hwaddr);
+               netdev->hwaddr = NULL;
        } else if (strcmp(p1, ".mtu") == 0) {
-               if (netdev->mtu) {
-                       free(netdev->mtu);
-                       netdev->mtu = NULL;
-               }
+               free(netdev->mtu);
+               netdev->mtu = NULL;
        } else if (strcmp(p1, ".ipv4.gateway") == 0) {
-               if (netdev->ipv4_gateway) {
-                       free(netdev->ipv4_gateway);
-                       netdev->ipv4_gateway = NULL;
-               }
+               free(netdev->ipv4_gateway);
+               netdev->ipv4_gateway = NULL;
        } else if (strcmp(p1, ".ipv6.gateway") == 0) {
-               if (netdev->ipv6_gateway) {
-                       free(netdev->ipv6_gateway);
-                       netdev->ipv6_gateway = NULL;
-               }
+               free(netdev->ipv6_gateway);
+               netdev->ipv6_gateway = NULL;
        }
                else return -1;
 
@@ -4183,36 +4159,22 @@ void lxc_conf_free(struct lxc_conf *conf)
 {
        if (!conf)
                return;
-       if (conf->console.log_path)
-               free(conf->console.log_path);
-       if (conf->console.path)
-               free(conf->console.path);
-       if (conf->rootfs.mount)
-               free(conf->rootfs.mount);
-       if (conf->rootfs.options)
-               free(conf->rootfs.options);
-       if (conf->rootfs.path)
-               free(conf->rootfs.path);
-       if (conf->rootfs.pivot)
-               free(conf->rootfs.pivot);
-       if (conf->logfile)
-               free(conf->logfile);
-       if (conf->utsname)
-               free(conf->utsname);
-       if (conf->ttydir)
-               free(conf->ttydir);
-       if (conf->fstab)
-               free(conf->fstab);
-       if (conf->rcfile)
-               free(conf->rcfile);
-       if (conf->init_cmd)
-               free(conf->init_cmd);
+       free(conf->console.log_path);
+       free(conf->console.path);
+       free(conf->rootfs.mount);
+       free(conf->rootfs.options);
+       free(conf->rootfs.path);
+       free(conf->rootfs.pivot);
+       free(conf->logfile);
+       free(conf->utsname);
+       free(conf->ttydir);
+       free(conf->fstab);
+       free(conf->rcfile);
+       free(conf->init_cmd);
        free(conf->unexpanded_config);
        lxc_clear_config_network(conf);
-       if (conf->lsm_aa_profile)
-               free(conf->lsm_aa_profile);
-       if (conf->lsm_se_context)
-               free(conf->lsm_se_context);
+       free(conf->lsm_aa_profile);
+       free(conf->lsm_se_context);
        lxc_seccomp_free(conf);
        lxc_clear_config_caps(conf);
        lxc_clear_config_keepcaps(conf);
@@ -4486,8 +4448,7 @@ void suggest_default_idmap(void)
        }
        fclose(f);
 
-       if (line)
-               free(line);
+       free(line);
 
        if (!urange || !grange) {
                ERROR("You do not have subuids or subgids allocated");
index a21ee1a6bc6b5e74d2cd0918fae311472d4515a2..2bc8f5f13dad29f8d1306e0e45e2e0008d6a6096 100644 (file)
@@ -241,8 +241,7 @@ static int config_string_item(char **conf_item, const char *value)
        char *new_value;
 
        if (!value || strlen(value) == 0) {
-               if (*conf_item)
-                       free(*conf_item);
+               free(*conf_item);
                *conf_item = NULL;
                return 0;
        }
@@ -253,8 +252,7 @@ static int config_string_item(char **conf_item, const char *value)
                return -1;
        }
 
-       if (*conf_item)
-               free(*conf_item);
+       free(*conf_item);
        *conf_item = new_value;
        return 0;
 }
@@ -1120,7 +1118,7 @@ static int config_environment(const char *key, const char *value,
        return 0;
 
 freak_out:
-       if (list_item) free(list_item);
+       free(list_item);
 
        return -1;
 }
@@ -1331,15 +1329,12 @@ static int config_cgroup(const char *key, const char *value,
        return 0;
 
 out:
-       if (cglist)
-               free(cglist);
+       free(cglist);
 
        if (cgelem) {
-               if (cgelem->subsystem)
-                       free(cgelem->subsystem);
+               free(cgelem->subsystem);
 
-               if (cgelem->value)
-                       free(cgelem->value);
+               free(cgelem->value);
 
                free(cgelem);
        }
@@ -1399,8 +1394,7 @@ static int config_idmap(const char *key, const char *value, struct lxc_conf *lxc
        return 0;
 
 out:
-       if (idmaplist)
-               free(idmaplist);
+       free(idmaplist);
 
        if (idmap) {
                free(idmap);
@@ -1796,8 +1790,7 @@ static int config_utsname(const char *key, const char *value,
        }
 
        strcpy(utsname->nodename, value);
-       if (lxc_conf->utsname)
-               free(lxc_conf->utsname);
+       free(lxc_conf->utsname);
        lxc_conf->utsname = utsname;
 
        return 0;
index 460dc6bbec0025ca71205c6139e2049dfd78a89f..d8122045dde68f7f2f8abd7dcfa5d92eac37c517 100644 (file)
@@ -95,8 +95,7 @@ again:
        f = fopen(path, "r");
        if (!f) {
                SYSERROR("opening %s", path);
-               if (buf)
-                       free(buf);
+               free(buf);
                return NULL;
        }
        sz += 1024;
@@ -133,8 +132,7 @@ static int apparmor_am_unconfined(void)
        int ret = 0;
        if (!p || strcmp(p, "unconfined") == 0)
                ret = 1;
-       if (p)
-               free(p);
+       free(p);
        return ret;
 }
 
index db25b4871fd000688aad020f78133f217a50f7ff..1959e61bc19517e3d1e80baefa3f2527170c0e1d 100644 (file)
@@ -500,8 +500,7 @@ int main(int argc, char *argv[])
                }
        }
 
-       if ( c_groups_lists )
-               free(c_groups_lists);
+       free(c_groups_lists);
 
        if ( cmd_groups_list ) {
                toss_list( cmd_groups_list );
index 58288e0de34c486d94f102bb6f7ca311be0f7050..a03c0c09bd97458c46c9f646168ac50d375c5d27 100644 (file)
@@ -72,8 +72,7 @@ static void print_file(char *path)
        while (getline(&line, &sz, f) != -1) {
                printf("%s", line);
        }
-       if (line)
-               free(line);
+       free(line);
        fclose(f);
 }
 
index 006ffc42a959dc8e0b645e694a5db4c64b178874..b9d1add19477daa9d544f6b46d7204f6e35cc297 100644 (file)
@@ -89,8 +89,7 @@ static int ensure_path(char **confpath, const char *path)
        err = 0;
 
 err:
-       if (fullpath)
-               free(fullpath);
+       free(fullpath);
        return err;
 }
 
index aec4b52f6a7e5d9bc7e684d79fa0c7e8503d0089..88404055eb8088442c12f3b8d0a2f578fcf22689 100644 (file)
@@ -390,10 +390,8 @@ static void ct_free(void)
                        lxc_container_put(ct[i].c);
                        ct[i].c = NULL;
                }
-               if (ct[i].stats) {
-                       free(ct[i].stats);
-                       ct[i].stats = NULL;
-               }
+               free(ct[i].stats);
+               ct[i].stats = NULL;
        }
 }
 
index b183c3908fb57f8f20a8f3d6e572e27dc2d3e420..6622db0dbd3425ee83b0d7ce199c8f9011269101 100644 (file)
@@ -133,8 +133,7 @@ static int get_alloted(char *me, char *intype, char *link)
                return n;
        }
        fclose(fin);
-       if (line)
-               free(line);
+       free(line);
        return -1;
 }
 
index 3c1fec5728e1ace0662123253a3d9e38ba9d6751..fbaa083d1a586ccb5583b5f084a5359c21af194c 100644 (file)
@@ -244,8 +244,7 @@ static int read_default_map(char *fnam, int which, char *username)
                break;
        }
 
-       if (line)
-               free(line);
+       free(line);
        fclose(fin);
        return 0;
 }
index 7ed87170868766728930f51eeb1e68f7a6319849..ee73b64c9b0e9d6db544c987a2fddf783574be8b 100644 (file)
@@ -217,14 +217,10 @@ static void lxc_container_free(struct lxc_container *c)
        if (!c)
                return;
 
-       if (c->configfile) {
-               free(c->configfile);
-               c->configfile = NULL;
-       }
-       if (c->error_string) {
-               free(c->error_string);
-               c->error_string = NULL;
-       }
+       free(c->configfile);
+       c->configfile = NULL;
+       free(c->error_string);
+       c->error_string = NULL;
        if (c->slock) {
                lxc_putlock(c->slock);
                c->slock = NULL;
@@ -233,18 +229,14 @@ static void lxc_container_free(struct lxc_container *c)
                lxc_putlock(c->privlock);
                c->privlock = NULL;
        }
-       if (c->name) {
-               free(c->name);
-               c->name = NULL;
-       }
+       free(c->name);
+       c->name = NULL;
        if (c->lxc_conf) {
                lxc_conf_free(c->lxc_conf);
                c->lxc_conf = NULL;
        }
-       if (c->config_path) {
-               free(c->config_path);
-               c->config_path = NULL;
-       }
+       free(c->config_path);
+       c->config_path = NULL;
 
        free(c);
 }
@@ -961,8 +953,7 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
                                exit(1);
                        }
                } else { // TODO come up with a better way here!
-                       if (bdev->dest)
-                               free(bdev->dest);
+                       free(bdev->dest);
                        bdev->dest = strdup(bdev->src);
                }
 
@@ -1390,8 +1381,7 @@ out:
        if (!ret && c)
                container_destroy(c);
 free_tpath:
-       if (tpath)
-               free(tpath);
+       free(tpath);
        return ret;
 }
 
@@ -1964,8 +1954,8 @@ static void mod_all_rdeps(struct lxc_container *c, bool inc)
                lxc_container_put(p);
        }
 out:
-       if (lxcpath) free(lxcpath);
-       if (lxcname) free(lxcname);
+       free(lxcpath);
+       free(lxcname);
        fclose(f);
 }
 
@@ -2210,8 +2200,7 @@ static bool set_config_filename(struct lxc_container *c)
                return false;
        }
 
-       if (c->configfile)
-               free(c->configfile);
+       free(c->configfile);
        c->configfile = newpath;
 
        return true;
@@ -2250,8 +2239,7 @@ static bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
                oldpath = NULL;
        }
 err:
-       if (oldpath)
-               free(oldpath);
+       free(oldpath);
        container_mem_unlock(c);
        return b;
 }
@@ -2598,8 +2586,7 @@ static int clone_update_rootfs(struct clone_update_data *data)
                        return -1;
                }
        } else { // TODO come up with a better way
-               if (bdev->dest)
-                       free(bdev->dest);
+               free(bdev->dest);
                bdev->dest = strdup(bdev->src);
        }
 
@@ -3038,14 +3025,10 @@ static int lxcapi_snapshot(struct lxc_container *c, const char *commentfile)
 
 static void lxcsnap_free(struct lxc_snapshot *s)
 {
-       if (s->name)
-               free(s->name);
-       if (s->comment_pathname)
-               free(s->comment_pathname);
-       if (s->timestamp)
-               free(s->timestamp);
-       if (s->lxcpath)
-               free(s->lxcpath);
+       free(s->name);
+       free(s->comment_pathname);
+       free(s->timestamp);
+       free(s->lxcpath);
 }
 
 static char *get_snapcomment_path(char* snappath, char *name)
@@ -3811,10 +3794,8 @@ static bool dump_net_info(struct lxc_container *c, char *directory)
 
                has_error = false;
 out:
-               if (veth)
-                       free(veth);
-               if (bridge)
-                       free(bridge);
+               free(veth);
+               free(bridge);
                if (has_error)
                        return false;
        }
@@ -4388,8 +4369,7 @@ free_ct_name:
        }
 
 out:
-       if (line)
-               free(line);
+       free(line);
 
        fclose(f);
        return ret;
@@ -4461,16 +4441,13 @@ free_ct_list:
        for (i = 0; i < ct_list_cnt; i++) {
                lxc_container_put(ct_list[i]);
        }
-       if (ct_list)
-               free(ct_list);
+       free(ct_list);
 
 free_active_name:
        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:
        for (i = 0; i < ct_cnt; i++) {
index ea9be3c54848d38368cdd2160409ca4e0a65690d..8e92104f3f969d424e4ddcf4397fa46eada9bfe3 100644 (file)
@@ -323,10 +323,8 @@ void lxc_putlock(struct lxc_lock *l)
                        close(l->u.f.fd);
                        l->u.f.fd = -1;
                }
-               if (l->u.f.fname) {
-                       free(l->u.f.fname);
-                       l->u.f.fname = NULL;
-               }
+               free(l->u.f.fname);
+               l->u.f.fname = NULL;
                break;
        }
        free(l);
index ea536161c7bb308fad59b613c3693e5484e78ad7..a6740f56a549e3717a89726eb7098cede1c90f00 100644 (file)
@@ -176,8 +176,7 @@ static char * is_wlan(const char *ifname)
        return physname;
 
 bad:
-       if (physname)
-               free(physname);
+       free(physname);
        return NULL;
 }
 
index 604c47b7083a275cc33b5a550f1433eaa064bec6..5852ca403dc08882f63556b779f0458dbf7dc57f 100644 (file)
@@ -59,8 +59,7 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
                }
        }
 
-       if (line)
-               free(line);
+       free(line);
        fclose(f);
        return err;
 }
index 825d8a1a4156329a465dd96b7426316df3cb40b3..3ba6c9a6030343e6ccdec0e90b4707044db5f08c 100644 (file)
@@ -564,10 +564,8 @@ int lxc_seccomp_load(struct lxc_conf *conf)
 }
 
 void lxc_seccomp_free(struct lxc_conf *conf) {
-       if (conf->seccomp) {
-               free(conf->seccomp);
-               conf->seccomp = NULL;
-       }
+       free(conf->seccomp);
+       conf->seccomp = NULL;
 #if HAVE_SCMP_FILTER_CTX
        if (conf->seccomp_ctx) {
                seccomp_release(conf->seccomp_ctx);
index 23a8b59dc9554ed38b74a47ae230228d069d0930..a552d81d24cb1c6f29a1c742230959cf13ab8681 100644 (file)
@@ -237,8 +237,7 @@ void lxc_attach_free_options(lxc_attach_options_t *options)
     int i;
     if (!options)
         return;
-    if (options->initial_cwd)
-        free(options->initial_cwd);
+    free(options->initial_cwd);
     if (options->extra_env_vars) {
         for (i = 0; options->extra_env_vars[i]; i++)
             free(options->extra_env_vars[i]);