* [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable
@ 2025-03-04 21:50 Daniel Starke
2025-03-05 3:38 ` Simon Marchi
0 siblings, 1 reply; 5+ messages in thread
From: Daniel Starke @ 2025-03-04 21:50 UTC (permalink / raw)
To: gdb-patches; +Cc: Daniel Starke
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable
2025-03-04 21:50 [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable Daniel Starke
@ 2025-03-05 3:38 ` Simon Marchi
2025-03-05 15:28 ` Tom Tromey
2025-03-05 16:17 ` daniel-email
0 siblings, 2 replies; 5+ messages in thread
From: Simon Marchi @ 2025-03-05 3:38 UTC (permalink / raw)
To: Daniel Starke, gdb-patches
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable
2025-03-05 3:38 ` Simon Marchi
@ 2025-03-05 15:28 ` Tom Tromey
2025-03-05 15:51 ` Simon Marchi
2025-03-05 16:17 ` daniel-email
1 sibling, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2025-03-05 15:28 UTC (permalink / raw)
To: Simon Marchi; +Cc: Daniel Starke, gdb-patches
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
Simon> I propose this little tweak shown below, changing the code to use
Simon> gdb_printf instead of gdb_puts, if that's ok with you.
Simon> + gdb_printf ("Executable and object file path: %s\n",
Probably should use _() here.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable
2025-03-05 15:28 ` Tom Tromey
@ 2025-03-05 15:51 ` Simon Marchi
0 siblings, 0 replies; 5+ messages in thread
From: Simon Marchi @ 2025-03-05 15:51 UTC (permalink / raw)
To: Tom Tromey; +Cc: Daniel Starke, gdb-patches
On 3/5/25 10:28 AM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
>
> Simon> I propose this little tweak shown below, changing the code to use
> Simon> gdb_printf instead of gdb_puts, if that's ok with you.
>
> Simon> + gdb_printf ("Executable and object file path: %s\n",
>
> Probably should use _() here.
>
> Approved-By: Tom Tromey <tom@tromey.com>
>
> Tom
Thanks, pushing with that fixed.
Simon
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable
2025-03-05 3:38 ` Simon Marchi
2025-03-05 15:28 ` Tom Tromey
@ 2025-03-05 16:17 ` daniel-email
1 sibling, 0 replies; 5+ messages in thread
From: daniel-email @ 2025-03-05 16:17 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
> I propose this little tweak shown below, changing the code to use
> gdb_printf instead of gdb_puts, if that's ok with you.
I did not test that but sure.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-03-05 16:18 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-04 21:50 [PATCH 1/1] gdb: Fix null pointer dereference on missing PATH variable Daniel Starke
2025-03-05 3:38 ` Simon Marchi
2025-03-05 15:28 ` Tom Tromey
2025-03-05 15:51 ` Simon Marchi
2025-03-05 16:17 ` daniel-email
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox