From FEDORA_PATCHES Mon Sep 17 00:00:00 2001 From: Keith Seitz Date: Mon, 4 Dec 2023 11:04:22 -0800 Subject: gdb-find_and_open_source-empty-string-ub-1of4.patch ;; Backport "gdbsupport: make gdb_abspath return an std::string" ;; (Simon Marchi) I'm trying to switch these functions to use std::string instead of char arrays, as much as possible. Some callers benefit from it (can avoid doing a copy of the result), while others suffer (have to make one more copy). Change-Id: Iced49b8ee2f189744c5072a3b217aab5af17a993 diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c --- a/gdb/compile/compile.c +++ b/gdb/compile/compile.c @@ -295,8 +295,8 @@ compile_file_command (const char *args, int from_tty) error (_("You must provide a filename for this command.")); args = skip_spaces (args); - gdb::unique_xmalloc_ptr abspath = gdb_abspath (args); - std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ()); + std::string abspath = gdb_abspath (args); + std::string buffer = string_printf ("#include \"%s\"\n", abspath.c_str ()); eval_compile_command (NULL, buffer.c_str (), scope, NULL); } diff --git a/gdb/completer.c b/gdb/completer.c --- a/gdb/completer.c +++ b/gdb/completer.c @@ -2036,7 +2036,7 @@ gdb_completion_word_break_characters_throw () rl_basic_quote_characters = NULL; } - return rl_completer_word_break_characters; + return (char *) rl_completer_word_break_characters; } char * diff --git a/gdb/corelow.c b/gdb/corelow.c --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -447,7 +447,7 @@ core_target_open (const char *arg, int from_tty) gdb::unique_xmalloc_ptr filename (tilde_expand (arg)); if (!IS_ABSOLUTE_PATH (filename.get ())) - filename = gdb_abspath (filename.get ()); + filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ()); flags = O_BINARY | O_LARGEFILE; if (write_files) diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c --- a/gdb/dwarf2/index-cache.c +++ b/gdb/dwarf2/index-cache.c @@ -291,7 +291,8 @@ set_index_cache_directory_command (const char *arg, int from_tty, cmd_list_element *element) { /* Make sure the index cache directory is absolute and tilde-expanded. */ - gdb::unique_xmalloc_ptr abs (gdb_abspath (index_cache_directory)); + gdb::unique_xmalloc_ptr abs + = make_unique_xstrdup (gdb_abspath (index_cache_directory).c_str ()); xfree (index_cache_directory); index_cache_directory = abs.release (); global_index_cache.set_directory (index_cache_directory); diff --git a/gdb/main.c b/gdb/main.c --- a/gdb/main.c +++ b/gdb/main.c @@ -135,12 +135,7 @@ set_gdb_data_directory (const char *new_datadir) "../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo which isn't canonical, but that's ok. */ if (!IS_ABSOLUTE_PATH (gdb_datadir.c_str ())) - { - gdb::unique_xmalloc_ptr abs_datadir - = gdb_abspath (gdb_datadir.c_str ()); - - gdb_datadir = abs_datadir.get (); - } + gdb_datadir = gdb_abspath (gdb_datadir.c_str ()); } /* Relocate a file or directory. PROGNAME is the name by which gdb diff --git a/gdb/objfiles.c b/gdb/objfiles.c --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -341,7 +341,7 @@ objfile::objfile (bfd *abfd, const char *name, objfile_flags flags_) objfile_alloc_data (this); - gdb::unique_xmalloc_ptr name_holder; + std::string name_holder; if (name == NULL) { gdb_assert (abfd == NULL); @@ -354,7 +354,7 @@ objfile::objfile (bfd *abfd, const char *name, objfile_flags flags_) else { name_holder = gdb_abspath (name); - expanded_name = name_holder.get (); + expanded_name = name_holder.c_str (); } original_name = obstack_strdup (&objfile_obstack, expanded_name); diff --git a/gdb/source.c b/gdb/source.c --- a/gdb/source.c +++ b/gdb/source.c @@ -512,15 +512,15 @@ add_path (const char *dirname, char **which_path, int parse_separators) for (const gdb::unique_xmalloc_ptr &name_up : dir_vec) { - char *name = name_up.get (); + const char *name = name_up.get (); char *p; struct stat st; - gdb::unique_xmalloc_ptr new_name_holder; + std::string new_name_holder; /* Spaces and tabs will have been removed by buildargv(). NAME is the start of the directory. P is the '\0' following the end. */ - p = name + strlen (name); + p = name_up.get () + strlen (name); while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */ #ifdef HAVE_DOS_BASED_FILE_SYSTEM @@ -561,16 +561,18 @@ add_path (const char *dirname, char **which_path, int parse_separators) } if (name[0] == '~') - new_name_holder.reset (tilde_expand (name)); + new_name_holder + = gdb::unique_xmalloc_ptr (tilde_expand (name)).get (); #ifdef HAVE_DOS_BASED_FILE_SYSTEM else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */ - new_name_holder.reset (concat (name, ".", (char *) NULL)); + new_name_holder = std::string (name) + "."; #endif else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$') new_name_holder = gdb_abspath (name); else - new_name_holder.reset (savestring (name, p - name)); - name = new_name_holder.get (); + new_name_holder = std::string (name, p - name); + + name = new_name_holder.c_str (); /* Unless it's a variable, check existence. */ if (name[0] != '$') @@ -915,7 +917,8 @@ openp (const char *path, openp_flags opts, const char *string, else if ((opts & OPF_RETURN_REALPATH) != 0) *filename_opened = gdb_realpath (filename); else - *filename_opened = gdb_abspath (filename); + *filename_opened + = make_unique_xstrdup (gdb_abspath (filename).c_str ()); } errno = last_errno; diff --git a/gdb/top.c b/gdb/top.c --- a/gdb/top.c +++ b/gdb/top.c @@ -2065,8 +2065,7 @@ init_history (void) const char *fname = ".gdb_history"; #endif - gdb::unique_xmalloc_ptr temp (gdb_abspath (fname)); - history_filename = temp.release (); + history_filename = xstrdup (gdb_abspath (fname).c_str ()); } if (!history_filename_empty ()) @@ -2150,10 +2149,10 @@ set_history_filename (const char *args, that was read. */ if (!history_filename_empty () && !IS_ABSOLUTE_PATH (history_filename)) { - gdb::unique_xmalloc_ptr temp (gdb_abspath (history_filename)); + std::string temp = gdb_abspath (history_filename); xfree (history_filename); - history_filename = temp.release (); + history_filename = xstrdup (temp.c_str ()); } } diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c --- a/gdb/tracefile-tfile.c +++ b/gdb/tracefile-tfile.c @@ -471,7 +471,7 @@ tfile_target_open (const char *arg, int from_tty) gdb::unique_xmalloc_ptr filename (tilde_expand (arg)); if (!IS_ABSOLUTE_PATH (filename.get ())) - filename = gdb_abspath (filename.get ()); + filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ()); flags = O_BINARY | O_LARGEFILE; flags |= O_RDONLY; diff --git a/gdbserver/server.cc b/gdbserver/server.cc --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -93,31 +93,31 @@ bool non_stop; static struct { /* Set the PROGRAM_PATH. Here we adjust the path of the provided binary if needed. */ - void set (gdb::unique_xmalloc_ptr &&path) + void set (const char *path) { - m_path = std::move (path); + m_path = path; /* Make sure we're using the absolute path of the inferior when creating it. */ - if (!contains_dir_separator (m_path.get ())) + if (!contains_dir_separator (m_path.c_str ())) { int reg_file_errno; /* Check if the file is in our CWD. If it is, then we prefix its name with CURRENT_DIRECTORY. Otherwise, we leave the name as-is because we'll try searching for it in $PATH. */ - if (is_regular_file (m_path.get (), ®_file_errno)) - m_path = gdb_abspath (m_path.get ()); + if (is_regular_file (m_path.c_str (), ®_file_errno)) + m_path = gdb_abspath (m_path.c_str ()); } } /* Return the PROGRAM_PATH. */ - char *get () - { return m_path.get (); } + const char *get () + { return m_path.empty () ? nullptr : m_path.c_str (); } private: /* The program name, adjusted if needed. */ - gdb::unique_xmalloc_ptr m_path; + std::string m_path; } program_path; static std::vector program_args; static std::string wrapper_argv; @@ -3054,7 +3054,7 @@ handle_v_run (char *own_buf) } } else - program_path.set (gdb::unique_xmalloc_ptr (new_program_name)); + program_path.set (new_program_name); /* Free the old argv and install the new one. */ free_vector_argv (program_args); @@ -3845,7 +3845,7 @@ captured_main (int argc, char *argv[]) int i, n; n = argc - (next_arg - argv); - program_path.set (make_unique_xstrdup (next_arg[0])); + program_path.set (next_arg[0]); for (i = 1; i < n; i++) program_args.push_back (xstrdup (next_arg[i])); diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc --- a/gdbsupport/pathstuff.cc +++ b/gdbsupport/pathstuff.cc @@ -126,23 +126,23 @@ gdb_realpath_keepfile (const char *filename) /* See gdbsupport/pathstuff.h. */ -gdb::unique_xmalloc_ptr +std::string gdb_abspath (const char *path) { gdb_assert (path != NULL && path[0] != '\0'); if (path[0] == '~') - return gdb_tilde_expand_up (path); + return gdb_tilde_expand (path); if (IS_ABSOLUTE_PATH (path) || current_directory == NULL) - return make_unique_xstrdup (path); + return path; /* Beware the // my son, the Emacs barfs, the botch that catch... */ - return gdb::unique_xmalloc_ptr - (concat (current_directory, - IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]) - ? "" : SLASH_STRING, - path, (char *) NULL)); + return string_printf + ("%s%s%s", current_directory, + (IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]) + ? "" : SLASH_STRING), + path); } /* See gdbsupport/pathstuff.h. */ @@ -225,8 +225,8 @@ get_standard_cache_dir () if (xdg_cache_home != NULL) { /* Make sure the path is absolute and tilde-expanded. */ - gdb::unique_xmalloc_ptr abs (gdb_abspath (xdg_cache_home)); - return string_printf ("%s/gdb", abs.get ()); + std::string abs = gdb_abspath (xdg_cache_home); + return string_printf ("%s/gdb", abs.c_str ()); } #endif @@ -234,8 +234,8 @@ get_standard_cache_dir () if (home != NULL) { /* Make sure the path is absolute and tilde-expanded. */ - gdb::unique_xmalloc_ptr abs (gdb_abspath (home)); - return string_printf ("%s/" HOME_CACHE_DIR "/gdb", abs.get ()); + std::string abs = gdb_abspath (home); + return string_printf ("%s/" HOME_CACHE_DIR "/gdb", abs.c_str ()); } return {}; diff --git a/gdbsupport/pathstuff.h b/gdbsupport/pathstuff.h --- a/gdbsupport/pathstuff.h +++ b/gdbsupport/pathstuff.h @@ -49,7 +49,7 @@ extern gdb::unique_xmalloc_ptr If CURRENT_DIRECTORY is NULL, this function returns a copy of PATH. */ -extern gdb::unique_xmalloc_ptr gdb_abspath (const char *path); +extern std::string gdb_abspath (const char *path); /* If the path in CHILD is a child of the path in PARENT, return a pointer to the first component in the CHILD's pathname below the