From: Simon Marchi <simark@simark.ca>
To: Daniel Starke <daniel-email@gmx.net>, gdb-patches@sourceware.org
Subject: Re: [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable
Date: Tue, 4 Mar 2025 22:38:53 -0500 [thread overview]
Message-ID: <edfe6e17-1c20-4a4c-944f-247ff71b6c10@simark.ca> (raw)
In-Reply-To: <20250304215011.2092-1-daniel-email@gmx.net>
On 2025-03-04 16:50, Daniel Starke wrote:
> When running "show" with missing PATH variable a null pointer is being
> dereferenced in path_info().
>
> path_command() correctly checks whether PATH has been set before using it.
> It then calls path_info() which retrieves the variable again but fails to
> perform the null pointer test on it. As a result, the application crashes with
> SIGSEGV on Windows for example.
>
> Fix this by handling the null pointer case in path_info() accordingly.
>
> Signed-off-by: Daniel Starke <daniel-email@gmx.net>
> ---
> gdb/infcmd.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/gdb/infcmd.c b/gdb/infcmd.c
> index 00703e44b7b..adb2592ae8e 100644
> --- a/gdb/infcmd.c
> +++ b/gdb/infcmd.c
> @@ -2116,7 +2116,10 @@ static void
> path_info (const char *args, int from_tty)
> {
> gdb_puts ("Executable and object file path: ");
> - gdb_puts (current_inferior ()->environment.get (path_var_name));
> + const char *env = current_inferior ()->environment.get (path_var_name);
> + if (!env)
> + env = "";
> + gdb_puts (env);
> gdb_puts ("\n");
> }
>
> --
> 2.39.5
>
I was wondering why I couldn't reproduce on Linux. On my system, the
gdb_puts call goes to pager_file::puts, which does handle the nullptr
case:
if (linebuffer == 0)
return;
On Windows, I suppose it goes to stdio_file::puts directly or something
like that, which doesn't handle the nullptr case.
I propose this little tweak shown below, changing the code to use
gdb_printf instead of gdb_puts, if that's ok with you.
From 37542545e29b2f5c2b8d1defcfa37e3e3b921854 Mon Sep 17 00:00:00 2001
From: Daniel Starke <daniel-email@gmx.net>
Date: Tue, 4 Mar 2025 22:50:11 +0100
Subject: [PATCH] gdb: fix null pointer dereference on missing PATH variable
When running "show" with missing PATH variable a null pointer is being
dereferenced in path_info().
path_command() correctly checks whether PATH has been set before using it.
It then calls path_info() which retrieves the variable again but fails to
perform the null pointer test on it. As a result, the application crashes with
SIGSEGV on Windows for example.
Fix this by handling the null pointer case in path_info() accordingly.
Signed-off-by: Daniel Starke <daniel-email@gmx.net>
Co-Authored-By: Simon Marchi <simon.marchi@efficios.com>
Change-Id: I41ef10f00802d3163793491454190008e78f5dc1
---
gdb/infcmd.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 00703e44b7b5..06b7038df506 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2115,9 +2115,10 @@ static const char path_var_name[] = "PATH";
static void
path_info (const char *args, int from_tty)
{
- gdb_puts ("Executable and object file path: ");
- gdb_puts (current_inferior ()->environment.get (path_var_name));
- gdb_puts ("\n");
+ const char *env = current_inferior ()->environment.get (path_var_name);
+
+ gdb_printf ("Executable and object file path: %s\n",
+ env != nullptr ? env : "");
}
/* Add zero or more directories to the front of the execution path. */
base-commit: aa2cd0e39dc81b28ba7c934faac18bd4d8287450
--
2.48.1
next prev parent reply other threads:[~2025-03-05 3:39 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-04 21:50 Daniel Starke
2025-03-05 3:38 ` Simon Marchi [this message]
2025-03-05 15:28 ` Tom Tromey
2025-03-05 15:51 ` Simon Marchi
2025-03-05 16:17 ` daniel-email
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=edfe6e17-1c20-4a4c-944f-247ff71b6c10@simark.ca \
--to=simark@simark.ca \
--cc=daniel-email@gmx.net \
--cc=gdb-patches@sourceware.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