Skip to content

Commit bfd65a4

Browse files
committed
Make paths' WCS->MBS conversion explicit
* dcrt0.cc (dll_crt0_1), dtable.cc (handle_to_fn), environ.cc (environ_init, getwinenveq, build_env), external.cc (fillout_pinfo), fhandler_disk_file.cc (__DIR_mounts::eval_ino, fhandler_disk_file::readdir_helper), fhandler_netdrive.cc (fhandler_netdrive::readdir), fhandler_process.cc (format_process_winexename, format_process_maps, format_process_stat, format_process_status), fhandler_procsys.cc (fill_filebuf, fhandler_procsys::readdir), mount.cc (fs_info::update, mount_info::create_root_entry, mount_info::conv_to_posix_path, mount_info::from_fstab_line), nlsfuncs.cc (internal_setlocale), path.cc (path_conv::check, sysmlink_info::check_shortcut, symlink_info::check_sysfile, symlink_info::check_reparse_point, symlink_info::check_nfs_symlink, cygwin_conv_path, cygwin_conv_path_list, cwdstuff::get_error_desc, cwdstuff::get), strfuncs.cc (sys_wcstombs_no_path, sys_wcstombs_alloc_no_path), uinfo.cc (ontherange, fetch_from_path, cygheap_pwdgrp::get_home, cygheap_pwdgrp::get_shell, cygheap_pwdgrp::get_gecos), wchar.h (sys_wcstombs_no_path, sys_wcstombs_alloc_no_path): Convert call sites of the sys_wcstombs*() family to specify explicitly when the parameter refers to a path or file name, to avoid future misconversions. Detailed explanation: The sys_wcstombs() function contains special handling for paths/file names, to work around file name restriction on Windows that are unexpected in the POSIX context of Cygwin. We actually do not want that special handling for WCS strings that do *not* refer to paths or file names. Neither do we want to convert those special file names unless they come from inside Cygwin: if the source of the string value is the Windows API, we *know* it cannot be such a special file name because Windows itself would not be able to handle it in the way Cygwin does. So let's switch the previous sys_wcstombs()/sys_wcstombs_no_path() (and the *_alloc* variant) around to sys_wcstombs_path()/sys_wcstombs(). We do this for several reasons: - whenever a call site wants to convert a WCS representation of a path or file name to an MBS one, it should be made very clear that we *want* the special file name conversion to happen. - it is shorter to read and write. - future calls to sys_wcstombs() will not incur unwanted conversion by accident (it is easy for unsuspecting programmers to assume that the function name "sys_wcstombs()" refers to a regular text conversion that has nothing to do with paths or filenames). By keeping the name sys_wcstombs() (and not switching to sys_wcstombs_path()), the following call sites are implicitly changed to *exclude* the special path/file name conversion: cygheap.h (get_drive): Cannot contain special characters dcrt0.cc (dll_crt0_1): Is input from outside of Cygwin environ.cc (getwinenveq): Is input from outside of Cygwin external.cc (cygwin_internal): Refers to user/domain names, not paths fhandler_clipboard.cc (fhandler_dev_clipboard::read): Is not a path or file name but characters from the Windows clipboard fhandler_console.cc: (dev_console::con_to_str): Is not a path or file name but characters from the console fhandler_registry.cc (encode_regname): Is a registry key, not a path or filename fhandler_registry.cc (multi_wcstombs): All call sites pass registry values, not paths or filenames fhandler_registry.cc (fstat): Is a registry value, not a path or filename fhandler_registry.cc (fill_filebuf): Is a registry value, not a path or filename miscfuncs.cc (strlwr): TODO miscfuncs.cc (strupr): TODO net.cc (get_ipv4fromreg): Is a registry value, not a path or filename net.cc (get_friendlyname): Is a device name, not a path or filename netdb.cc (open_system_file): Is from outside Cygwin smallprint.cc (__small_vsprintf): Is a free text, not a path or filename uinfo.cc (cygheap_user::init): Refers to a user name, not a path or filename uinfo.cc (pwdgrp::fetch_account_from_windows): Refers to value from outside Cygwin By keeping the function name sys_wcstombs_alloc() (and not changing it to sys_wcstombs_alloc_path()), the following call sites are implicitly changed to *exclude* the special path/file name conversion: environ.cc (environ_init): Is input from outside of Cygwin environ.cc (build_env): Refers to the environment from outside Cygwin ldap.cc (cyg_ldap::remap_uid): Refers to a user name, not a path or filename ldap.cc (cyg_ldap::remap_gid): Refers to a group name, not a path or filename pinfo.cc (_pinfo::cmdline): Refers to a command line from Windows, outside Cygwin uinfo.cc (cygheap_user::env_logsrv): Is a server name, not a path or filename uinfo.cc (cygheap_user::env_domain): Refers to the user/domain name, not a path or filename uinfo.cc (cygheap_user::env_userprofile): Refers to Windows' idea of a path, outside Cygwin uinfo.cc (cygheap_user::env_systemroot): Refers to Windows' idea of a path, outside Cygwin uinfo.cc (fetch_from_description): Refers to values from outside of Cygwin uinfo.cc (cygheap_pwdgrp::get_gecos): Refers to user/domain name and email address, not path nor filename Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 501d56d commit bfd65a4

