public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: path: Fix path conversion of virtual drive.
@ 2021-12-09  8:17 Takashi Yano
  2021-12-09 10:08 ` Corinna Vinschen
  0 siblings, 1 reply; 3+ messages in thread
From: Takashi Yano @ 2021-12-09  8:17 UTC (permalink / raw)
  To: cygwin-patches

- The last change in path.cc introduced a bug that causes an error
  when accessing a virtual drive which mounts UNC path such as
  "\\server\share\dir" rather than "\\server\share". This patch
  fixes the issue.
---
 winsup/cygwin/path.cc | 50 +++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 21 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index eb1255849..6682d2a58 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -3507,29 +3507,37 @@ restart:
 		  if (RtlEqualUnicodePathPrefix (&fpath, &ro_u_uncp, TRUE)
 		      && !RtlEqualUnicodePathPrefix (&upath, &ro_u_uncp, TRUE))
 		    {
-		      /* ...get the remote path from the volume path name,
-			 replace remote path with drive letter, check again. */
+		      /* ...get the remote path, replace remote path
+			 with drive letter, check again. */
+		      WCHAR drive[3] =
+			{(WCHAR) towupper (upath.Buffer[4]), L':', L'\0'};
 		      WCHAR remote[MAX_PATH];
 
-		      fpbuf[1] = L'\\';
-		      BOOL r = GetVolumePathNameW (fpbuf, remote, MAX_PATH);
-		      fpbuf[1] = L'?';
-		      if (r)
-			{
-			  int remlen = wcslen (remote);
-			  if (remote[remlen - 1] == L'\\')
-			    remlen--;
-			  /* Hackfest */
-			  fpath.Buffer[4] = upath.Buffer[4]; /* Drive letter */
-			  fpath.Buffer[5] = L':';
-			  WCHAR *to = fpath.Buffer + 6;
-			  WCHAR *from = to + remlen - 6;
-			  memmove (to, from,
-				   (wcslen (from) + 1) * sizeof (WCHAR));
-			  fpath.Length -= (from - to) * sizeof (WCHAR);
-			  if (RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
-			    goto file_not_symlink;
-			}
+		      if (!QueryDosDeviceW (drive, remote, MAX_PATH))
+			goto file_not_symlink; /* fallback */
+
+		      int remlen = wcslen (remote);
+		      if (remote[remlen - 1] == L'\\')
+			remlen--;
+		      WCHAR *p;
+		      if (wcsstr (remote, L"\\??\\UNC\\") == remote)
+			remlen -= 6;
+		      else if ((p = wcschr (remote, L';') + 1)
+			       && wcsstr (p, drive) == p
+			       && (p = wcschr (p + 2, L'\\')))
+			remlen -= p - remote - 1;
+		      else
+			goto file_not_symlink; /* fallback */
+		      /* Hackfest */
+		      fpath.Buffer[4] = drive[0]; /* Drive letter */
+		      fpath.Buffer[5] = L':';
+		      WCHAR *to = fpath.Buffer + 6;
+		      WCHAR *from = to + remlen;
+		      memmove (to, from,
+			       (wcslen (from) + 1) * sizeof (WCHAR));
+		      fpath.Length -= (from - to) * sizeof (WCHAR);
+		      if (RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
+			goto file_not_symlink;
 		    }
 		  issymlink = true;
 		  /* upath.Buffer is big enough and unused from this point on.
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Cygwin: path: Fix path conversion of virtual drive.
  2021-12-09  8:17 [PATCH] Cygwin: path: Fix path conversion of virtual drive Takashi Yano
@ 2021-12-09 10:08 ` Corinna Vinschen
  2021-12-09 11:14   ` Takashi Yano
  0 siblings, 1 reply; 3+ messages in thread
From: Corinna Vinschen @ 2021-12-09 10:08 UTC (permalink / raw)
  To: cygwin-patches

Hi Takashi,

On Dec  9 17:17, Takashi Yano wrote:
> - The last change in path.cc introduced a bug that causes an error
>   when accessing a virtual drive which mounts UNC path such as
>   "\\server\share\dir" rather than "\\server\share". This patch
>   fixes the issue.
> ---
>  winsup/cygwin/path.cc | 50 +++++++++++++++++++++++++------------------
>  1 file changed, 29 insertions(+), 21 deletions(-)
> 
> diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
> index eb1255849..6682d2a58 100644
> --- a/winsup/cygwin/path.cc
> +++ b/winsup/cygwin/path.cc
> @@ -3507,29 +3507,37 @@ restart:
>  		  if (RtlEqualUnicodePathPrefix (&fpath, &ro_u_uncp, TRUE)
>  		      && !RtlEqualUnicodePathPrefix (&upath, &ro_u_uncp, TRUE))
>  		    {
> -		      /* ...get the remote path from the volume path name,
> -			 replace remote path with drive letter, check again. */
> +		      /* ...get the remote path, replace remote path
> +			 with drive letter, check again. */
> +		      WCHAR drive[3] =
> +			{(WCHAR) towupper (upath.Buffer[4]), L':', L'\0'};
>  		      WCHAR remote[MAX_PATH];
>  
> -		      fpbuf[1] = L'\\';
> -		      BOOL r = GetVolumePathNameW (fpbuf, remote, MAX_PATH);
> -		      fpbuf[1] = L'?';
> -		      if (r)
> -			{
> -			  int remlen = wcslen (remote);
> -			  if (remote[remlen - 1] == L'\\')
> -			    remlen--;
> -			  /* Hackfest */
> -			  fpath.Buffer[4] = upath.Buffer[4]; /* Drive letter */
> -			  fpath.Buffer[5] = L':';
> -			  WCHAR *to = fpath.Buffer + 6;
> -			  WCHAR *from = to + remlen - 6;
> -			  memmove (to, from,
> -				   (wcslen (from) + 1) * sizeof (WCHAR));
> -			  fpath.Length -= (from - to) * sizeof (WCHAR);
> -			  if (RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
> -			    goto file_not_symlink;
> -			}
> +		      if (!QueryDosDeviceW (drive, remote, MAX_PATH))
> +			goto file_not_symlink; /* fallback */
> +
> +		      int remlen = wcslen (remote);

QueryDosDeviceW returns the string followed by two \0 chars, and that's
reflected by its return value.  You could skip the wcslen call:

                      int remlen;
		      remlen = QueryDosDeviceW (drive, remote, MAX_PATH);
		      if (!remlen)
		      	goto file_not_symlink;
		      remlen -= 2;


> +		      if (remote[remlen - 1] == L'\\')
> +			remlen--;
> +		      WCHAR *p;
> +		      if (wcsstr (remote, L"\\??\\UNC\\") == remote)

That should be wcsncmp.  The subst'ed UNC path always begins with that
string.  Alternatively:

		      UNICODE_STRING rpath;
		      RtlInitCountedUnicodeString (&rpath, remote,
						   remlen * sizeof (WCHAR));
		      if (RtlEqualUnicodePathPrefix (&rpath, &ro_u_uncp, TRUE))


> +			remlen -= 6;
> +		      else if ((p = wcschr (remote, L';') + 1)

This expression is always true, even if wcschr returns a NULL pointer.

> +			       && wcsstr (p, drive) == p

                               && wcsncmp (p, drive, 2) == 2?

Alternatively just skip the additional drive letter check and move
the pointer immediately forward to the next backslash:

> +			       && (p = wcschr (p + 2, L'\\')))
> +			remlen -= p - remote - 1;
> +		      else
> +			goto file_not_symlink; /* fallback */
> +		      /* Hackfest */
> +		      fpath.Buffer[4] = drive[0]; /* Drive letter */
> +		      fpath.Buffer[5] = L':';
> +		      WCHAR *to = fpath.Buffer + 6;
> +		      WCHAR *from = to + remlen;
> +		      memmove (to, from,
> +			       (wcslen (from) + 1) * sizeof (WCHAR));
> +		      fpath.Length -= (from - to) * sizeof (WCHAR);
> +		      if (RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
> +			goto file_not_symlink;
>  		    }
>  		  issymlink = true;
>  		  /* upath.Buffer is big enough and unused from this point on.
> -- 
> 2.34.1

Thanks,
Corinna

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Cygwin: path: Fix path conversion of virtual drive.
  2021-12-09 10:08 ` Corinna Vinschen
