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

Nam Cao posted 3 patches 10 months ago
[PATCH v2 1/3] selftests: coredump: Properly initialize pointer
Posted by Nam Cao 10 months ago
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(+)

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);
 
-- 
2.39.5
Re: [PATCH v2 1/3] selftests: coredump: Properly initialize pointer
Posted by ALOK TIWARI 10 months ago

On 11-04-2025 20:39, Nam Cao wrote:
>   	/* Step 4: Make sure all stack pointer values are non-zero */
> +	line = NULL;

such case it should initialize at declaration time.
better to move up char *test_dir, *line = NULL;

>   	for (i = 0; -1 != getline(&line, &line_length, file); ++i) {
>   		stack = strtoull(line, NULL, 10);
>   		ASSERT_NE(stack, 0);
>   	}


Thanks,
Alok