diff mbox series

[1/3] selftests: coredump: Properly initialize pointer

Message ID b595166b7ece013b371c2bac1098533f1ffa12d0.1743438749.git.namcao@linutronix.de (mailing list archive)
State New
Headers show
Series selftests: coredump: Some bug fixes | expand

Commit Message

Nam Cao March 31, 2025, 4:50 p.m. UTC
The buffer pointer "line" is not initialized. This pointer is passed to
getline().

It can still work if the stack is zero-initialized, because getline() can
work with a NULL pointer as buffer.

But this is obviously broken. This bug shows up while running the test on a
riscv64 machine.

Fix it by properly initializing the pointer.

Fixes: 15858da53542 ("selftests: coredump: Add stackdump test")
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
 tools/testing/selftests/coredump/stackdump_test.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

John Ogness April 1, 2025, 7:56 a.m. UTC | #1
On 2025-03-31, Nam Cao <namcao@linutronix.de> wrote:
> The buffer pointer "line" is not initialized. This pointer is passed to
> getline().

Ouch.

> It can still work if the stack is zero-initialized, because getline() can
> work with a NULL pointer as buffer.
>
> But this is obviously broken. This bug shows up while running the test on a
> riscv64 machine.
>
> Fix it by properly initializing the pointer.
>
> Fixes: 15858da53542 ("selftests: coredump: Add stackdump test")
> Signed-off-by: Nam Cao <namcao@linutronix.de>
> ---
>  tools/testing/selftests/coredump/stackdump_test.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/tools/testing/selftests/coredump/stackdump_test.c b/tools/testing/selftests/coredump/stackdump_test.c
> index 137b2364a082..1dc54e128586 100644
> --- a/tools/testing/selftests/coredump/stackdump_test.c
> +++ b/tools/testing/selftests/coredump/stackdump_test.c
> @@ -100,6 +100,8 @@ TEST_F(coredump, stackdump)
>  	FILE *file;
>  	pid_t pid;
>  
> +	line = NULL;

The syntax of getline(3) is quite interesting, since it
allocates/reallocates/uses the lineptr as needed and possibly requires
the application to free the data. I recommend moving the initialization
down to the getline() call and also add the corresponding free().

Something like this:

diff --git a/tools/testing/selftests/coredump/stackdump_test.c b/tools/testing/selftests/coredump/stackdump_test.c
index 137b2364a082..c23cf95c3f6d 100644
--- a/tools/testing/selftests/coredump/stackdump_test.c
+++ b/tools/testing/selftests/coredump/stackdump_test.c
@@ -138,10 +138,12 @@ TEST_F(coredump, stackdump)
 	ASSERT_NE(file, NULL);
 
 	/* Step 4: Make sure all stack pointer values are non-zero */
+	line = NULL;
 	for (i = 0; -1 != getline(&line, &line_length, file); ++i) {
 		stack = strtoull(line, NULL, 10);
 		ASSERT_NE(stack, 0);
 	}
+	free(line);
 
 	ASSERT_EQ(i, 1 + NUM_THREAD_SPAWN);
 
Because of how getline() works, technically your patch is good
enough. But we should probably excercise more precision in the use of
getline() so as to set a good example.

John Ogness
diff mbox series

Patch

diff --git a/tools/testing/selftests/coredump/stackdump_test.c b/tools/testing/selftests/coredump/stackdump_test.c
index 137b2364a082..1dc54e128586 100644
--- a/tools/testing/selftests/coredump/stackdump_test.c
+++ b/tools/testing/selftests/coredump/stackdump_test.c
@@ -100,6 +100,8 @@  TEST_F(coredump, stackdump)
 	FILE *file;
 	pid_t pid;
 
+	line = NULL;
+
 	/*
 	 * Step 1: Setup core_pattern so that the stackdump script is executed when the child
 	 * process crashes