[PATCH] Cygwin: mbrtowi: fix segfault when pwi is NULL

David McFarland corngood@gmail.com
Tue Apr 18 20:05:34 GMT 2023


mbrtowi was missing null-checks on pwi, but NULL is passed from
regex/engine.c:173.

In a git repo with sendemail.smtpserver set, this results in a segfault when
using git-send-email, which calls:

git config --get-regexp '^sende?mail[.]'

Fixes: 60c25da90d01 ("Cygwin: mbrtowi: define replacement for mbrtowc, returning UTF-32 value")
Signed-off-by: David McFarland <corngood@gmail.com>
---
 winsup/cygwin/strfuncs.cc | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index 57abf2564..76b7216cc 100644
--- a/winsup/cygwin/strfuncs.cc
+++ b/winsup/cygwin/strfuncs.cc
@@ -159,7 +159,8 @@ mbrtowi (wint_t *pwi, const char *s, size_t n, mbstate_t *ps)
   len = mbrtowc (&w1, s, n, ps);
   if (len == (size_t) -1 || len == (size_t) -2)
     return len;
-  *pwi = w1;
+  if (pwi)
+    *pwi = w1;
   /* Convert surrogate pair to wint_t value */
   if (len > 0 && w1 >= 0xd800 && w1 <= 0xdbff)
     {
@@ -169,7 +170,8 @@ mbrtowi (wint_t *pwi, const char *s, size_t n, mbstate_t *ps)
       if (len2 > 0 && w2 >= 0xdc00 && w2 <= 0xdfff)
 	{
 	  len += len2;
-	  *pwi = (((w1 & 0x3ff) << 10) | (w2 & 0x3ff)) + 0x10000;
+	  if (pwi)
+	    *pwi = (((w1 & 0x3ff) << 10) | (w2 & 0x3ff)) + 0x10000;
 	}
       else
 	{
-- 
2.39.0.windows.2.1.gf04e877035



More information about the Cygwin-developers mailing list