Skip to contentSkip to conversation

selftests: cgroup: Fix the invalid check in proc_read_text()

PATCH In review Wang Yufen 2 participants 2 messages Dec 7, 2022 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Wang Yufen

author
#0 lore raw

PATCH 1/1 selftests: cgroup: Fix the invalid check in proc_read_text()

Unsigned variable cannot be lesser than zero, So the return check of read_text() is invalid. To fix, add a new ssize_t variable read for read_text() return value check.

Fixes 6c26df84e1f2 ("selftests: cgroup: return -errno from cg_read()/cg_write() on failure")
---
 tools/testing/selftests/cgroup/cgroup_util.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/cgroup/cgroup_util.c +3 -2
index 4c52cc6..19137c0 100644
@@ -555,6 +555,7 @@ int proc_mount_contains(const char *option)
 ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size)
 {
 	char path[PATH_MAX];
+	ssize_t read;
 
 	if (!pid)
 		snprintf(path, sizeof(path), "/proc/%s/%s",
@@ -562,8 +563,8 @@ ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t
 	else
 		snprintf(path, sizeof(path), "/proc/%d/%s", pid, item);
 
-	size = read_text(path, buf, size);
-	return size < 0 ? -1 : size;
+	read = read_text(path, buf, size);
+	return read < 0 ? -1 : read;
 }
 
 int proc_read_strstr(int pid, bool thread, const char *item, const char *needle)
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 1670401989-8208-1-git-send-email-wangyufen-hv44wF8Li93QT0dZR+AlfA@public.gmane.org

#2 lore raw
Quoted 5 lines · Wang Yufen

Unsigned variable cannot be lesser than zero, So the return check of read_text() is invalid. To fix, add a new ssize_t variable read for read_text() return value check.

Fixes 6c26df84e1f2 ("selftests: cgroup: return -errno from cg_read()/cg_write() on failure")

Thanks for sending this, but a fix was already sent recently (by YueHaibing):
https://lore.kernel.org/lkml/20221105110611.28920-1-yuehaibing-hv44wF8Li93QT0dZR+AlfA@public.gmane.org/

, and it currently lives in the mm-nomm-stable branch in Andrew's tree AFAICT as commit e410f7aa641d ("selftests: cgroup: fix unsigned comparison with less than zero").

Quoted 31 lines
---
 tools/testing/selftests/cgroup/cgroup_util.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/cgroup/cgroup_util.c +3 -3
index 4c52cc6..19137c0 100644
@@ -555,6 +555,7 @@ int proc_mount_contains(const char *option)
 ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size)
 {
        char path[PATH_MAX];
+       ssize_t read;
 
        if (!pid)
                snprintf(path, sizeof(path), "/proc/%s/%s",
@@ -562,8 +563,8 @@ ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t
        else
                snprintf(path, sizeof(path), "/proc/%d/%s", pid, item);
 
-       size = read_text(path, buf, size);
-       return size < 0 ? -1 : size;
+       read = read_text(path, buf, size);
+       return read < 0 ? -1 : read;
 }
 
 int proc_read_strstr(int pid, bool thread, const char *item, const char *needle)
--

1.8.3.1