Fix length returned from sys_cp_wcstombs in case nwc > # of wchars

* strfuncs.cc (sys_cp_wcstombs): Always return number of multibytes
	without trailing NUL as the documentation implies.  Throughout Cygwin,
	fix usage to align to this pattern.
	* fhandler_process.cc (format_process_winexename): Drop trailing NUL
	and LF from output.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2015-10-22 14:22:07 +02:00
parent c0345822e5
commit ef75017378
9 changed files with 23 additions and 14 deletions

View File

@ -1,3 +1,11 @@
2015-10-22 Corinna Vinschen <corinna@vinschen.de>
* strfuncs.cc (sys_cp_wcstombs): Always return number of multibytes
without trailing NUL as the documentation implies. Throughout Cygwin,
fix usage to align to this pattern.
* fhandler_process.cc (format_process_winexename): Drop trailing NUL
and LF from output.
2015-10-21 Corinna Vinschen <corinna@vinschen.de> 2015-10-21 Corinna Vinschen <corinna@vinschen.de>
* thread.cc (pthread_getattr_np): Fix memory leak, remove usage of * thread.cc (pthread_getattr_np): Fix memory leak, remove usage of

View File

@ -952,7 +952,7 @@ dll_crt0_1 (void *)
if (!__argc) if (!__argc)
{ {
PWCHAR wline = GetCommandLineW (); PWCHAR wline = GetCommandLineW ();
size_t size = sys_wcstombs (NULL, 0, wline); size_t size = sys_wcstombs (NULL, 0, wline) + 1;
char *line = (char *) alloca (size); char *line = (char *) alloca (size);
sys_wcstombs (line, size, wline); sys_wcstombs (line, size, wline);

View File

@ -895,7 +895,7 @@ getwinenveq (const char *name, size_t namelen, int x)
int totlen = GetEnvironmentVariableW (name0, valbuf, 32768); int totlen = GetEnvironmentVariableW (name0, valbuf, 32768);
if (totlen > 0) if (totlen > 0)
{ {
totlen = sys_wcstombs (NULL, 0, valbuf); totlen = sys_wcstombs (NULL, 0, valbuf) + 1;
if (x == HEAP_1_STR) if (x == HEAP_1_STR)
totlen += namelen; totlen += namelen;
else else

View File

@ -568,9 +568,9 @@ format_process_winexename (void *data, char *&destbuf)
_pinfo *p = (_pinfo *) data; _pinfo *p = (_pinfo *) data;
size_t len = sys_wcstombs (NULL, 0, p->progname); size_t len = sys_wcstombs (NULL, 0, p->progname);
destbuf = (char *) crealloc_abort (destbuf, len + 1); destbuf = (char *) crealloc_abort (destbuf, len + 1);
sys_wcstombs (destbuf, len, p->progname); /* With trailing \0 for backward compat reasons. */
destbuf[len] = '\n'; sys_wcstombs (destbuf, len + 1, p->progname);
return len + 1; return len;
} }
struct heap_info struct heap_info

View File

@ -286,7 +286,7 @@ multi_wcstombs (char *dst, size_t len, const wchar_t *src, size_t nwc)
while (nwc) while (nwc)
{ {
siz = sys_wcstombs (dst, len, src, nwc); siz = sys_wcstombs (dst, len, src, nwc) + 1;
sum += siz; sum += siz;
if (dst) if (dst)
{ {
@ -555,7 +555,8 @@ fhandler_registry::fstat (struct stat *buf)
else else
buf->st_size = sys_wcstombs (NULL, 0, buf->st_size = sys_wcstombs (NULL, 0,
(wchar_t *) tmpbuf, (wchar_t *) tmpbuf,
dwSize / sizeof (wchar_t)); dwSize / sizeof (wchar_t))
+ 1;
if (tmpbuf) if (tmpbuf)
free (tmpbuf); free (tmpbuf);
} }
@ -972,7 +973,7 @@ fhandler_registry::fill_filebuf ()
} }
if (type == REG_SZ || type == REG_EXPAND_SZ || type == REG_LINK) if (type == REG_SZ || type == REG_EXPAND_SZ || type == REG_LINK)
bufalloc = sys_wcstombs (NULL, 0, (wchar_t *) tmpbuf, bufalloc = sys_wcstombs (NULL, 0, (wchar_t *) tmpbuf,
size / sizeof (wchar_t)); size / sizeof (wchar_t)) + 1;
else if (type == REG_MULTI_SZ) else if (type == REG_MULTI_SZ)
bufalloc = multi_wcstombs (NULL, 0, (wchar_t *) tmpbuf, bufalloc = multi_wcstombs (NULL, 0, (wchar_t *) tmpbuf,
size / sizeof (wchar_t)); size / sizeof (wchar_t));

View File

@ -2107,7 +2107,7 @@ get_friendlyname (struct ifall *ifp, PIP_ADAPTER_ADDRESSES pap)
&ifp->ifa_frndlyname; &ifp->ifa_frndlyname;
iff->ifrf_len = sys_wcstombs (iff->ifrf_friendlyname, iff->ifrf_len = sys_wcstombs (iff->ifrf_friendlyname,
IFRF_FRIENDLYNAMESIZ, IFRF_FRIENDLYNAMESIZ,
pap->FriendlyName); pap->FriendlyName) + 1;
} }
static void static void