@ 2021-12-09 11:14   ` Takashi Yano
  0 siblings, 0 replies; 3+ messages in thread
From: Takashi Yano @ 2021-12-09 11:14 UTC (permalink / raw)
  To: cygwin-patches

On Thu, 9 Dec 2021 11:08:46 +0100
Corinna Vinschen wrote:
> On Dec  9 17:17, Takashi Yano wrote:
> > +		      if (!QueryDosDeviceW (drive, remote, MAX_PATH))
> > +			goto file_not_symlink; /* fallback */
> > +
> > +		      int remlen = wcslen (remote);
> 
> QueryDosDeviceW returns the string followed by two \0 chars, and that's
> reflected by its return value.  You could skip the wcslen call:
> 
>                       int remlen;
> 		      remlen = QueryDosDeviceW (drive, remote, MAX_PATH);
> 		      if (!remlen)
> 		      	goto file_not_symlink;
> 		      remlen -= 2;
> 
> 
> > +		      if (remote[remlen - 1] == L'\\')
> > +			remlen--;
> > +		      WCHAR *p;
> > +		      if (wcsstr (remote, L"\\??\\UNC\\") == remote)
> 
> That should be wcsncmp.  The subst'ed UNC path always begins with that
> string.  Alternatively:
> 
> 		      UNICODE_STRING rpath;
> 		      RtlInitCountedUnicodeString (&rpath, remote,
> 						   remlen * sizeof (WCHAR));
> 		      if (RtlEqualUnicodePathPrefix (&rpath, &ro_u_uncp, TRUE))
> 
> 
> > +			remlen -= 6;
> > +		      else if ((p = wcschr (remote, L';') + 1)
> 
> This expression is always true, even if wcschr returns a NULL pointer.
> 
> > +			       && wcsstr (p, drive) == p
> 
>                                && wcsncmp (p, drive, 2) == 2?
> 
> Alternatively just skip the additional drive letter check and move
> the pointer immediately forward to the next backslash:

Thanks for checking and advice. I'll submit v2 patch shortly.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-09 11:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-09  8:17 [PATCH] Cygwin: path: Fix path conversion of virtual drive Takashi Yano
2021-12-09 10:08 ` Corinna Vinschen
2021-12-09 11:14   ` Takashi Yano

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).