This is the mail archive of the
mailing list for the GDB project.
[PATCH] Guard against 'current_directory == NULL' on gdb_abspath (PR gdb/23613)
- From: Sergio Durigan Junior <sergiodj at redhat dot com>
- To: GDB Patches <gdb-patches at sourceware dot org>
- Cc: Sergio Durigan Junior <sergiodj at redhat dot com>
- Date: Wed, 10 Jul 2019 19:46:15 -0400
- Subject: [PATCH] Guard against 'current_directory == NULL' on gdb_abspath (PR gdb/23613)
This bug has been reported a few days ago against Fedora GDB. The
problem reported is that GDB segfaults when the working directory is
deleted. It's pretty use to reproduce it:
Debugging the problem is a bit tricky, because, since the current
directory doesn't exist anymore, a corefile cannot be saved there.
After a few attempts, I came up with the following:
gdb -ex 'shell mkdir bla' -ex 'cd bla' -ex 'shell rmdir ../bla' -ex 'r echo' ./gdb/gdb
This assumes that you're inside a build directory which contains
./gdb/gdb, of course.
After investigating it, I found that the problem happens at
gdb_abspath, where we're dereferencing 'current_directory' without
checking if it's NULL:
IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
? "" : SLASH_STRING,
So I fixed the problem with the patch below. The idea is that, if
'current_directory' is NULL, then the final string returned should be
just the "path".
After fixing the bug, I found a similar one reported against our
bugzilla: PR gdb/23613. The problem is the same, but the reproducer
is a bit different.
I really tried writing a testcase for this, but unfortunately it's
apparently not possible to start GDB inside a non-existent directory
I regression tested this patch on the BuildBot, and no regressions
2019-07-10 Sergio Durigan Junior <firstname.lastname@example.org>
* gdbsupport/pathstuff.c (gdb_abspath): Guard against
'current_directory == NULL' case.
gdb/gdbsupport/pathstuff.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/gdb/gdbsupport/pathstuff.c b/gdb/gdbsupport/pathstuff.c
index fafecd543d..aa51e8f36e 100644
@@ -134,7 +134,7 @@ gdb_abspath (const char *path)
if (path == '~')
return gdb_tilde_expand_up (path);
- if (IS_ABSOLUTE_PATH (path))
+ if (IS_ABSOLUTE_PATH (path) || current_directory == NULL)
return make_unique_xstrdup (path);
/* Beware the // my son, the Emacs barfs, the botch that catch... */