Skip to contentSkip to conversation

selftests: cgroup: Add 'malloc' failures checks in test_memcontrol

PATCH Applied Ivan Orlov 5 participants 5 messages Feb 26, 2023 – Mar 28, 2023

Subsystems (4)
lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

PATCH 1/1 selftests: cgroup: Add 'malloc' failures checks in test_memcontrol

There are several 'malloc' calls in test_memcontrol, which can be unsuccessful. This patch will add 'malloc' failures checking to give more details about test's fail reasons and avoid possible undefined behavior during the future null dereference (like the one in alloc_anon_50M_check_swap function).

---
 tools/testing/selftests/cgroup/test_memcontrol.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/selftests/cgroup/test_memcontrol.c +15 -0
index 1e616a8c6a9c..f4f7c0aef702 100644
@@ -98,6 +98,11 @@ static int alloc_anon_50M_check(const char *cgroup, void *arg)
 	int ret = -1;
 
 	buf = malloc(size);
+	if (buf == NULL) {
+		fprintf(stderr, "malloc() failed\n");
+		return -1;
+	}
+
 	for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
 		*ptr = 0;
 
@@ -211,6 +216,11 @@ static int alloc_anon_noexit(const char *cgroup, void *arg)
 	char *buf, *ptr;
 
 	buf = malloc(size);
+	if (buf == NULL) {
+		fprintf(stderr, "malloc() failed\n");
+		return -1;
+	}
+
 	for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
 		*ptr = 0;
 
@@ -778,6 +788,11 @@ static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)
 	int ret = -1;
 
 	buf = malloc(size);
+	if (buf == NULL) {
+		fprintf(stderr, "malloc() failed\n");
+		return -1;
+	}
+
 	for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
 		*ptr = 0;
 
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Ivan Orlov

There are several 'malloc' calls in test_memcontrol, which can be unsuccessful. This patch will add 'malloc' failures checking to give more details about test's fail reasons and avoid possible undefined behavior during the future null dereference (like the one in alloc_anon_50M_check_swap function).

Thanks.

Acked-by on v1 1/1
#2 lore raw
In reply to Ivan Orlov
Quoted 7 lines · Ivan Orlov

There are several 'malloc' calls in test_memcontrol, which can be unsuccessful. This patch will add 'malloc' failures checking to give more details about test's fail reasons and avoid possible undefined behavior during the future null dereference (like the one in alloc_anon_50M_check_swap function).

maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Ivan Orlov
Quoted 7 lines · Ivan Orlov

There are several 'malloc' calls in test_memcontrol, which can be unsuccessful. This patch will add 'malloc' failures checking to give more details about test's fail reasons and avoid possible undefined behavior during the future null dereference (like the one in alloc_anon_50M_check_swap function).

Thanks!

maintainer
#4 lore raw
Quoted 7 lines · Roman Gushchin
Quoted 6 lines · Ivan Orlov

There are several 'malloc' calls in test_memcontrol, which can be unsuccessful. This patch will add 'malloc' failures checking to give more details about test's fail reasons and avoid possible undefined behavior during the future null dereference (like the one in alloc_anon_50M_check_swap function).

Quoted 5 lines
Quoted 1 line

Thanks!

Thank you all. Applied to linux-kselftest next for Linux 6.4-rc1

thanks,
-- Shuah