Skip to content

Commit f7d8dd0

Browse files
committed
Merge branch 'wcstombs'
This topic branch fixes the problem where a UTF-16 command-line was converted to UTF-8 in an incorrect way (because Cygwin treated it as if it was a file name and applied some magic that is intended to allow for otherwise invalid file names on Windows). Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 1f9149a + f8ea69f commit f7d8dd0

File tree

14 files changed

+68
-60
lines changed

14 files changed

+68
-60
lines changed

newlib/libc/locale/lctype.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525

2626
#define LCCTYPE_SIZE (sizeof(struct lc_ctype_T) / sizeof(char *))
2727

28+
#ifdef __CYGWIN__
29+
/* Cygwin uses __utf8_mbtowc() by default, therefore mb_cur_max := 6 */
30+
static char numone[] = { '\x06', '\0'};
31+
#else
2832
static char numone[] = { '\1', '\0'};
33+
#endif
2934

3035
const struct lc_ctype_T _C_ctype_locale = {
3136
"ASCII", /* codeset */

winsup/cygwin/dcrt0.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,9 +912,9 @@ dll_crt0_1 (void *)
912912
if (!__argc)
913913
{
914914
PWCHAR wline = GetCommandLineW ();
915-
size_t size = sys_wcstombs_no_path (NULL, 0, wline) + 1;
915+
size_t size = sys_wcstombs (NULL, 0, wline) + 1;
916916
char *line = (char *) alloca (size);
917-
sys_wcstombs_no_path (line, size, wline);
917+
sys_wcstombs (line, size, wline);
918918

919919
/* Scan the command line and build argv. Expand wildcards if not
920920
called from another cygwin process. */

winsup/cygwin/dtable.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ handle_to_fn (HANDLE h, char *posix_fn)
10281028
if (wcsncasecmp (w32, DEVICE_PREFIX, DEVICE_PREFIX_LEN) != 0
10291029
|| !QueryDosDeviceW (NULL, fnbuf, sizeof (fnbuf) / sizeof (WCHAR)))
10301030
{
1031-
sys_wcstombs (posix_fn, NT_MAX_PATH, w32, w32len);
1031+
sys_wcstombs_path (posix_fn, NT_MAX_PATH, w32, w32len);
10321032
return false;
10331033
}
10341034

winsup/cygwin/environ.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ win32env_to_cygenv (PWCHAR rawenv, bool posify)
918918
eventually want to use them). */
919919
for (i = 0, w = rawenv; *w != L'\0'; w = wcschr (w, L'\0') + 1, i++)
920920
{
921-
sys_wcstombs_alloc_no_path (&newp, HEAP_NOTHEAP, w);
921+
sys_wcstombs_alloc (&newp, HEAP_NOTHEAP, w);
922922
if (i >= envc)
923923
envp = (char **) realloc (envp, (4 + (envc += 100)) * sizeof (char *));
924924
envp[i] = newp;
@@ -978,15 +978,15 @@ getwinenveq (const char *name, size_t namelen, int x)
978978
int totlen = GetEnvironmentVariableW (name0, valbuf, 32768);
979979
if (totlen > 0)
980980
{
981-
totlen = sys_wcstombs_no_path (NULL, 0, valbuf) + 1;
981+
totlen = sys_wcstombs (NULL, 0, valbuf) + 1;
982982
if (x == HEAP_1_STR)
983983
totlen += namelen;
984984
else
985985
namelen = 0;
986986
char *p = (char *) cmalloc_abort ((cygheap_types) x, totlen);
987987
if (namelen)
988988
strcpy (p, name);
989-
sys_wcstombs_no_path (p + namelen, totlen, valbuf);
989+
sys_wcstombs (p + namelen, totlen, valbuf);
990990
debug_printf ("using value from GetEnvironmentVariable for '%W'", name0);
991991
return p;
992992
}
@@ -1144,7 +1144,7 @@ build_env (const char * const *envp, PWCHAR &envblock, int &envc,
11441144
for (winnum = 0, var = cwinenv;
11451145
*var;
11461146
++winnum, var = wcschr (var, L'\0') + 1)
1147-
sys_wcstombs_alloc_no_path (&winenv[winnum], HEAP_NOTHEAP, var);
1147+
sys_wcstombs_alloc (&winenv[winnum], HEAP_NOTHEAP, var);
11481148
}
11491149
DestroyEnvironmentBlock (cwinenv);
11501150
/* Eliminate variables which are already available in envp, as well as

winsup/cygwin/external.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fillout_pinfo (pid_t pid, int winpid)
9292
ep.rusage_self = p->rusage_self;
9393
ep.rusage_children = p->rusage_children;
9494
ep.progname[0] = '\0';
95-
sys_wcstombs(ep.progname, MAX_PATH, p->progname);
95+
sys_wcstombs_path (ep.progname, MAX_PATH, p->progname);
9696
ep.strace_mask = 0;
9797
ep.version = EXTERNAL_PINFO_VERSION;
9898

winsup/cygwin/fhandler/disk_file.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,7 @@ fhandler_disk_file::readdir_helper (DIR *dir, dirent *de, DWORD w32_err,
24122412
char *p = stpcpy (file, pc.get_posix ());
24132413
if (p[-1] != '/')
24142414
*p++ = '/';
2415-
sys_wcstombs (p, NT_MAX_PATH - (p - file),
2415+
sys_wcstombs_path (p, NT_MAX_PATH - (p - file),
24162416
fname->Buffer, fname->Length / sizeof (WCHAR));
24172417
path_conv fpath (file, PC_SYM_NOFOLLOW);
24182418
if (fpath.issymlink ())
@@ -2433,7 +2433,7 @@ fhandler_disk_file::readdir_helper (DIR *dir, dirent *de, DWORD w32_err,
24332433
}
24342434
}
24352435

2436-
sys_wcstombs (de->d_name, NAME_MAX + 1, fname->Buffer,
2436+
sys_wcstombs_path (de->d_name, NAME_MAX + 1, fname->Buffer,
24372437
fname->Length / sizeof (WCHAR));
24382438

24392439
/* Don't try to optimize relative to dir->__d_position. On several

winsup/cygwin/fhandler/netdrive.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,15 @@ fhandler_netdrive::readdir (DIR *dir, dirent *de)
283283
tp.u_get (&ds);
284284
RtlInitUnicodeString (&ss, bs);
285285
RtlDowncaseUnicodeString (&ds, &ss, FALSE);
286-
sys_wcstombs (de->d_name, sizeof de->d_name,
286+
sys_wcstombs_path (de->d_name, sizeof de->d_name,
287287
ds.Buffer, ds.Length / sizeof (WCHAR));
288288
de->d_ino = hash_path_name (get_ino (), de->d_name);
289289
}
290290
else
291291
{
292-
sys_wcstombs (de->d_name, sizeof de->d_name, bs);
292+
sys_wcstombs_path (de->d_name, sizeof de->d_name, bs);
293293
char *rpath = tp.c_get ();
294-
sys_wcstombs (rpath, NT_MAX_PATH, nro->lpRemoteName);
294+
sys_wcstombs_path (rpath, NT_MAX_PATH, nro->lpRemoteName);
295295
de->d_ino = readdir_get_ino (rpath, false);
296296
/* We can't trust remote inode numbers of only 32 bit. That means,
297297
remote NT4 NTFS, as well as shares of Samba version < 3.0. */

winsup/cygwin/fhandler/process.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,10 @@ static off_t
574574
format_process_winexename (void *data, char *&destbuf)
575575
{
576576
_pinfo *p = (_pinfo *) data;
577-
size_t len = sys_wcstombs (NULL, 0, p->progname);
577+
size_t len = sys_wcstombs_path (NULL, 0, p->progname);
578578
destbuf = (char *) crealloc_abort (destbuf, len + 1);
579579
/* With trailing \0 for backward compat reasons. */
580-
sys_wcstombs (destbuf, len + 1, p->progname);
580+
sys_wcstombs_path (destbuf, len + 1, p->progname);
581581
return len;
582582
}
583583

@@ -1051,7 +1051,7 @@ format_process_maps (void *data, char *&destbuf)
10511051
drive_maps.fixup_if_match (msi->SectionFileName.Buffer);
10521052
if (mount_table->conv_to_posix_path (dosname,
10531053
posix_modname, 0))
1054-
sys_wcstombs (posix_modname, NT_MAX_PATH, dosname);
1054+
sys_wcstombs_path (posix_modname, NT_MAX_PATH, dosname);
10551055
stat (posix_modname, &st);
10561056
}
10571057
else if (!threads.fill_if_match (cur.abase, mb.Type,
@@ -1108,7 +1108,7 @@ format_process_stat (void *data, char *&destbuf)
11081108
else
11091109
{
11101110
PWCHAR last_slash = wcsrchr (p->progname, L'\\');
1111-
sys_wcstombs (cmd, NAME_MAX + 1,
1111+
sys_wcstombs_path (cmd, NAME_MAX + 1,
11121112
last_slash ? last_slash + 1 : p->progname);
11131113
int len = strlen (cmd);
11141114
if (len > 4)
@@ -1215,7 +1215,8 @@ format_process_status (void *data, char *&destbuf)
12151215
bool fetch_siginfo = false;
12161216

12171217
PWCHAR last_slash = wcsrchr (p->progname, L'\\');
1218-
sys_wcstombs (cmd, NAME_MAX + 1, last_slash ? last_slash + 1 : p->progname);
1218+
sys_wcstombs_path (cmd, NAME_MAX + 1,
1219+
last_slash ? last_slash + 1 : p->progname);
12191220
int len = strlen (cmd);
12201221
if (len > 4)
12211222
{

winsup/cygwin/fhandler/procsys.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ fhandler_procsys::fill_filebuf ()
236236
NtClose (h);
237237
if (!NT_SUCCESS (status))
238238
goto unreadable;
239-
len = sys_wcstombs (NULL, 0, target.Buffer, target.Length / sizeof (WCHAR));
239+
len = sys_wcstombs_path (NULL, 0,
240+
target.Buffer, target.Length / sizeof (WCHAR));
240241
filebuf = (char *) crealloc_abort (filebuf, procsys_len + len + 1);
241-
sys_wcstombs (fnamep = stpcpy (filebuf, procsys), len + 1, target.Buffer,
242-
target.Length / sizeof (WCHAR));
242+
sys_wcstombs_path (fnamep = stpcpy (filebuf, procsys), len + 1,
243+
target.Buffer, target.Length / sizeof (WCHAR));
243244
while ((fnamep = strchr (fnamep, '\\')))
244245
*fnamep = '/';
245246
return true;
@@ -377,8 +378,8 @@ fhandler_procsys::readdir (DIR *dir, dirent *de)
377378
res = ENMFILE;
378379
else
379380
{
380-
sys_wcstombs (de->d_name, NAME_MAX + 1, dbi->ObjectName.Buffer,
381-
dbi->ObjectName.Length / sizeof (WCHAR));
381+
sys_wcstombs_path (de->d_name, NAME_MAX + 1, dbi->ObjectName.Buffer,
382+
dbi->ObjectName.Length / sizeof (WCHAR));
382383
de->d_ino = hash_path_name (get_ino (), de->d_name);
383384
if (RtlEqualUnicodeString (&dbi->ObjectTypeName, &ro_u_natdir, FALSE))
384385
de->d_type = DT_DIR;

winsup/cygwin/local_includes/wchar.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,29 @@ extern size_t _sys_wcstombs_alloc (char **dst_p, int type, const wchar_t *src,
173173
size_t nwc, bool is_path);
174174

175175
static inline size_t
176-
sys_wcstombs (char *dst, size_t len, const wchar_t * src,
177-
size_t nwc = (size_t) -1)
176+
sys_wcstombs_path (char *dst, size_t len, const wchar_t * src,
177+
size_t nwc = (size_t) -1)
178178
{
179179
return _sys_wcstombs (dst, len, src, nwc, true);
180180
}
181181

182182
static inline size_t
183-
sys_wcstombs_no_path (char *dst, size_t len, const wchar_t * src,
184-
size_t nwc = (size_t) -1)
183+
sys_wcstombs (char *dst, size_t len, const wchar_t * src,
184+
size_t nwc = (size_t) -1)
185185
{
186186
return _sys_wcstombs (dst, len, src, nwc, false);
187187
}
188188

189189
static inline size_t
190-
sys_wcstombs_alloc (char **dst_p, int type, const wchar_t *src,
191-
size_t nwc = (size_t) -1)
190+
sys_wcstombs_alloc_path (char **dst_p, int type, const wchar_t *src,
191+
size_t nwc = (size_t) -1)
192192
{
193193
return _sys_wcstombs_alloc (dst_p, type, src, nwc, true);
194194
}
195195

196196
static inline size_t
197-
sys_wcstombs_alloc_no_path (char **dst_p, int type, const wchar_t *src,
198-
size_t nwc = (size_t) -1)
197+
sys_wcstombs_alloc (char **dst_p, int type, const wchar_t *src,
198+
size_t nwc = (size_t) -1)
199199
{
200200
return _sys_wcstombs_alloc (dst_p, type, src, nwc, false);
201201
}

0 commit comments

Comments
 (0)