[cygutils - Chuck Wilson's collection of Cygwin utilities] branch master, updated. v1_4_16-8-g936dce4

Mark Geisert via Cygwin-apps-cvs <[email protected]>
Newsgroups gmane.os.cygwin.cvs.apps
Message-ID <[email protected]>


https://sourceware.org/git/gitweb.cgi?p=cygwin-apps/cygutils.git;h=936dce44b5799e3fd6926b9faacd9046cf5f2aaa

commit 936dce44b5799e3fd6926b9faacd9046cf5f2aaa
Author: Mark Geisert <[email protected]>
Date:   Fri Sep 24 22:50:44 2021 -0700

    Update to released 1.4.16-7 content

https://sourceware.org/git/gitweb.cgi?p=cygwin-apps/cygutils.git;h=de65716432a2d8f29834149191b0ed276b5cf6fc

commit de65716432a2d8f29834149191b0ed276b5cf6fc
Author: Mark Geisert <[email protected]>
Date:   Fri Sep 24 22:47:04 2021 -0700

    Update to released 1.4.16-6 content

https://sourceware.org/git/gitweb.cgi?p=cygwin-apps/cygutils.git;h=a852972c1db68d41019acd1c2db35c165c63c78f

commit a852972c1db68d41019acd1c2db35c165c63c78f
Author: Mark Geisert <[email protected]>
Date:   Fri Sep 24 22:42:24 2021 -0700

    Update to released 1.4.16-5 content

https://sourceware.org/git/gitweb.cgi?p=cygwin-apps/cygutils.git;h=f7c4555aa83a128efee00202765c59344b565184

commit f7c4555aa83a128efee00202765c59344b565184
Author: Mark Geisert <[email protected]>
Date:   Fri Sep 24 22:37:55 2021 -0700

    Update to released 1.4.16-4 content

https://sourceware.org/git/gitweb.cgi?p=cygwin-apps/cygutils.git;h=49b00c580c21ebdd75f2c112a797755d74400f6d

commit 49b00c580c21ebdd75f2c112a797755d74400f6d
Author: Mark Geisert <[email protected]>
Date:   Fri Sep 24 22:31:49 2021 -0700

    Update to released 1.4.16-3 content


Diff:
---
 ChangeLog                       |  34 +++++++++++
 NEWS                            |   3 +
 cygutils.cygport                |   2 +-
 src/clip/getclip.c              |  63 ++++++++++++++++----
 src/clip/putclip.c              | 125 ++++++++++++++++++++++++++++++++++++++--
 src/cygdrop/cygdrop.cc          |   6 +-
 src/readshortcut/readshortcut.c |   2 +-
 7 files changed, 216 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c67ee00..921b9d0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,37 @@
+2021-09-23  Mark Geisert  <[email protected]>  (1.4.16-7)
+
+	* src/readshortcut/readshortcut.c: Fix mbstowcs usage so no chance
+	of buffer overflow to be caught at runtime by ssp.  Thanks to Keith
+	Christian for the report.
+
+2021-08-28  Mark Geisert  <[email protected]>  (1.4.16-6)
+
+	* src/clip/putclip.c: Supply terminator for Unicode input lacking it.
+	Add diagnostic for OpenClipboard() failure that results in "Couldn't
+	write CF_TEXT format to the clipboard" error message.  Thanks to
+	Takashi Yano for the reports.
+	* Makefile.am: src_clip_getclip_LDADD no longer needs -lkernel32.
+
+2021-08-24  Mark Geisert  <[email protected]>  (1.4.16-5)
+
+	* src/clip/putclip.c: Fix broken Unicode support. Don't GlobalFree a
+	handle that's been transferred to the system by SetClipboardData().
+	Thanks to Christian Franke for that last bit.
+
+2021-08-15  Mark Geisert  <[email protected]>  (1.4.16-4)
+
+	* Makefile.am: Add -lkernel32 to src_clip_getclip_LDADD.  This somehow
+	didn't make it into the released -3 package.  Thank you Takashi Yano.
+	* Fix maintainer build environment issues that caused cygstart crash
+	on non-AVX user systems: Thank you Takashi Yano, Brian Inglis, Achim
+	Gratz and user Jay Abel for reporting the issue.
+	* src/cygdrop/cygdrop.cc: Fix crash with recent gcc and fix printf
+	format strings. Thank you Christian Franke.
+
+2021-07-05  Mark Geisert  <[email protected]>  (1.4.16-3)
+
+	* src/clip/getclip.c: Add Unicode support.
+
 2017-07-03  Mark Geisert  <[email protected]>
 
 	Release 1.4.16
diff --git a/NEWS b/NEWS
index dbc9879..fcfab44 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,9 @@
   * Minor formatting fixes to quash compiler warnings.
   * Execute bits removed from files not needing them.
   * Temporarily rename /usr/lib/libpopt.la while building package.
+  * Unicode support added to getclip and putclip.
+  * Fix mbstowcs bug in readshortcut causing "buffer overflow detected" from
+    -fstack-protect at runtime.
 
 1.4.15
   New maintainer Mark Geisert <[email protected]>.
diff --git a/cygutils.cygport b/cygutils.cygport
index cdbae68..d2ca1f5 100644
--- a/cygutils.cygport
+++ b/cygutils.cygport
@@ -1,6 +1,6 @@
 NAME=cygutils
 VERSION=1.4.16
-RELEASE=2
+RELEASE=7
 
 SUMM="A collection of simple utilities"
 DESC="A collection of simple (single source file) utilities, including"
diff --git a/src/clip/getclip.c b/src/clip/getclip.c
index 4ddbce3..82e7070 100644
--- a/src/clip/getclip.c
+++ b/src/clip/getclip.c
@@ -29,7 +29,11 @@
 #include "common.h"
 
 #include <io.h>
+#include <wchar.h>
 #include <sys/cygwin.h>
+#include <w32api/stringapiset.h>
+
+#define DEBUGGING 0 // Set 1 to display debug output on stderr
 
 typedef struct
 {
@@ -356,16 +360,31 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
     {
       const char *CYGWIN_NATIVE = "CYGWIN_NATIVE_CLIPBOARD";
       UINT cygnativeformat;
-      UINT formatlist[2];
+      UINT formatlist[3];
       UINT format;
 
       OpenClipboard (NULL);
+#if DEBUGGING
+      {
+        fprintf (stderr, "ThreadLocale: %u\nClipboardFormats:",
+                 GetThreadLocale ());
+        format = 0;
+        do {
+          format = EnumClipboardFormats (format);
+          fprintf (stderr, " %u", format);
+          if (format == CF_LOCALE)
+            fprintf (stderr, "(%u)", *(LCID *) GetClipboardData (CF_LOCALE));
+        } while (format != 0);
+        fprintf (stderr, "\n");
+      }
+#endif
       cygnativeformat = RegisterClipboardFormat (CYGWIN_NATIVE);
 
       formatlist[0] = cygnativeformat;
-      formatlist[1] = CF_TEXT;
+      formatlist[1] = CF_UNICODETEXT;
+      formatlist[2] = CF_TEXT;
 
-      if ((format = GetPriorityClipboardFormat (formatlist, 2)) <= 0)
+      if ((format = GetPriorityClipboardFormat (formatlist, 3)) <= 0)
         {
           CloseClipboard ();
           return (0);
@@ -380,25 +399,49 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
               cygcb_t *clipbuf = (cygcb_t *) GlobalLock (hglb);
               if (clipbuf)
                 {
-                  fwrite (clipbuf->data, sizeof (unsigned char), clipbuf->len, out);
+                  fwrite (clipbuf->data, sizeof (char), clipbuf->len, out);
                   GlobalUnlock (hglb);
                 }
 	    }
 	  else
 	    {
-              unsigned char *nativebuf = (unsigned char *) GlobalLock (hglb);
+              char *nativebuf = (char *) GlobalLock (hglb);
               if (nativebuf)
                 {
                   size_t buflen = (*(size_t *) nativebuf);
                   buf = nativebuf + sizeof (size_t);
                   len = buflen;
 
-                  fwrite (buf, sizeof (unsigned char), len, out);
+                  fwrite (buf, sizeof (char), len, out);
                   GlobalUnlock (hglb);
                 }
 	    }
         }
-      else
+      else if (format == CF_UNICODETEXT)
+        {
+          LPWSTR lpwstr = (LPWSTR) GlobalLock (hglb);
+          if (lpwstr)
+            {
+              int srclen = wcslen (lpwstr); // wide-char count
+              int dstlen = srclen << 2; // byte count
+              LPSTR dst = (LPSTR) malloc (dstlen);
+              int res = WideCharToMultiByte (CP_UTF8, 0, lpwstr, srclen,
+                                             dst, dstlen, NULL, NULL);
+              if (res > 0)
+                fwrite (dst, sizeof (char), res, out);
+              else if (res == 0)
+                {
+#if DEBUGGING
+                  DWORD err = GetLastError ();
+                  /* look up error code displayed here in w32api/winerror.h */
+                  fprintf (stderr, "WideCharToMultiByte returns %ld\n", err);
+#endif
+                }
+              free (dst);
+              GlobalUnlock (hglb);
+            }
+        }
+      else /* format == CF_TEXT */
         {
           LPSTR lpstr = (LPSTR) GlobalLock (hglb);
           if (lpstr)
@@ -407,7 +450,7 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
               buf = lpstr;
               len = lplen;
 
-              fwrite (buf, sizeof (unsigned char), len, out);
+              fwrite (buf, sizeof (char), len, out);
               GlobalUnlock (hglb);
             }
         }
@@ -425,7 +468,7 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
       char *pos;
 
       OpenClipboard (0);
-      hglb = GetClipboardData (CF_TEXT);
+      hglb = GetClipboardData (CF_TEXT); //TODO support CF_UNICODETEXT too?
       lpstr = GlobalLock (hglb);
       lplen = strlen (lpstr);
 
@@ -481,7 +524,7 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
       char *pos;
 
       OpenClipboard (0);
-      hglb = GetClipboardData (CF_TEXT);
+      hglb = GetClipboardData (CF_TEXT); //TODO support CF_UNICODETEXT too?
       lpstr = GlobalLock (hglb);
       lplen = strlen (lpstr);
 
diff --git a/src/clip/putclip.c b/src/clip/putclip.c
index 24aefdf..76bddeb 100644
--- a/src/clip/putclip.c
+++ b/src/clip/putclip.c
@@ -25,10 +25,15 @@
 #if HAVE_CONFIG_H
 # include "config.h"
 #endif
-#include <io.h>
 
 #include "common.h"
+
+#include <io.h>
+#include <wchar.h>
 #include <sys/cygwin.h>
+#include <w32api/stringapiset.h>
+
+#define DEBUGGING 0 // Set 1 to display debug output on stderr
 
 typedef struct
 {
@@ -396,7 +401,16 @@ putclip (FILE * in, flags_struct flags, FILE * f, char *name)
       const char *CYGWIN_NATIVE = "CYGWIN_NATIVE_CLIPBOARD";
       UINT cygnativeformat;
 
-      OpenClipboard (0);
+      if (!OpenClipboard (0))
+        {
+          fprintf (stderr, "Unable to open the clipboard\n");
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "OpenClipboard returns %ld\n", err);
+#endif
+          return (PUTCLIP_ERR);
+        }
       cygnativeformat = RegisterClipboardFormat (CYGWIN_NATIVE);
       CloseClipboard ();
 
@@ -515,7 +529,18 @@ putclip (FILE * in, flags_struct flags, FILE * f, char *name)
 
       /* Allocate memory and copy the string to it */
       /* cygwin native format */
-      OpenClipboard (0);
+      if (!OpenClipboard (0))
+        {
+          fprintf (stderr, "Unable to open the clipboard\n");
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "OpenClipboard returns %ld\n", err);
+#endif
+          if (convbuf)
+            free (convbuf);
+          return (PUTCLIP_ERR);
+        }
       if (cygNewFormat)
 	hData = GlobalAlloc (GMEM_MOVEABLE, convlen + sizeof (cygcb_t));
       else
