This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2 2/5] Get rid of "gdb_dirbuf" and use "getcwd (NULL, 0)"


On 09/19/2017 05:28 AM, Sergio Durigan Junior wrote:
> Currently we have "current_directory" and "gdb_dirbuf" globals, which
> means that we basically have two possible places to consult when we
> want to know GDB's current working directory.
> 
> This is not ideal and can lead to confusion.  Moreover, the way we're
> using "gdb_difbuf" along with "getcwd" is problematic because we
> declare the buffer with "1024" elements hardcoded, which does not take
> into account longer pathnames that are possible in many filesystems.
> Using "PATH_MAX" would also not be a solution because of portability
> problems.  Therefore, the best solution is to rely on the fact that
> "getcwd (NULL, 0)" will "do the right thing" and return a
> heap-allocated string containing the full path.  With the new "getcwd"
> module from gnulib, it is now possible to do that without worrying
> about breaking some target.

s/target/host/

> --- a/gdb/cli/cli-cmds.c
> +++ b/gdb/cli/cli-cmds.c
> @@ -382,13 +382,16 @@ pwd_command (char *args, int from_tty)
>  {
>    if (args)
>      error (_("The \"pwd\" command does not take an argument: %s"), args);
> -  if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
> +
> +  gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
> +
> +  if (cwd.get () == NULL)

No need for get() here:

  if (cwd == NULL)

> diff --git a/gdb/mi/mi-cmd-env.c b/gdb/mi/mi-cmd-env.c
> index 977b6e274d..1e4c09352f 100644
> --- a/gdb/mi/mi-cmd-env.c
> +++ b/gdb/mi/mi-cmd-env.c
> @@ -73,12 +73,12 @@ mi_cmd_env_pwd (const char *command, char **argv, int argc)
>      }
>       
>    /* Otherwise the mi level is 2 or higher.  */
> -
> -  if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
> +  gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
> +  if (cwd.get () == NULL)

Ditto.

Otherwise OK.

Thanks,
Pedro Alves


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]