View File

@ -2167,7 +2167,7 @@ symlink_info::check_shortcut (HANDLE h)
{ {
char *tmpbuf = tp.c_get (); char *tmpbuf = tp.c_get ();
if (sys_wcstombs (tmpbuf, NT_MAX_PATH, (PWCHAR) (cp + 2)) if (sys_wcstombs (tmpbuf, NT_MAX_PATH, (PWCHAR) (cp + 2))
> SYMLINK_MAX + 1) > SYMLINK_MAX)
return 0; return 0;
res = posixify (tmpbuf); res = posixify (tmpbuf);
} }
@ -2248,7 +2248,7 @@ symlink_info::check_sysfile (HANDLE h)
srcbuf += 2; srcbuf += 2;
char *tmpbuf = tp.c_get (); char *tmpbuf = tp.c_get ();
if (sys_wcstombs (tmpbuf, NT_MAX_PATH, (PWCHAR) srcbuf) if (sys_wcstombs (tmpbuf, NT_MAX_PATH, (PWCHAR) srcbuf)
> SYMLINK_MAX + 1) > SYMLINK_MAX)
debug_printf ("symlink string too long"); debug_printf ("symlink string too long");
else else
res = posixify (tmpbuf); res = posixify (tmpbuf);
@ -2368,7 +2368,7 @@ symlink_info::check_nfs_symlink (HANDLE h)
PWCHAR spath = (PWCHAR) PWCHAR spath = (PWCHAR)
(pffei->EaName + pffei->EaNameLength + 1); (pffei->EaName + pffei->EaNameLength + 1);
res = sys_wcstombs (contents, SYMLINK_MAX + 1, res = sys_wcstombs (contents, SYMLINK_MAX + 1,
spath, pffei->EaValueLength) - 1; spath, pffei->EaValueLength);
pflags |= PATH_SYMLINK; pflags |= PATH_SYMLINK;
} }
return res; return res;

View File

@ -474,7 +474,6 @@ sys_cp_wcstombs (wctomb_p f_wctomb, const char *charset, char *dst, size_t len,
} }
if (n + bytes <= len) if (n + bytes <= len)
{ {
n += bytes;
if (dst) if (dst)
{ {
for (int i = 0; i < bytes; ++i) for (int i = 0; i < bytes; ++i)
@ -482,6 +481,7 @@ sys_cp_wcstombs (wctomb_p f_wctomb, const char *charset, char *dst, size_t len,
} }
if (*pwcs++ == 0x00) if (*pwcs++ == 0x00)
break; break;
n += bytes;
} }
else else
break; break;

View File

@ -56,7 +56,7 @@ cygheap_user::init ()
if (GetEnvironmentVariableW (L"USERNAME", user_name, user_name_len) if (GetEnvironmentVariableW (L"USERNAME", user_name, user_name_len)
|| GetEnvironmentVariableW (L"USER", user_name, user_name_len)) || GetEnvironmentVariableW (L"USER", user_name, user_name_len))
{ {
char mb_user_name[user_name_len = sys_wcstombs (NULL, 0, user_name)]; char mb_user_name[user_name_len = sys_wcstombs (NULL, 0, user_name) + 1];
sys_wcstombs (mb_user_name, user_name_len, user_name); sys_wcstombs (mb_user_name, user_name_len, user_name);
set_name (mb_user_name); set_name (mb_user_name);
} }