@@ -554,10 +579,84 @@ putclip (FILE * in, flags_struct flags, FILE * f, char *name)
           fprintf (f,
                    "Couldn't write native format to the clipboard %04x %x\n",
                    cygnativeformat, hData);
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "SetClipboardData returns %ld\n", err);
+#endif
+          free (convbuf);
+          return (PUTCLIP_ERR);
+        }
+      CloseClipboard ();
+#if 0 // Per MSDN, don't GlobalFree a handle successfully transferred to system
+      if (GlobalFree (hData))
+        {
+          fprintf (f,
+                   "Couldn't free global buffer after write to clipboard.\n");
+          free (convbuf);
+          return (PUTCLIP_ERR);
+        }
+#endif
+      hData = NULL;
+      clipbuf = NULL;
+
+      /* CF_UNICODETEXT format */
+      if (!OpenClipboard (0))
+        {
+          fprintf (f, "Unable to open the clipboard\n");
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "OpenClipboard returns %ld\n", err);
+#endif
+          free (convbuf);
+          return (PUTCLIP_ERR);
+        }
+      if (!(hData = GlobalAlloc (GMEM_MOVEABLE, sizeof (WCHAR) * convlen + 2)))
+        {
+          fprintf (f, "Couldn't allocate global buffer for write.\n");
+          free (convbuf);
+          return (PUTCLIP_ERR);
+        }
+      if (!(clipbuf = (void *) GlobalLock (hData)))
+        {
+          fprintf (f, "Couldn't lock global buffer.\n");
+          free (convbuf);
+          return (PUTCLIP_ERR);
+        }
+
+      int res = MultiByteToWideChar (CP_UTF8, 0, convbuf, convlen,
+                                     (LPWSTR) clipbuf, convlen + 1);
+      if (res == 0)
+        {
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "MultiByteToWideChar returns %ld\n", err);
+#endif
+        }
+      else /* res != 0 */
+        {
+          *((WCHAR *) clipbuf + res) = 0; /* NULL-terminate just in case */
+#if DEBUGGING
+          fprintf (stderr, "len %d, convlen %d, res %d\n", len, convlen, res);
+#endif
+        }
+
+      GlobalUnlock (hData);
+      if (!SetClipboardData (CF_UNICODETEXT, hData))
+        {
+          fprintf (f, "Couldn't write CF_UNICODETEXT format to the clipboard.\n");
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "SetClipboardData returns %ld\n", err);
+#endif
           free (convbuf);
           return (PUTCLIP_ERR);
         }
       CloseClipboard ();
+#if 0 // Per MSDN, don't GlobalFree a handle successfully transferred to system
       if (GlobalFree (hData))
         {
           fprintf (f,
@@ -565,11 +664,22 @@ putclip (FILE * in, flags_struct flags, FILE * f, char *name)
           free (convbuf);
           return (PUTCLIP_ERR);
         }
+#endif
       hData = NULL;
       clipbuf = NULL;
 
       /* CF_TEXT format */
-      OpenClipboard (0);
+      if (!OpenClipboard (0))
+        {
+          fprintf (f, "Unable to open the clipboard\n");
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "OpenClipboard returns %ld\n", err);
+#endif
+          free (convbuf);
+          return (PUTCLIP_ERR);
+        }
       if (!(hData = GlobalAlloc (GMEM_MOVEABLE, convlen + 2)))
         {
           fprintf (f, "Couldn't allocate global buffer for write.\n");
@@ -591,10 +701,16 @@ putclip (FILE * in, flags_struct flags, FILE * f, char *name)
       if (!SetClipboardData (CF_TEXT, hData))
         {
           fprintf (f, "Couldn't write CF_TEXT format to the clipboard.\n");
+#if DEBUGGING
+          DWORD err = GetLastError ();
+          /* look up error code displayed here in w32api/winerror.h */
+          fprintf (stderr, "SetClipboardData returns %ld\n", err);
+#endif
           free (convbuf);
           return (PUTCLIP_ERR);
         }
       CloseClipboard ();
+#if 0 // Per MSDN, don't GlobalFree a handle successfully transferred to system
       if (GlobalFree (hData))
         {
           fprintf (f,
@@ -602,6 +718,7 @@ putclip (FILE * in, flags_struct flags, FILE * f, char *name)
           free (convbuf);
           return (PUTCLIP_ERR);
         }
+#endif
       hData = NULL;
       clipbuf = NULL;
 
diff --git a/src/cygdrop/cygdrop.cc b/src/cygdrop/cygdrop.cc
index 35bcc19..0c036bb 100644
--- a/src/cygdrop/cygdrop.cc
+++ b/src/cygdrop/cygdrop.cc
@@ -39,7 +39,7 @@ static void help (FILE * f, const char *name);
 static void version (FILE * f, const char *name);
 static void license (FILE * f, const char *name);
 
-static int
+static void
 usageCore (FILE * f, const char * name)
 {
   fprintf (f,
@@ -378,7 +378,7 @@ main (int argc, char **argv)
 	     ? " [logon_id]"  : ""));
 	  const struct group * g = strsid_to_group (strsid);
 	  if (g)
-	    printf (" gid=%lu(%s)", g->gr_gid, g->gr_name);
+	    printf (" gid=%u(%s)", (unsigned) g->gr_gid, g->gr_name);
 	  printf ("\n");
 	}
 
@@ -415,7 +415,7 @@ main (int argc, char **argv)
 	printf ("r   %s", strsid);
 	const struct group * g = strsid_to_group (strsid);
 	if (g)
-	  printf (" gid=%lu(%s)", g->gr_gid, g->gr_name);
+	  printf (" gid=%u(%s)", (unsigned) g->gr_gid, g->gr_name);
 	printf ("\n");
       }
 
diff --git a/src/readshortcut/readshortcut.c b/src/readshortcut/readshortcut.c
index 9912985..d19bef0 100644
--- a/src/readshortcut/readshortcut.c
+++ b/src/readshortcut/readshortcut.c
@@ -373,7 +373,7 @@ readshortcut (optvals * opts)
       WCHAR ws[BUFF_SIZE];
 
       /* Ensure that the string is Unicode. */
-      mbstowcs (wsz, opts->target_fname, sizeof wsz);
+      mbstowcs (wsz, opts->target_fname, MAX_PATH);
 
       /* Load the shortcut.  */
       hres = persist_file->lpVtbl->Load (persist_file, wsz, STGM_READ);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.