https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=ef75017378c2b6ae62cb8bdb196a4d188302f930

commit ef75017378c2b6ae62cb8bdb196a4d188302f930
Author: Corinna Vinschen <cori...@vinschen.de>
Date:   Thu Oct 22 14:22:07 2015 +0200

    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 <cori...@vinschen.de>

Diff:
---
 winsup/cygwin/ChangeLog            | 8 ++++++++
 winsup/cygwin/dcrt0.cc             | 2 +-
 winsup/cygwin/environ.cc           | 2 +-
 winsup/cygwin/fhandler_process.cc  | 6 +++---
 winsup/cygwin/fhandler_registry.cc | 7 ++++---
 winsup/cygwin/net.cc               | 2 +-
 winsup/cygwin/path.cc              | 6 +++---
 winsup/cygwin/strfuncs.cc          | 2 +-
 winsup/cygwin/uinfo.cc             | 2 +-
 9 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 4cbbd0b..7cd19b5 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,11 @@
+2015-10-22  Corinna Vinschen  <cori...@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  <cori...@vinschen.de>
 
        * thread.cc (pthread_getattr_np): Fix memory leak, remove usage of
diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index 8ac7f4c..3d293f6 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -952,7 +952,7 @@ dll_crt0_1 (void *)
   if (!__argc)
     {
       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);
       sys_wcstombs (line, size, wline);
 
diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
index 8f25fb1..ab6511b 100644
--- a/winsup/cygwin/environ.cc
+++ b/winsup/cygwin/environ.cc
@@ -895,7 +895,7 @@ getwinenveq (const char *name, size_t namelen, int x)
   int totlen = GetEnvironmentVariableW (name0, valbuf, 32768);
   if (totlen > 0)
     {
-      totlen = sys_wcstombs (NULL, 0, valbuf);
+      totlen = sys_wcstombs (NULL, 0, valbuf) + 1;
       if (x == HEAP_1_STR)
        totlen += namelen;
       else
diff --git a/winsup/cygwin/fhandler_process.cc 
b/winsup/cygwin/fhandler_process.cc
index d3ee874..516fbe3 100644
--- a/winsup/cygwin/fhandler_process.cc
+++ b/winsup/cygwin/fhandler_process.cc
@@ -568,9 +568,9 @@ format_process_winexename (void *data, char *&destbuf)
   _pinfo *p = (_pinfo *) data;
   size_t len = sys_wcstombs (NULL, 0, p->progname);
   destbuf = (char *) crealloc_abort (destbuf, len + 1);
-  sys_wcstombs (destbuf, len, p->progname);
-  destbuf[len] = '\n';
-  return len + 1;
+  /* With trailing \0 for backward compat reasons. */
+  sys_wcstombs (destbuf, len + 1, p->progname);
+  return len;
 }
 
 struct heap_info
diff --git a/winsup/cygwin/fhandler_registry.cc 
b/winsup/cygwin/fhandler_registry.cc
index fbdb440..6663637 100644
--- a/winsup/cygwin/fhandler_registry.cc
+++ b/winsup/cygwin/fhandler_registry.cc
@@ -286,7 +286,7 @@ multi_wcstombs (char *dst, size_t len, const wchar_t *src, 
size_t nwc)
 
   while (nwc)
     {
-      siz = sys_wcstombs (dst, len, src, nwc);
+      siz = sys_wcstombs (dst, len, src, nwc) + 1;
       sum += siz;
       if (dst)
        {
@@ -555,7 +555,8 @@ fhandler_registry::fstat (struct stat *buf)
                      else
                        buf->st_size = sys_wcstombs (NULL, 0,
                                                     (wchar_t *) tmpbuf,
-                                                    dwSize / sizeof (wchar_t));
+                                                    dwSize / sizeof (wchar_t))
+                                      + 1;
                      if (tmpbuf)
                        free (tmpbuf);
                    }
@@ -972,7 +973,7 @@ fhandler_registry::fill_filebuf ()
        }
       if (type == REG_SZ || type == REG_EXPAND_SZ || type == REG_LINK)
        bufalloc = sys_wcstombs (NULL, 0, (wchar_t *) tmpbuf,
-                                size / sizeof (wchar_t));
+                                size / sizeof (wchar_t)) + 1;
       else if (type == REG_MULTI_SZ)
        bufalloc = multi_wcstombs (NULL, 0, (wchar_t *) tmpbuf,
                                   size / sizeof (wchar_t));
diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc
index 20b4d3c..0f3946a 100644
--- a/winsup/cygwin/net.cc
+++ b/winsup/cygwin/net.cc
@@ -2107,7 +2107,7 @@ get_friendlyname (struct ifall *ifp, 
PIP_ADAPTER_ADDRESSES pap)
                                 &ifp->ifa_frndlyname;
   iff->ifrf_len = sys_wcstombs (iff->ifrf_friendlyname,
                                IFRF_FRIENDLYNAMESIZ,
-                               pap->FriendlyName);
+                               pap->FriendlyName) + 1;
 }
 
 static void
diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index 488d4e6..54c96fe 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -2167,7 +2167,7 @@ symlink_info::check_shortcut (HANDLE h)
        {
          char *tmpbuf = tp.c_get ();
          if (sys_wcstombs (tmpbuf, NT_MAX_PATH, (PWCHAR) (cp + 2))
-             > SYMLINK_MAX + 1)
+             > SYMLINK_MAX)
            return 0;
          res = posixify (tmpbuf);
        }
@@ -2248,7 +2248,7 @@ symlink_info::check_sysfile (HANDLE h)
            srcbuf += 2;
          char *tmpbuf = tp.c_get ();
          if (sys_wcstombs (tmpbuf, NT_MAX_PATH, (PWCHAR) srcbuf)
-             > SYMLINK_MAX + 1)
+             > SYMLINK_MAX)
            debug_printf ("symlink string too long");
          else
            res = posixify (tmpbuf);
@@ -2368,7 +2368,7 @@ symlink_info::check_nfs_symlink (HANDLE h)
       PWCHAR spath = (PWCHAR)
                     (pffei->EaName + pffei->EaNameLength + 1);
       res = sys_wcstombs (contents, SYMLINK_MAX + 1,
-                         spath, pffei->EaValueLength) - 1;
+                         spath, pffei->EaValueLength);
       pflags |= PATH_SYMLINK;
     }
   return res;
diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index 94ce82c..0be0f53 100644
--- a/winsup/cygwin/strfuncs.cc
+++ b/winsup/cygwin/strfuncs.cc
@@ -474,7 +474,6 @@ sys_cp_wcstombs (wctomb_p f_wctomb, const char *charset, 
char *dst, size_t len,
        }
       if (n + bytes <= len)
        {
-         n += bytes;
          if (dst)
            {
              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)
            break;
+         n += bytes;
        }
       else
        break;
diff --git a/winsup/cygwin/uinfo.cc b/winsup/cygwin/uinfo.cc
index da5809f..df7d42f 100644
--- a/winsup/cygwin/uinfo.cc
+++ b/winsup/cygwin/uinfo.cc
@@ -56,7 +56,7 @@ cygheap_user::init ()
   if (GetEnvironmentVariableW (L"USERNAME", 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);
       set_name (mb_user_name);
     }

Reply via email to