From: John Ogness <john.ogness@linutronix.de>
To: Nam Cao <namcao@linutronix.de>, Christian Brauner <brauner@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Nam Cao <namcao@linutronix.de>
Subject: Re: [PATCH 1/3] selftests: coredump: Properly initialize pointer
Date: Tue, 01 Apr 2025 10:02:58 +0206 [thread overview]
Message-ID: <8434esmi0l.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <b595166b7ece013b371c2bac1098533f1ffa12d0.1743438749.git.namcao@linutronix.de>
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
next prev parent reply other threads:[~2025-04-01 7:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-31 16:50 [PATCH 0/3] selftests: coredump: Some bug fixes Nam Cao
2025-03-31 16:50 ` [PATCH 1/3] selftests: coredump: Properly initialize pointer Nam Cao
2025-04-01 7:56 ` John Ogness [this message]
2025-03-31 16:50 ` [PATCH 2/3] selftests: coredump: Use waitpid() instead of busy-wait Nam Cao
2025-04-01 12:08 ` John Ogness
2025-03-31 16:50 ` [PATCH 3/3] selftests: coredump: Raise timeout to 2 minutes Nam Cao
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=8434esmi0l.fsf@jogness.linutronix.de \
--to=john.ogness@linutronix.de \
--cc=brauner@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=namcao@linutronix.de \
--cc=shuah@kernel.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
all inboxes | Powered by JetHome®