Commit af7df1c9 authored by Ivan Orlov's avatar Ivan Orlov Committed by Andrew Morton

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).

Link: https://lkml.kernel.org/r/20230226131634.34366-1-ivan.orlov0322@gmail.comSigned-off-by: default avatarIvan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: default avatarMuchun Song <songmuchun@bytedance.com>
Acked-by: default avatarShakeel Butt <shakeelb@google.com>
Acked-by: default avatarRoman Gushchin <roman.gushchin@linux.dev>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Zefan Li <lizefan.x@bytedance.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent bdeb9188
...@@ -98,6 +98,11 @@ static int alloc_anon_50M_check(const char *cgroup, void *arg) ...@@ -98,6 +98,11 @@ static int alloc_anon_50M_check(const char *cgroup, void *arg)
int ret = -1; int ret = -1;
buf = malloc(size); buf = malloc(size);
if (buf == NULL) {
fprintf(stderr, "malloc() failed\n");
return -1;
}
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE) for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
*ptr = 0; *ptr = 0;
...@@ -211,6 +216,11 @@ static int alloc_anon_noexit(const char *cgroup, void *arg) ...@@ -211,6 +216,11 @@ static int alloc_anon_noexit(const char *cgroup, void *arg)
char *buf, *ptr; char *buf, *ptr;
buf = malloc(size); buf = malloc(size);
if (buf == NULL) {
fprintf(stderr, "malloc() failed\n");
return -1;
}
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE) for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
*ptr = 0; *ptr = 0;
...@@ -778,6 +788,11 @@ static int alloc_anon_50M_check_swap(const char *cgroup, void *arg) ...@@ -778,6 +788,11 @@ static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)
int ret = -1; int ret = -1;
buf = malloc(size); buf = malloc(size);
if (buf == NULL) {
fprintf(stderr, "malloc() failed\n");
return -1;
}
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE) for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
*ptr = 0; *ptr = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment