[PATCH master 1/3] test: self: ramfs: fix null pointer dereference on error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



expect_success is meant for return values of functions that return
negative error codes and as such compares the first argument to be
zero or greater, which means PTR_ERR_OR_ZERO will treat NULL pointers as
valid all the same leading to NULL pointer dereferences should the test
fail instead of a descriptive error message. Fix this by translating
NULL pointer to -errno or -EFAULT if errno is unset.

Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx>
---
 test/self/ramfs.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/test/self/ramfs.c b/test/self/ramfs.c
index bce396dc171e..1bc2b3b068f0 100644
--- a/test/self/ramfs.c
+++ b/test/self/ramfs.c
@@ -27,7 +27,16 @@ BSELFTEST_GLOBALS();
 	__cond; \
 })
 
+static inline int ptr_to_err(const void *ptr)
+{
+	if (ptr)
+		return PTR_ERR_OR_ZERO(ptr);
+
+	return -errno ?: -EFAULT;
+}
+
 #define expect_success(ret, ...) __expect((ret), (ret) >= 0, __VA_ARGS__)
+#define expect_ptrok(ptr, ...) expect_success(ptr_to_err(ptr), __VA_ARGS__)
 #define expect_fail(ret, ...) __expect((ret), (ret) < 0, __VA_ARGS__)
 
 static inline int get_file_count(int i)
@@ -101,7 +110,7 @@ static void test_ramfs(void)
 		}
 
 		dir = opendir(dname);
-		if (!expect_success(PTR_ERR_OR_ZERO(dir), "opening parent directory"))
+		if (!expect_ptrok(dir, "opening parent directory"))
 			continue;
 
 		j = 0;
@@ -120,11 +129,11 @@ static void test_ramfs(void)
 	}
 
 	oldpwd = pushd(dname);
-	if (!expect_success(oldpwd != NULL ? 0 : -EINVAL, "pushd()"))
+	if (!expect_ptrok(oldpwd, "pushd()"))
 		goto out;;
 
 	dir = opendir(".");
-	if (!expect_success(PTR_ERR_OR_ZERO(dir), "opening parent directory"))
+	if (!expect_ptrok(dir, "opening parent directory"))
 		goto out;
 
 	i = 1;
@@ -183,7 +192,7 @@ static void test_ramfs(void)
 		expect_success(ret, "write_file()");
 
 		buf = read_file("test/a/b/c/d/e/f/g/h/i/j/k/l/FILE", NULL);
-		if (expect_success(PTR_ERR_OR_ZERO(buf), "read_file()")) {
+		if (expect_ptrok(buf, "read_file()")) {
 			expect_success(memcmp(buf, ARRAY_AND_SIZE(hello)),
 				       "read_file() content");
 		}
-- 
2.39.2





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux