mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/3] test: self: ramfs: fix null pointer dereference on error
@ 2024-01-03 10:13 Ahmad Fatoum
  2024-01-03 10:13 ` [PATCH master 2/3] test: self: regulator: depend on REGULATOR_FIXED for test Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-01-03 10:13 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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@pengutronix.de>
---
 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




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-03 14:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-03 10:13 [PATCH master 1/3] test: self: ramfs: fix null pointer dereference on error Ahmad Fatoum
2024-01-03 10:13 ` [PATCH master 2/3] test: self: regulator: depend on REGULATOR_FIXED for test Ahmad Fatoum
2024-01-03 10:13 ` [PATCH master 3/3] test: self: mmu: fix message on skipping tests Ahmad Fatoum
2024-01-03 14:10 ` [PATCH master 1/3] test: self: ramfs: fix null pointer dereference on error Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox