[lxc-devel] [go-lxc/v2] Fix some cgo issues

stgraber on Github lxc-bot at linuxcontainers.org
Wed Sep 19 14:11:00 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 303 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180919/90da5546/attachment.bin>
-------------- next part --------------
From 29a9498cda3e20ab20c88d7fcb9d6616633a4a90 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 18 Sep 2018 14:48:38 +0200
Subject: [PATCH 1/2] Don't mask error return values from liblxc
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 container.go  |  4 ++--
 lxc-binding.c | 16 +++++++++-------
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/container.go b/container.go
index dfa85a3..58f1d7f 100644
--- a/container.go
+++ b/container.go
@@ -1168,7 +1168,7 @@ func (c *Container) ConsoleFd(ttynum int) (int, error) {
 
 	ret := int(C.go_lxc_console_getfd(c.container, C.int(ttynum)))
 	if ret < 0 {
-		return -1, ErrAttachFailed
+		return ret, ErrAttachFailed
 	}
 	return ret, nil
 }
@@ -1359,7 +1359,7 @@ func (c *Container) RunCommandNoWait(args []string, options AttachOptions) (int,
 	))
 
 	if ret < 0 {
-		return -1, ErrAttachFailed
+		return ret, ErrAttachFailed
 	}
 
 	return int(attachedPid), nil
diff --git a/lxc-binding.c b/lxc-binding.c
index e054ed4..eb7892c 100644
--- a/lxc-binding.c
+++ b/lxc-binding.c
@@ -172,10 +172,12 @@ bool go_lxc_clone(struct lxc_container *c, const char *newname, const char *lxcp
 
 int go_lxc_console_getfd(struct lxc_container *c, int ttynum) {
 	int masterfd;
+	int ret = 0;
+
+	ret = c->console_getfd(c, &ttynum, &masterfd);
+	if (ret < 0)
+		return ret;
 
-	if (c->console_getfd(c, &ttynum, &masterfd) < 0) {
-		return -1;
-	}
 	return masterfd;
 }
 
@@ -251,7 +253,7 @@ int go_lxc_attach_no_wait(struct lxc_container *c,
 
 	ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, attached_pid);
 	if (ret < 0)
-		return -1;
+		return ret;
 
 	return 0;
 }
@@ -301,16 +303,16 @@ int go_lxc_attach(struct lxc_container *c,
 
 	ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
 	if (ret < 0)
-		return -1;
+		return ret;
 
 	ret = wait_for_pid_status(pid);
 	if (ret < 0)
-		return -1;
+		return ret;
 
 	if (WIFEXITED(ret))
 		return WEXITSTATUS(ret);
 
-	return -1;
+	return ret;
 }
 
 int go_lxc_attach_run_wait(struct lxc_container *c,

From 8c6482e40ed298d6aa4857f6c1a0c064f9180dff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 19 Sep 2018 16:08:32 +0200
Subject: [PATCH 2/2] Handle malloc failures
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxc-binding.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/lxc-binding.c b/lxc-binding.c
index eb7892c..7998271 100644
--- a/lxc-binding.c
+++ b/lxc-binding.c
@@ -92,12 +92,20 @@ bool go_lxc_wait(struct lxc_container *c, const char *state, int timeout) {
 }
 
 char* go_lxc_get_config_item(struct lxc_container *c, const char *key) {
+	char *value = NULL;
+
 	int len = c->get_config_item(c, key, NULL, 0);
 	if (len <= 0) {
 		return NULL;
 	}
 
-	char* value = (char*)malloc(sizeof(char)*len + 1);
+again:
+	value = (char*)malloc(sizeof(char)*len + 1);
+
+	if (value == NULL) {
+		goto again;
+	}
+
 	if (c->get_config_item(c, key, value, len + 1) != len) {
 		return NULL;
 	}
@@ -121,12 +129,20 @@ char* go_lxc_get_running_config_item(struct lxc_container *c, const char *key) {
 }
 
 char* go_lxc_get_keys(struct lxc_container *c, const char *key) {
+	char *value = NULL;
+
 	int len = c->get_keys(c, key, NULL, 0);
 	if (len <= 0) {
 		return NULL;
 	}
 
-	char* value = (char*)malloc(sizeof(char)*len + 1);
+again:
+	value = (char*)malloc(sizeof(char)*len + 1);
+
+	if (value == NULL) {
+		goto again;
+	}
+
 	if (c->get_keys(c, key, value, len + 1) != len) {
 		return NULL;
 	}
@@ -134,12 +150,20 @@ char* go_lxc_get_keys(struct lxc_container *c, const char *key) {
 }
 
 char* go_lxc_get_cgroup_item(struct lxc_container *c, const char *key) {
+	char *value = NULL;
+
 	int len = c->get_cgroup_item(c, key, NULL, 0);
 	if (len <= 0) {
 		return NULL;
 	}
 
-	char* value = (char*)malloc(sizeof(char)*len + 1);
+again:
+	value = (char*)malloc(sizeof(char)*len + 1);
+
+	if (value == NULL) {
+		goto again;
+	}
+
 	if (c->get_cgroup_item(c, key, value, len + 1) != len) {
 		return NULL;
 	}


More information about the lxc-devel mailing list