mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 1/3] test: self: ramfs: fix null pointer dereference on error
Date: Wed,  3 Jan 2024 11:13:42 +0100	[thread overview]
Message-ID: <20240103101344.2628970-1-a.fatoum@pengutronix.de> (raw)

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




             reply	other threads:[~2024-01-03 10:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-03 10:13 Ahmad Fatoum [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240103101344.2628970-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox