From 966e9f1fc82662e4f9c330200490c121dacbd7c8 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Sun, 3 Sep 2017 20:37:21 +0200 Subject: [PATCH] network: remove allocation from lxc_mkifname() lxc_mkifname() really doesn't need to allocate any memory. Signed-off-by: Christian Brauner --- src/lxc/criu.c | 8 ++------ src/lxc/lxc_user_nic.c | 13 +++++-------- src/lxc/network.c | 42 +++++++++++++++++------------------------- src/lxc/network.h | 2 +- 4 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/lxc/criu.c b/src/lxc/criu.c index 2f444ec44..9305b52a0 100644 --- a/src/lxc/criu.c +++ b/src/lxc/criu.c @@ -767,14 +767,10 @@ static bool restore_net_info(struct lxc_container *c) if (netdev->priv.veth_attr.pair[0] == '\0' && netdev->priv.veth_attr.veth1[0] == '\0') { - char *tmp; - - tmp = lxc_mkifname(template); - if (!tmp) + if (!lxc_mkifname(template)) goto out_unlock; - strcpy(netdev->priv.veth_attr.veth1, tmp); - free(tmp); + strcpy(netdev->priv.veth_attr.veth1, template); } } diff --git a/src/lxc/lxc_user_nic.c b/src/lxc/lxc_user_nic.c index 6920c5462..9026bc425 100644 --- a/src/lxc/lxc_user_nic.c +++ b/src/lxc/lxc_user_nic.c @@ -566,23 +566,20 @@ out_del: static char *get_new_nicname(char *br, int pid, char **cnic) { int ret; - char *nicname; - char template[IFNAMSIZ]; + char nicname[IFNAMSIZ]; - ret = snprintf(template, sizeof(template), "vethXXXXXX"); - if (ret < 0 || (size_t)ret >= sizeof(template)) + ret = snprintf(nicname, sizeof(nicname), "vethXXXXXX"); + if (ret < 0 || (size_t)ret >= sizeof(nicname)) return NULL; - nicname = lxc_mkifname(template); - if (!nicname) + if (!lxc_mkifname(nicname)) return NULL; if (!create_nic(nicname, br, pid, cnic)) { - free(nicname); return NULL; } - return nicname; + return strdup(nicname); } struct entry_line { diff --git a/src/lxc/network.c b/src/lxc/network.c index 1126cdec3..d2247273c 100644 --- a/src/lxc/network.c +++ b/src/lxc/network.c @@ -218,9 +218,6 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd out_delete: if (netdev->ifindex != 0) lxc_netdev_delete_by_name(veth1); - if (netdev->priv.veth_attr.pair != veth1) - free(veth1); - free(veth2); return -1; } @@ -247,29 +244,29 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n if (err) { ERROR("Failed to create macvlan interface \"%s\" on \"%s\": %s", peer, netdev->link, strerror(-err)); - goto out; + goto on_error; } netdev->ifindex = if_nametoindex(peer); if (!netdev->ifindex) { ERROR("Failed to retrieve ifindex for \"%s\"", peer); - goto out; + goto on_error; } if (netdev->upscript) { err = run_script(handler->name, "net", netdev->upscript, "up", "macvlan", netdev->link, (char*) NULL); if (err) - goto out; + goto on_error; } DEBUG("Instantiated macvlan \"%s\" with ifindex is %d and mode %d", peer, netdev->ifindex, netdev->priv.macvlan_attr.mode); return 0; -out: + +on_error: lxc_netdev_delete_by_name(peer); - free(peer); return -1; } @@ -1918,14 +1915,14 @@ const char *lxc_net_type_to_str(int type) static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -char *lxc_mkifname(const char *template) +char *lxc_mkifname(char *template) { - int ifexists = 0; - size_t i = 0; - char *name = NULL; unsigned int seed; FILE *urandom; struct ifaddrs *ifa, *ifaddr; + char name[IFNAMSIZ]; + bool exists = false; + size_t i = 0; if (strlen(template) >= IFNAMSIZ) return NULL; @@ -1949,17 +1946,14 @@ char *lxc_mkifname(const char *template) /* Generate random names until we find one that doesn't exist. */ while (true) { - ifexists = 0; - name = strdup(template); - - if (name == NULL) - return NULL; + name[0] = '\0'; + strcpy(name, template); + exists = false; for (i = 0; i < strlen(name); i++) { if (name[i] == 'X') { #ifdef HAVE_RAND_R - name[i] = padchar[rand_r(&seed) % - (strlen(padchar) - 1)]; + name[i] = padchar[rand_r(&seed) % (strlen(padchar) - 1)]; #else name[i] = padchar[rand() % (strlen(padchar) - 1)]; #endif @@ -1967,20 +1961,18 @@ char *lxc_mkifname(const char *template) } for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (strcmp(ifa->ifa_name, name) == 0) { - ifexists = 1; + if (!strcmp(ifa->ifa_name, name)) { + exists = true; break; } } - if (ifexists == 0) + if (!exists) break; - - free(name); } freeifaddrs(ifaddr); - return name; + return strcpy(template, name); } int setup_private_host_hw_addr(char *veth1) diff --git a/src/lxc/network.h b/src/lxc/network.h index fe127734f..833bfe6f3 100644 --- a/src/lxc/network.h +++ b/src/lxc/network.h @@ -256,7 +256,7 @@ extern int lxc_neigh_proxy_off(const char *name, int family); /* Generate a new unique network interface name. * Allocated memory must be freed by caller. */ -extern char *lxc_mkifname(const char *template); +extern char *lxc_mkifname(char *template); extern const char *lxc_net_type_to_str(int type); extern int setup_private_host_hw_addr(char *veth1);