Skip to content

Commit 6abd4cf

Browse files
committed
gdb: check for empty strings in get_standard_cache_dir/get_standard_config_dir
As reported in PR 27157, if some environment variables read at startup by GDB are defined but empty, we hit the assert in gdb_abspath: $ XDG_CACHE_HOME= ./gdb -nx --data-directory=data-directory -q AddressSanitizer:DEADLYSIGNAL ================================================================= ==2007040==ERROR: AddressSanitizer: SEGV on unknown address 0x0000000001b0 (pc 0x5639d4aa4127 bp 0x7ffdac232c00 sp 0x7ffdac232bf0 T0) ==2007040==The signal is caused by a READ memory access. ==2007040==Hint: address points to the zero page. #0 0x5639d4aa4126 in target_stack::top() const /home/smarchi/src/binutils-gdb/gdb/target.h:1334 #1 0x5639d4aa41f1 in inferior::top_target() /home/smarchi/src/binutils-gdb/gdb/inferior.h:369 #2 0x5639d4a70b1f in current_top_target() /home/smarchi/src/binutils-gdb/gdb/target.c:120 #3 0x5639d4b00591 in gdb_readline_wrapper_cleanup::gdb_readline_wrapper_cleanup() /home/smarchi/src/binutils-gdb/gdb/top.c:1046 #4 0x5639d4afab31 in gdb_readline_wrapper(char const*) /home/smarchi/src/binutils-gdb/gdb/top.c:1104 #5 0x5639d4ccce2c in defaulted_query /home/smarchi/src/binutils-gdb/gdb/utils.c:893 #6 0x5639d4ccd6af in query(char const*, ...) /home/smarchi/src/binutils-gdb/gdb/utils.c:985 #7 0x5639d4ccaec1 in internal_vproblem /home/smarchi/src/binutils-gdb/gdb/utils.c:373 #8 0x5639d4ccb3d1 in internal_verror(char const*, int, char const*, __va_list_tag*) /home/smarchi/src/binutils-gdb/gdb/utils.c:439 #9 0x5639d5151a92 in internal_error(char const*, int, char const*, ...) /home/smarchi/src/binutils-gdb/gdbsupport/errors.cc:55 bminor#10 0x5639d5162ab4 in gdb_abspath(char const*) /home/smarchi/src/binutils-gdb/gdbsupport/pathstuff.cc:132 bminor#11 0x5639d5162fac in get_standard_cache_dir[abi:cxx11]() /home/smarchi/src/binutils-gdb/gdbsupport/pathstuff.cc:228 bminor#12 0x5639d3e76a81 in _initialize_index_cache() /home/smarchi/src/binutils-gdb/gdb/dwarf2/index-cache.c:325 bminor#13 0x5639d4dbbe92 in initialize_all_files() /home/smarchi/build/binutils-gdb/gdb/init.c:321 bminor#14 0x5639d4b00259 in gdb_init(char*) /home/smarchi/src/binutils-gdb/gdb/top.c:2344 bminor#15 0x5639d4440715 in captured_main_1 /home/smarchi/src/binutils-gdb/gdb/main.c:950 #16 0x5639d444252e in captured_main /home/smarchi/src/binutils-gdb/gdb/main.c:1229 #17 0x5639d44425cf in gdb_main(captured_main_args*) /home/smarchi/src/binutils-gdb/gdb/main.c:1254 #18 0x5639d3923371 in main /home/smarchi/src/binutils-gdb/gdb/gdb.c:32 #19 0x7fa002d3f0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #20 0x5639d392314d in _start (/home/smarchi/build/binutils-gdb/gdb/gdb+0x4d414d) gdb_abspath doesn't handle empty strings, so handle this case in the callers. If a variable is defined but empty, I think it's reasonable in this case to just ignore it, as if it was not defined. Note that this sometimes also lead to a segfault, because the failed assertion happens very early during startup, before things are fully initialized. gdbsupport/ChangeLog: PR gdb/27157 * pathstuff.cc (get_standard_cache_dir, get_standard_config_dir, find_gdb_home_config_file): Add empty string check. gdb/testsuite/ChangeLog: PR gdb/27157 * gdb.base/empty-host-env-vars.exp: New test. Change-Id: I8654d8e97e74e1dff6d308c111ae4b1bbf07bef9
1 parent d00f086 commit 6abd4cf

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

gdb/testsuite/ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2021-01-08 Simon Marchi <[email protected]>
2+
3+
PR gdb/27157
4+
* gdb.base/empty-host-env-vars.exp: New test.
5+
16
2021-01-08 Andrew Burgess <[email protected]>
27

