Commit 48c2bb0b authored by Jay Kamat's avatar Jay Kamat Committed by Shuah Khan (Samsung OSG)

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: 84092dbc ("selftests: cgroup: add memory controller self-tests")
Signed-off-by: default avatarJay Kamat <jgkamat@fb.com>
Acked-by: default avatarRoman Gushchin <guro@fb.com>
Signed-off-by: default avatarShuah Khan (Samsung OSG) <shuah@kernel.org>
parent 7035c568
...@@ -89,17 +89,28 @@ int cg_read(const char *cgroup, const char *control, char *buf, size_t len) ...@@ -89,17 +89,28 @@ int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
int cg_read_strcmp(const char *cgroup, const char *control, int cg_read_strcmp(const char *cgroup, const char *control,
const char *expected) const char *expected)
{ {
size_t size = strlen(expected) + 1; size_t size;
char *buf; char *buf;
int ret;
/* Handle the case of comparing against empty string */
if (!expected)
size = 32;
else
size = strlen(expected) + 1;
buf = malloc(size); buf = malloc(size);
if (!buf) if (!buf)
return -1; return -1;
if (cg_read(cgroup, control, buf, size)) if (cg_read(cgroup, control, buf, size)) {
free(buf);
return -1; return -1;
}
return strcmp(expected, buf); ret = strcmp(expected, buf);
free(buf);
return ret;
} }
int cg_read_strstr(const char *cgroup, const char *control, const char *needle) int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
......
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