File tree

14 files changed

+62
-56
lines changed

14 files changed

+62
-56
lines changed

winsup/cygwin/dcrt0.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,9 +935,9 @@ dll_crt0_1 (void *)
935935
if (!__argc)
936936
{
937937
PWCHAR wline = GetCommandLineW ();
938-
size_t size = sys_wcstombs_no_path (NULL, 0, wline) + 1;
938+
size_t size = sys_wcstombs (NULL, 0, wline) + 1;
939939
char *line = (char *) alloca (size);
940-
sys_wcstombs_no_path (line, size, wline);
940+
sys_wcstombs (line, size, wline);
941941

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

winsup/cygwin/dtable.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ handle_to_fn (HANDLE h, char *posix_fn)
995995
if (wcsncasecmp (w32, DEVICE_PREFIX, DEVICE_PREFIX_LEN) != 0
996996
|| !QueryDosDeviceW (NULL, fnbuf, sizeof (fnbuf) / sizeof (WCHAR)))
997997
{
998-
sys_wcstombs (posix_fn, NT_MAX_PATH, w32, w32len);
998+
sys_wcstombs_path (posix_fn, NT_MAX_PATH, w32, w32len);
999999
return false;
10001000
}
10011001

winsup/cygwin/environ.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ environ_init (char **envp, int envc)
829829
eventually want to use them). */
830830
for (i = 0, w = rawenv; *w != L'\0'; w = wcschr (w, L'\0') + 1, i++)
831831
{
832-
sys_wcstombs_alloc_no_path (&newp, HEAP_NOTHEAP, w);
832+
sys_wcstombs_alloc (&newp, HEAP_NOTHEAP, w);
833833
if (i >= envc)
834834
envp = (char **) realloc (envp, (4 + (envc += 100)) * sizeof (char *));
835835
envp[i] = newp;
@@ -908,15 +908,15 @@ getwinenveq (const char *name, size_t namelen, int x)
908908
int totlen = GetEnvironmentVariableW (name0, valbuf, 32768);
909909
if (totlen > 0)
910910
{
911-
totlen = sys_wcstombs_no_path (NULL, 0, valbuf) + 1;
911+
totlen = sys_wcstombs (NULL, 0, valbuf) + 1;
912912
if (x == HEAP_1_STR)
913913
totlen += namelen;
914914
else
915915
namelen = 0;
916916
char *p = (char *) cmalloc_abort ((cygheap_types) x, totlen);
917917
if (namelen)
918918
strcpy (p, name);
919-
sys_wcstombs_no_path (p + namelen, totlen, valbuf);
919+
sys_wcstombs (p + namelen, totlen, valbuf);
920920
debug_printf ("using value from GetEnvironmentVariable for '%W'", name0);
921921
return p;
922922
}
@@ -1069,7 +1069,7 @@ build_env (const char * const *envp, PWCHAR &envblock, int &envc,
10691069
for (winnum = 0, var = cwinenv;
10701070
*var;
10711071
++winnum, var = wcschr (var, L'\0') + 1)
1072-
sys_wcstombs_alloc_no_path (&winenv[winnum], HEAP_NOTHEAP, var);
1072+
sys_wcstombs_alloc (&winenv[winnum], HEAP_NOTHEAP, var);
10731073
}
10741074
DestroyEnvironmentBlock (cwinenv);
10751075
/* 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
@@ -88,7 +88,7 @@ fillout_pinfo (pid_t pid, int winpid)
8888
ep.rusage_self = p->rusage_self;
8989
ep.rusage_children = p->rusage_children;
9090
ep.progname[0] = '\0';
91-
sys_wcstombs(ep.progname, MAX_PATH, p->progname);
91+
sys_wcstombs_path (ep.progname, MAX_PATH, p->progname);
9292
ep.strace_mask = 0;
9393
ep.version = EXTERNAL_PINFO_VERSION;
9494

winsup/cygwin/fhandler_disk_file.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class __DIR_mounts
5050
char *c = stpcpy (fname, parent_dir);
5151
if (c[- 1] != '/')
5252
*c++ = '/';
53-
sys_wcstombs (c, mounts[idx].Length + 1,
53+
sys_wcstombs_path (c, mounts[idx].Length + 1,
5454
mounts[idx].Buffer, mounts[idx].Length / sizeof (WCHAR));
5555
path_conv pc (fname, PC_SYM_NOFOLLOW | PC_POSIX | PC_KEEP_HANDLE);
5656
if (!stat_worker (pc, &st))
@@ -2057,7 +2057,7 @@ fhandler_disk_file::readdir_helper (DIR *dir, dirent *de, DWORD w32_err,
20572057
char *p = stpcpy (file, pc.get_posix ());
20582058
if (p[-1] != '/')
20592059
*p++ = '/';
2060-
sys_wcstombs (p, NT_MAX_PATH - (p - file),
2060+
sys_wcstombs_path (p, NT_MAX_PATH - (p - file),
20612061
fname->Buffer, fname->Length / sizeof (WCHAR));
20622062
path_conv fpath (file, PC_SYM_NOFOLLOW);
20632063
if (fpath.issymlink ())
@@ -2078,7 +2078,7 @@ fhandler_disk_file::readdir_helper (DIR *dir, dirent *de, DWORD w32_err,
20782078
}
20792079
}
20802080

2081-
sys_wcstombs (de->d_name, NAME_MAX + 1, fname->Buffer,
2081+
sys_wcstombs_path (de->d_name, NAME_MAX + 1, fname->Buffer,
20822082
fname->Length / sizeof (WCHAR));
20832083

20842084
/* 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
@@ -252,15 +252,15 @@ fhandler_netdrive::readdir (DIR *dir, dirent *de)
252252
tp.u_get (&ds);
253253
RtlInitUnicodeString (&ss, bs);
254254
RtlDowncaseUnicodeString (&ds, &ss, FALSE);
255-
sys_wcstombs (de->d_name, sizeof de->d_name,
255+
sys_wcstombs_path (de->d_name, sizeof de->d_name,
256256
ds.Buffer, ds.Length / sizeof (WCHAR));
257257
de->d_ino = hash_path_name (get_ino (), de->d_name);
258258
}
259259
else
260260
{
261-
sys_wcstombs (de->d_name, sizeof de->d_name, bs);
261+
sys_wcstombs_path (de->d_name, sizeof de->d_name, bs);
262262
char *rpath = tp.c_get ();
263-
sys_wcstombs (rpath, NT_MAX_PATH, nro->lpRemoteName);
263+
sys_wcstombs_path (rpath, NT_MAX_PATH, nro->lpRemoteName);
264264
de->d_ino = readdir_get_ino (rpath, false);
265265
/* We can't trust remote inode numbers of only 32 bit. That means,
266266
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
@@ -563,10 +563,10 @@ static off_t
563563
format_process_winexename (void *data, char *&destbuf)
564564
{
565565
_pinfo *p = (_pinfo *) data;
566-
size_t len = sys_wcstombs (NULL, 0, p->progname);
566+
size_t len = sys_wcstombs_path (NULL, 0, p->progname);
567567
destbuf = (char *) crealloc_abort (destbuf, len + 1);
568568
/* With trailing \0 for backward compat reasons. */
569-
sys_wcstombs (destbuf, len + 1, p->progname);
569+
sys_wcstombs_path (destbuf, len + 1, p->progname);
570570
return len;
571571
}
572572