38
* gdb.base/completion.exp: Add a new test.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2021 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# GDB reads some environment variables on startup, make sure it behaves
17+
# correctly if these variables are defined but empty.
18+
19+
set all_env_vars { HOME XDG_CACHE_HOME LOCALAPPDATA XDG_CONFIG_HOME }
20+
21+
# Record the initial value of the index-cache directory.
22+
clean_restart
23+
set index_cache_directory ""
24+
gdb_test_multiple "show index-cache directory" "" {
25+
-re -wrap "The directory of the index cache is \"(.*)\"\\." {
26+
set index_cache_directory $expect_out(1,string)
27+
set index_cache_directory [string_to_regexp $index_cache_directory]
28+
pass $gdb_test_name
29+
}
30+
}
31+
32+
foreach_with_prefix env_var_name $all_env_vars {
33+
# Restore the original state of the environment variable.
34+
save_vars env($env_var_name) {
35+
set env($env_var_name) {}
36+
clean_restart
37+
38+
# Verify that the empty environment variable didn't affect the
39+
# index-cache directory setting, that we still see the initial value.
40+
# "HOME" is different, because if that one is unset, GDB isn't even
41+
# able to compute the default location. In that case, we expect it to
42+
# be empty.
43+
if { $env_var_name == "HOME" } {
44+
gdb_test "show index-cache directory" \
45+
"The directory of the index cache is \"\"\\."
46+
} else {
47+
gdb_test "show index-cache directory" \
48+
"The directory of the index cache is \"$index_cache_directory\"\\."
49+
}
50+
}
51+
}
52+
53+
# Try the same, but with all the env vars set to an empty value at the same
54+
# time.
55+
with_test_prefix "all env vars" {
56+
set save_vars_arg {}
57+
foreach env_var_name $all_env_vars {
58+
lappend save_vars_arg env($env_var_name)
59+
}
60+
61+
# Restore the original state of all the environment variables.
62+
save_vars $save_vars_arg {
63+
foreach env_var_name $all_env_vars {
64+
set env($env_var_name) {}
65+
}
66+
67+
clean_restart
68+
69+
gdb_test "show index-cache directory" \
70+
"The directory of the index cache is \"\"\\."
71+
}
72+
}

gdbsupport/ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2021-01-08 Simon Marchi <[email protected]>
2+
3+
PR gdb/27157
4+
* pathstuff.cc (get_standard_cache_dir, get_standard_config_dir,
5+
find_gdb_home_config_file): Add empty string check.
6+
17
2021-01-06 Mike Frysinger <[email protected]>
28

39
* common-utils.h (align_up): Fix typo in header comment.

gdbsupport/pathstuff.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ get_standard_cache_dir ()
222222

223223
#ifndef __APPLE__
224224
const char *xdg_cache_home = getenv ("XDG_CACHE_HOME");
225-
if (xdg_cache_home != NULL)
225+
if (xdg_cache_home != NULL && xdg_cache_home[0] != '\0')
226226
{
227227
/* Make sure the path is absolute and tilde-expanded. */
228228
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_cache_home));
@@ -231,7 +231,7 @@ get_standard_cache_dir ()
231231
#endif
232232

233233
const char *home = getenv ("HOME");
234-
if (home != NULL)
234+
if (home != NULL && home[0] != '\0')
235235
{
236236
/* Make sure the path is absolute and tilde-expanded. */
237237
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
@@ -240,14 +240,14 @@ get_standard_cache_dir ()
240240

241241
#ifdef WIN32
242242
const char *win_home = getenv ("LOCALAPPDATA");
243-
if (win_home != NULL)
243+
if (win_home != NULL && win_home[0] != '\0')
244244
{
245245
/* Make sure the path is absolute and tilde-expanded. */
246246
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (win_home));
247247
return string_printf ("%s/gdb", abs.get ());
248248
}
249249
#endif
250-
250+
251251
return {};
252252
}
253253

@@ -289,7 +289,7 @@ get_standard_config_dir ()
289289

290290
#ifndef __APPLE__
291291
const char *xdg_config_home = getenv ("XDG_CONFIG_HOME");
292-
if (xdg_config_home != NULL)
292+
if (xdg_config_home != NULL && xdg_config_home[0] != '\0')
293293
{
294294
/* Make sure the path is absolute and tilde-expanded. */
295295
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_config_home));
@@ -298,7 +298,7 @@ get_standard_config_dir ()
298298
#endif
299299

300300
const char *home = getenv ("HOME");
301-
if (home != NULL)
301+
if (home != NULL && home[0] != '\0')
302302
{
303303
/* Make sure the path is absolute and tilde-expanded. */
304304
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
@@ -340,7 +340,7 @@ find_gdb_home_config_file (const char *name, struct stat *buf)
340340
}
341341

342342
const char *homedir = getenv ("HOME");
343-
if (homedir != nullptr)
343+
if (homedir != nullptr && homedir[0] != '\0')
344344
{
345345
/* Make sure the path is absolute and tilde-expanded. */
346346
gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (homedir));

0 commit comments

Comments
 (0)