[PATCH master 1/3] test: self: ramfs: fix null pointer dereference on error
Sascha Hauer
sha at pengutronix.de
Wed Jan 3 06:10:01 PST 2024
On Wed, Jan 03, 2024 at 11:13:42AM +0100, Ahmad Fatoum wrote:
> 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 at pengutronix.de>
> ---
> test/self/ramfs.c | 17 +++++++++++++----
> 1 file changed, 13 insertions(+), 4 deletions(-)
Applied, thanks
Sascha
>
> 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
>
>
>
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
More information about the barebox
mailing list