@@ -1006,7 +1006,7 @@ format_process_maps (void *data, char *&destbuf)
10061006
drive_maps.fixup_if_match (msi->SectionFileName.Buffer);
10071007
if (mount_table->conv_to_posix_path (dosname,
10081008
posix_modname, 0))
1009-
sys_wcstombs (posix_modname, NT_MAX_PATH, dosname);
1009+
sys_wcstombs_path (posix_modname, NT_MAX_PATH, dosname);
10101010
stat64 (posix_modname, &st);
10111011
}
10121012
else if (!threads.fill_if_match (cur.abase, mb.Type,
@@ -1052,7 +1052,7 @@ format_process_stat (void *data, char *&destbuf)
10521052
else
10531053
{
10541054
PWCHAR last_slash = wcsrchr (p->progname, L'\\');
1055-
sys_wcstombs (cmd, NAME_MAX + 1,
1055+
sys_wcstombs_path (cmd, NAME_MAX + 1,
10561056
last_slash ? last_slash + 1 : p->progname);
10571057
int len = strlen (cmd);
10581058
if (len > 4)
@@ -1170,7 +1170,8 @@ format_process_status (void *data, char *&destbuf)
11701170
vmtext = 0UL, vmshare = 0UL;
11711171

11721172
PWCHAR last_slash = wcsrchr (p->progname, L'\\');
1173-
sys_wcstombs (cmd, NAME_MAX + 1, last_slash ? last_slash + 1 : p->progname);
1173+
sys_wcstombs_path (cmd, NAME_MAX + 1,
1174+
last_slash ? last_slash + 1 : p->progname);
11741175
int len = strlen (cmd);
11751176
if (len > 4)
11761177
{

winsup/cygwin/fhandler_procsys.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ fhandler_procsys::fill_filebuf ()
241241
NtClose (h);
242242
if (!NT_SUCCESS (status))
243243
goto unreadable;
244-
len = sys_wcstombs (NULL, 0, target.Buffer, target.Length / sizeof (WCHAR));
244+
len = sys_wcstombs_path (NULL, 0,
245+
target.Buffer, target.Length / sizeof (WCHAR));
245246
filebuf = (char *) crealloc_abort (filebuf, procsys_len + len + 1);
246-
sys_wcstombs (fnamep = stpcpy (filebuf, procsys), len + 1, target.Buffer,
247-
target.Length / sizeof (WCHAR));
247+
sys_wcstombs_path (fnamep = stpcpy (filebuf, procsys), len + 1,
248+
target.Buffer, target.Length / sizeof (WCHAR));
248249
while ((fnamep = strchr (fnamep, '\\')))
249250
*fnamep = '/';
250251
return true;
@@ -353,7 +354,7 @@ fhandler_procsys::readdir (DIR *dir, dirent *de)
353354
res = ENMFILE;
354355
else
355356
{
356-
sys_wcstombs (de->d_name, NAME_MAX + 1, f.dbi.ObjectName.Buffer,
357+
sys_wcstombs_path (de->d_name, NAME_MAX + 1, f.dbi.ObjectName.Buffer,
357358
f.dbi.ObjectName.Length / sizeof (WCHAR));
358359
de->d_ino = hash_path_name (get_ino (), de->d_name);
359360
if (RtlEqualUnicodeString (&f.dbi.ObjectTypeName, &ro_u_natdir,

winsup/cygwin/mount.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fs_info::update (PUNICODE_STRING upath, HANDLE in_vol)
435435
{
436436
/* The filesystem name is only used in fillout_mntent and only if
437437
the filesystem isn't one of the well-known filesystems anyway. */
438-
sys_wcstombs (fsn, sizeof fsn, ffai_buf.ffai.FileSystemName,
438+
sys_wcstombs_path (fsn, sizeof fsn, ffai_buf.ffai.FileSystemName,
439439
ffai_buf.ffai.FileSystemNameLength / sizeof (WCHAR));
440440
strlwr (fsn);
441441
}
@@ -462,7 +462,7 @@ mount_info::create_root_entry (const PWCHAR root)
462462
/* Create a default root dir derived from the location of the Cygwin DLL.
463463
The entry is immutable, unless the "override" option is given in /etc/fstab. */
464464
char native_root[PATH_MAX];
465-
sys_wcstombs (native_root, PATH_MAX, root);
465+
sys_wcstombs_path (native_root, PATH_MAX, root);
466466
assert (*native_root != '\0');
467467
if (add_item (native_root, "/",
468468
MOUNT_SYSTEM | MOUNT_BINARY | MOUNT_IMMUTABLE | MOUNT_AUTOMATIC | MOUNT_NOACL)
@@ -858,7 +858,7 @@ mount_info::conv_to_posix_path (PWCHAR src_path, char *posix_path,
858858
}
859859
tmp_pathbuf tp;
860860
char *buf = tp.c_get ();
861-
sys_wcstombs (buf, NT_MAX_PATH, src_path);
861+
sys_wcstombs_path (buf, NT_MAX_PATH, src_path);
862862
int ret = conv_to_posix_path (buf, posix_path, ccp_flags);
863863
if (changed)
864864
src_path[0] = L'C';
@@ -1204,7 +1204,7 @@ mount_info::from_fstab_line (char *line, bool user)
12041204
{
12051205
tmp_pathbuf tp;
12061206
char *mb_tmp = tp.c_get ();
1207-
sys_wcstombs (mb_tmp, PATH_MAX, tmp);
1207+
sys_wcstombs_path (mb_tmp, PATH_MAX, tmp);
12081208

12091209
mount_flags |= MOUNT_USER_TEMP;
12101210
int res = mount_table->add_item (mb_tmp, posix_path, mount_flags);

winsup/cygwin/nlsfuncs.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ internal_setlocale ()
15501550
if (w_path)
15511551
{
15521552
char *c_path = tp.c_get ();
1553-
sys_wcstombs (c_path, 32768, w_path);
1553+
sys_wcstombs_path (c_path, 32768, w_path);
15541554
setenv ("PATH", c_path, 1);
15551555
}
15561556
}

0 commit comments

Comments
 (0)