Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle UNC paths correctly when making cURL relocatable #51

Merged
merged 1 commit into from
Jun 22, 2021

Conversation

dscho
Copy link
Member

@dscho dscho commented Jun 14, 2021

The code to make cURL relocatable simplifies/normalizes paths, e.g.
reducing multiple forward slashes to a single one.

This is okay in general, but at the beginning of the path, a double
slash has a special meaning: it denotes a UNC path of the form
//<host>/<path>. It would therefore be a mistake to reduce those first
two slashes to a single one: otherwise it would refer to the absolute
path relative to the current directory's drive.

Let's preserve the double slashes.

This fixes git-for-windows/git#3266

Signed-off-by: Johannes Schindelin <[email protected]>
@@ -156,7 +156,7 @@ index 000000000..c09458e95
+ *path_p = '/';
+ }
+ /* Replace any '//' with '/' */
+ path_p = path;
+ path_p = path + !!*path; /* skip first character, if any, to handle UNC paths correctly */
+ while ((path_p = strstr (path_p, "//")) != NULL)
+ {
+ memmove (path_p, path_p + 1, path_size--);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's unrelated to the change, but it looks like a bug to me.
This memmove() call will modify data located after the "path" string in memory for most of the cases.
Like if the "path" is "foobar//goo" it will be called like memmove("//goo", "/goo", 11), so it will move data located after the end of the "path" string unless the memmove() has a specific implementation to check end-of-string (that would be quite unusual).

I'd expect it to be something like:

memmove (path_p, path_p + 1, strlen(path_p + 1) + 1);
or to be shorter:
memmove (path_p, path_p + 1, strlen(path_p));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you're right... This bug exists in MSYS2, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a much better version would look like this:

#define IS_SLASH(c) ((c) == '/' || (c) == '\\')

void
sanitize_path(char *path)
{
  int i, j = IS_SLASH(*path) ? 1 : 0;

  for (i = j; path[i]; i++) {
    if (!IS_SLASH(path[i]))
      path[j++] = path[i];
    else {
      /* reduce runs of slashes into a single forward slash */
      while (IS_SLASH(path[i+1]))
        i++;
      path[j++] = '/';
    }
  }

  path[j] = '\0';
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to set aside time at some point in the near future to get this one in.

@dscho dscho merged commit 6c42040 into git-for-windows:main Jun 22, 2021
@dscho dscho deleted the relocatable-curl-and-unc-paths branch June 22, 2021 12:57
dscho added a commit to git-for-windows/build-extra that referenced this pull request Jun 25, 2021
Remote HTTPS repositories [could not be accessed
from within portable Git installed into a network
share](git-for-windows/git#3266). This [has
been fixed](git-for-windows/MINGW-packages#51).

Signed-off-by: Johannes Schindelin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fail to access https://github.com/* if portable version is unpacked in a network share.
2 participants