[PATCH] selftests: cgroup: Add 'malloc' failures checks in test_memcontrol

Ivan Orlov posted 1 patch 2 years, 6 months ago
tools/testing/selftests/cgroup/test_memcontrol.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
[PATCH] selftests: cgroup: Add 'malloc' failures checks in test_memcontrol
Posted by Ivan Orlov 2 years, 6 months ago
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).

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
---
 tools/testing/selftests/cgroup/test_memcontrol.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
index 1e616a8c6a9c..f4f7c0aef702 100644
--- a/tools/testing/selftests/cgroup/test_memcontrol.c
+++ b/tools/testing/selftests/cgroup/test_memcontrol.c
@@ -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;
 
-- 
2.34.1
Re: [PATCH] selftests: cgroup: Add 'malloc' failures checks in test_memcontrol
Posted by Roman Gushchin 2 years, 6 months ago
On Sun, Feb 26, 2023 at 04:16:33PM +0300, Ivan Orlov wrote:
> 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).
> 
> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>

Acked-by: Roman Gushchin <roman.gushchin@linux.dev>

Thanks!
Re: [PATCH] selftests: cgroup: Add 'malloc' failures checks in test_memcontrol
Posted by Shuah Khan 2 years, 5 months ago
On 2/27/23 11:15, Roman Gushchin wrote:
> On Sun, Feb 26, 2023 at 04:16:33PM +0300, Ivan Orlov wrote:
>> 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).
>>
>> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
> 
> Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
> 
> Thanks!

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

thanks,
-- Shuah
Re: [PATCH] selftests: cgroup: Add 'malloc' failures checks in test_memcontrol
Posted by Shakeel Butt 2 years, 6 months ago
On Sun, Feb 26, 2023 at 04:16:33PM +0300, Ivan Orlov wrote:
> 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).
> 
> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>

Acked-by: Shakeel Butt <shakeelb@google.com>
Re: [PATCH] selftests: cgroup: Add 'malloc' failures checks in test_memcontrol
Posted by Muchun Song 2 years, 6 months ago

> On Feb 26, 2023, at 21:16, Ivan Orlov <ivan.orlov0322@gmail.com> wrote:
> 
> 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).
> 
> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>

Reviewed-by: Muchun Song <songmuchun@bytedance.com>

Thanks.