Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Fix cg_read_strcmp()

Fix a couple issues with cg_read_strcmp(), to improve correctness of
cgroup tests
- Fix cg_read_strcmp() always returning 0 for empty "needle" strings.
Previously, this function read to a size = 1 buffer when comparing
against empty strings, which would lead to cg_read_strcmp() comparing
two empty strings.
- Fix a memory leak in cg_read_strcmp()

Fixes: 84092dbcf901 ("selftests: cgroup: add memory controller self-tests")

Signed-off-by: Jay Kamat <jgkamat@fb.com>
Acked-by: Roman Gushchin <guro@fb.com>
Signed-off-by: Shuah Khan (Samsung OSG) <shuah@kernel.org>

authored by

Jay Kamat and committed by
Shuah Khan (Samsung OSG)
48c2bb0b 7035c568

+14 -3
+14 -3
tools/testing/selftests/cgroup/cgroup_util.c
··· 89 89 int cg_read_strcmp(const char *cgroup, const char *control, 90 90 const char *expected) 91 91 { 92 - size_t size = strlen(expected) + 1; 92 + size_t size; 93 93 char *buf; 94 + int ret; 95 + 96 + /* Handle the case of comparing against empty string */ 97 + if (!expected) 98 + size = 32; 99 + else 100 + size = strlen(expected) + 1; 94 101 95 102 buf = malloc(size); 96 103 if (!buf) 97 104 return -1; 98 105 99 - if (cg_read(cgroup, control, buf, size)) 106 + if (cg_read(cgroup, control, buf, size)) { 107 + free(buf); 100 108 return -1; 109 + } 101 110 102 - return strcmp(expected, buf); 111 + ret = strcmp(expected, buf); 112 + free(buf); 113 + return ret; 103 114 } 104 115 105 116 int cg_read_strstr(const char *cgroup, const char *control, const char *needle)