public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Eli Zaretskii <eliz@gnu.org>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 5/5] gdbsupport: add path_join function
Date: Mon, 18 Apr 2022 14:11:34 -0400	[thread overview]
Message-ID: <c9629980-853e-b284-60d5-f0ad24053b9d@polymtl.ca> (raw)
In-Reply-To: <83h76u3n04.fsf@gnu.org>



On 2022-04-15 01:59, Eli Zaretskii wrote:
>> Date: Thu, 14 Apr 2022 16:01:37 -0400
>> From: Simon Marchi via Gdb-patches <gdb-patches@sourceware.org>
>> Cc: Simon Marchi <simon.marchi@efficios.com>
>>
>> From: Simon Marchi <simon.marchi@efficios.com>
>>
>> In this review [1], Eli pointed out that we should be careful when
>> concatenating file names to avoid duplicated slashes.  On Windows, a
>> double slash at the beginning of a file path has a special meaning.  So
>> naively concatenating "/"  and "foo/bar" would give "//foo/bar", which
>> would not give the desired results.  We already have a few spots doing:
>>
>>   if (first_path ends with a slash)
>>     path = first_path + second_path
>>   else
>>     path = first_path + slash + second_path
>>
>> In general, I think it's nice to avoid superfluous slashes in file
>> paths, since they might end up visible to the user and look a bit
>> unprofessional.
>>
>> Introduce the path_join function that can be used to join multiple path
>> components together (along with unit tests).
> 
> Thanks.
> 
>> +static void
>> +test ()
>> +{
>> +  std::string s;
>> +
>> +  s = ::path_join ("/foo", "bar", nullptr);
>> +  SELF_CHECK (s == "/foo/bar");
>> +
>> +  s = ::path_join ("/foo", "/bar", nullptr);
>> +  SELF_CHECK (s == "/foo/bar");
>> +
>> +  s = ::path_join ("/", "bar", nullptr);
>> +  SELF_CHECK (s == "/bar");
>> +
>> +  s = ::path_join ("foo/", "/bar", nullptr);
>> +  SELF_CHECK (s == "foo/bar");
>> +
>> +  s = ::path_join ("foo", "bar", "", nullptr);
>> +  SELF_CHECK (s == "foo/bar");
>> +
>> +  s = ::path_join ("", "/foo", nullptr);
>> +  SELF_CHECK (s == "/foo");
>> +
>> +  s = ::path_join ("", "foo", nullptr);
>> +  SELF_CHECK (s == "foo");
>> +
>> +  s = ::path_join ("foo", "", "bar", nullptr);
>> +  SELF_CHECK (s == "foo/bar");
>> +
>> +  s = ::path_join ("foo", "", nullptr);
>> +  SELF_CHECK (s == "foo");
>> +
>> +  s = ::path_join ("foo/", "", nullptr);
>> +  SELF_CHECK (s == "foo/");
> 
> Suggest to add a couple of Windows-specific tests here: one which
> starts with "d:/" instead of just "/", and another with backslashes.

Should these tests only be ran on Windows hosts?  In an ideal world,
this function (and the rest of path handling in GDB) should support
cross-debugging both ways: GDB on Linux debugging a remote Windows
program, GDB on Windows debugging a remote Linux program.  That means
corner cases like GDB on Linux should not recognize C:/foo as an
absolute path when debugging a native Linux program, but it should
recognize it as an absolute path when debugging a remote Windows
program.  And it should recognize foo\bar\ as ending with a directory
separator when debugging remotely a Windows program, and not when
debugging natively.  And vice-versa for a Windows GDB / Linux program.

Existing code isn't written with that in mind though (macros like
IS_DIR_SEPARATOR are host-specific), and I'm not ready to tackle that
right now.   So I've written path_join ignoring those cross-debugging
scenarios.

So, if I add a test like:

  s = ::path_join ("foo\\", "bar", nullptr);
  SELF_CHECK (s == "foo\\bar" || s == "foo/bar");

it won't pass on Linux, as s will be "foo\\/bar".  So, as long as
path_join is host-dependent, I think we'll need to accept that some
tests will be host-dependent.  That's not ideal, but that's how things
are at the moment.

>> +std::string
>> +path_join (const char *component...)
>> +{
>> +  std::string path = component;
>> +
>> +  auto skip_leading_dir_seps = [] (const char *s)
>> +    {
>> +      while (*s == '/')
>> +	++s;
>> +
>> +      return s;
>> +    };
>> +
>> +  va_list args;
>> +  va_start (args, component);
>> +
>> +  const char *c = va_arg (args, const char *);
>> +  while (c != nullptr)
>> +    {
>> +      if (!path.empty ())
>> +	c = skip_leading_dir_seps (c);
>> +
>> +      if (*c != '\0')
>> +	{
>> +	  if (!path.empty () && path.back () != '/')
>> +	    path += '/';
>> +
>> +	  path += c;
>> +	}
>> +
>> +      c = va_arg (args, const char *);
>> +    }
>> +
>> +  va_end (args);
>> +
>> +  return path;
>> +}
> 
> I think this should use IS_DIR_SEPARATOR (or override the '=' and '!='
> operators), to support Windows file names with backslashes.

Changed both instances of '/' to use IS_DIR_SEPARATOR.

Simon

  reply	other threads:[~2022-04-18 18:11 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-14 20:01 [PATCH 1/5] gdb: call gdb_tilde_expand instead of gdb_tilde_expand_up in source_script_with_search Simon Marchi
2022-04-14 20:01 ` [PATCH 2/5] gdbsupport: make gdb_abspath return an std::string Simon Marchi
2022-04-18 19:41   ` Tom Tromey
2022-04-18 20:09     ` Pedro Alves
2022-04-18 20:11     ` Simon Marchi
2022-04-14 20:01 ` [PATCH 3/5] gdbsupport: make gdb_realpath_keepfile " Simon Marchi
2022-04-18 19:44   ` Tom Tromey
2022-04-14 20:01 ` [PATCH 4/5] gdb: use gdb_tilde_expand instead of gdb_tilde_expand_up in source_script_with_search Simon Marchi
2022-04-18 19:44   ` Tom Tromey
2022-04-18 20:12     ` Simon Marchi
2022-04-14 20:01 ` [PATCH 5/5] gdbsupport: add path_join function Simon Marchi
2022-04-15  5:59   ` Eli Zaretskii
2022-04-18 18:11     ` Simon Marchi [this message]
2022-04-18 18:30       ` Eli Zaretskii
2022-04-18 19:24       ` Pedro Alves
2022-04-15 14:38   ` Lancelot SIX
2022-04-15 16:55     ` Lancelot SIX
2022-04-18 18:43     ` Simon Marchi
2022-04-18 19:09       ` Pedro Alves
2022-04-18 19:12         ` Simon Marchi
2022-04-18 20:55           ` Simon Marchi
2022-04-18 21:07             ` Pedro Alves
2022-04-19  0:19               ` Simon Marchi
2022-04-18 19:22       ` Pedro Alves
2022-04-18 20:01       ` Tom Tromey
2022-04-18 23:11       ` Lancelot SIX
2022-04-20  0:22         ` Simon Marchi
2022-04-18 19:36 ` [PATCH 1/5] gdb: call gdb_tilde_expand instead of gdb_tilde_expand_up in source_script_with_search Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c9629980-853e-b284-60d5-f0ad24053b9d@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).