* [PATCH] selftests/cgroup: Add NULL check after malloc in cgroup_util.c
@ 2026-05-11 6:08 Hongfu Li
2026-05-11 8:35 ` Tejun Heo
2026-05-11 12:04 ` Vishal Moola
0 siblings, 2 replies; 3+ messages in thread
From: Hongfu Li @ 2026-05-11 6:08 UTC (permalink / raw)
To: tj, hannes, mkoutny, shuah, jthoughton, seanjc, zhangguopeng
Cc: cgroups, linux-kselftest, linux-kernel, Hongfu Li
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>
---
tools/testing/selftests/cgroup/lib/cgroup_util.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/cgroup/lib/cgroup_util.c b/tools/testing/selftests/cgroup/lib/cgroup_util.c
index 6a7295347e90..0c5e6d4bbc3a 100644
--- a/tools/testing/selftests/cgroup/lib/cgroup_util.c
+++ b/tools/testing/selftests/cgroup/lib/cgroup_util.c
@@ -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;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/cgroup: Add NULL check after malloc in cgroup_util.c
2026-05-11 6:08 [PATCH] selftests/cgroup: Add NULL check after malloc in cgroup_util.c Hongfu Li
@ 2026-05-11 8:35 ` Tejun Heo
2026-05-11 12:04 ` Vishal Moola
1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2026-05-11 8:35 UTC (permalink / raw)
To: Hongfu Li
Cc: hannes, mkoutny, shuah, jthoughton, seanjc, zhangguopeng, cgroups,
linux-kselftest, linux-kernel
Hello,
Applied to cgroup/for-7.2.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/cgroup: Add NULL check after malloc in cgroup_util.c
2026-05-11 6:08 [PATCH] selftests/cgroup: Add NULL check after malloc in cgroup_util.c Hongfu Li
2026-05-11 8:35 ` Tejun Heo
@ 2026-05-11 12:04 ` Vishal Moola
1 sibling, 0 replies; 3+ messages in thread
From: Vishal Moola @ 2026-05-11 12:04 UTC (permalink / raw)
To: Hongfu Li
Cc: tj, hannes, mkoutny, shuah, jthoughton, seanjc, zhangguopeng,
cgroups, linux-kselftest, linux-kernel
On Mon, May 11, 2026 at 02:08:53PM +0800, Hongfu Li wrote:
> 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>
> ---
Reviewed-by: Vishal Moola <vishal.moola@gmail.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-11 12:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-11 6:08 [PATCH] selftests/cgroup: Add NULL check after malloc in cgroup_util.c Hongfu Li
2026-05-11 8:35 ` Tejun Heo
2026-05-11 12:04 ` Vishal Moola
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox