selftests/cgroup: Add NULL check after malloc in cgroup_util.c

Add NULL checks after malloc() in three helper functions to prevent
NULL pointer dereference on memory allocation failure.
- cg_name()
- cg_name_indexed()
- cg_control()

These functions allocate memory with malloc() but previously called
snprintf() unconditionally, which would trigger undefined behavior
if allocation fails.

Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
Signed-off-by: Tejun Heo <tj@kernel.org>
This commit is contained in:
Hongfu Li 2026-05-11 14:08:53 +08:00 committed by Tejun Heo
parent 7ea04cc4fe
commit c26849d321

View File

@ -59,7 +59,8 @@ char *cg_name(const char *root, const char *name)
size_t len = strlen(root) + strlen(name) + 2;
char *ret = malloc(len);
snprintf(ret, len, "%s/%s", root, name);
if (ret)
snprintf(ret, len, "%s/%s", root, name);
return ret;
}
@ -69,7 +70,8 @@ char *cg_name_indexed(const char *root, const char *name, int index)
size_t len = strlen(root) + strlen(name) + 10;
char *ret = malloc(len);
snprintf(ret, len, "%s/%s_%d", root, name, index);
if (ret)
snprintf(ret, len, "%s/%s_%d", root, name, index);
return ret;
}
@ -79,7 +81,8 @@ char *cg_control(const char *cgroup, const char *control)
size_t len = strlen(cgroup) + strlen(control) + 2;
char *ret = malloc(len);
snprintf(ret, len, "%s/%s", cgroup, control);
if (ret)
snprintf(ret, len, "%s/%s", cgroup, control);
return ret;
}