diff src/filepath.c @ 18479:cbea1392c393 v8.1.2234

patch 8.1.2234: get_short_pathname() fails depending on encoding Commit: https://github.com/vim/vim/commit/3f39697b73f661d6900c7cf5430d967a129660d7 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Oct 30 04:10:06 2019 +0100 patch 8.1.2234: get_short_pathname() fails depending on encoding Problem: get_short_pathname() fails depending on encoding. Solution: Use the wide version of the library function. (closes https://github.com/vim/vim/issues/5129)
author Bram Moolenaar <Bram@vim.org>
date Wed, 30 Oct 2019 04:15:04 +0100
parents 8d09b7f53c71
children 9676dc5fd705
line wrap: on
line diff
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -30,26 +30,56 @@
 get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
 {
     int		l, len;
-    char_u	*newbuf;
-
-    len = *fnamelen;
-    l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
+    WCHAR	*newbuf;
+    WCHAR	*wfname;
+
+    len = MAXPATHL;
+    newbuf = malloc(len * sizeof(*newbuf));
+    if (newbuf == NULL)
+	return FAIL;
+
+    wfname = enc_to_utf16(*fnamep, NULL);
+    if (wfname == NULL)
+    {
+	vim_free(newbuf);
+	return FAIL;
+    }
+
+    l = GetShortPathNameW(wfname, newbuf, len);
     if (l > len - 1)
     {
 	// If that doesn't work (not enough space), then save the string
 	// and try again with a new buffer big enough.
-	newbuf = vim_strnsave(*fnamep, l);
+	WCHAR *newbuf_t = newbuf;
+	newbuf = vim_realloc(newbuf, (l + 1) * sizeof(*newbuf));
 	if (newbuf == NULL)
+	{
+	    vim_free(wfname);
+	    vim_free(newbuf_t);
 	    return FAIL;
-
-	vim_free(*bufp);
-	*fnamep = *bufp = newbuf;
-
+	}
 	// Really should always succeed, as the buffer is big enough.
-	l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
+	l = GetShortPathNameW(wfname, newbuf, l+1);
     }
-
-    *fnamelen = l;
+    if (l != 0)
+    {
+	char_u *p = utf16_to_enc(newbuf, NULL);
+	if (p != NULL)
+	{
+	    vim_free(*bufp);
+	    *fnamep = *bufp = p;
+	}
+	else
+	{
+	    vim_free(wfname);
+	    vim_free(newbuf);
+	    return FAIL;
+	}
+    }
+    vim_free(wfname);
+    vim_free(newbuf);
+
+    *fnamelen = l == 0 ? l : STRLEN(*bufp);
     return OK;
 }