From aeb038c9f17ba6a82bb881ff6e84f0ac4c980723 Mon Sep 17 00:00:00 2001
From: LiFeng <lifeng68@huawei.com>
Date: Sat, 12 Jun 2021 13:54:25 +0800
Subject: [PATCH] coredump: fix coredump when cgroup get return error

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/lxccontainer.c | 10 ++++++----
 src/lxc/string_utils.c |  4 ++++
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 01e6cbb69..2d581911a 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -5824,8 +5824,9 @@ static void metrics_get_blk_stats(struct lxc_container *c, struct cgroup_ops *cg
     buf = malloc(len + 1);
     (void)memset(buf, 0, len + 1);
     ret = cgroup_ops->get(cgroup_ops, item, buf, len, c->name, c->config_path);
-    if (ret != len) {
-        DEBUG("get cgroup item %s len %d has changed to %d", item, len, ret);
+    if (ret <= 0) {
+        DEBUG("unable to read cgroup item %s", item);
+        goto out;
     }
 
     lines = lxc_string_split_and_trim(buf, '\n');
@@ -5880,8 +5881,9 @@ static void metrics_get_io_stats_v2(struct lxc_container *c, struct cgroup_ops *
     buf = malloc(len + 1);
     (void)memset(buf, 0, len + 1);
     ret = cgroup_ops->get(cgroup_ops, item, buf, len, c->name, c->config_path);
-    if (ret != len) {
-        DEBUG("get cgroup item %s len %d change to %d", item, len, ret);
+    if (ret <= 0) {
+        DEBUG("unable to read cgroup item %s", item);
+        goto out;
     }
 
     lines = lxc_string_split_and_trim(buf, '\n');
diff --git a/src/lxc/string_utils.c b/src/lxc/string_utils.c
index 9118add02..d3c60897c 100644
--- a/src/lxc/string_utils.c
+++ b/src/lxc/string_utils.c
@@ -473,6 +473,10 @@ char **lxc_string_split_and_trim(const char *string, char _sep)
 		result_count++;
 	}
 
+	if (result == NULL) {
+		return calloc(1, sizeof(char *));
+	}
+
 	/* if we allocated too much, reduce it */
 	return realloc(result, (result_count + 1) * sizeof(char *));
 
-- 
2.25.1

