This patches addresses the issue in 
<https://sourceforge.net/p/mingw-w64/mailman/message/36111483/>.

The C99 standard treats %s, %c, %ls and %lc identically in *scanf and w*scanf,
hence this patch is merely copy-pasting from 
72d60c1a06490ec5937e6c620956b167bf0bf329.

With this patch the problem in the original post no longer happens on i686 and 
x86_64.

-- 
Best regards,
LH_Mouse
From c470391c15e8006c43bc8d3924d1ad44de94ed87 Mon Sep 17 00:00:00 2001
From: Liu Hao <lh_mo...@126.com>
Date: Tue, 14 Nov 2017 10:16:24 +0800
Subject: [PATCH] stdio/mingw_wvfscanf.c: Fix segmentation fault when a char or
 string format (without malloc option) is used, like
 72d60c1a06490ec5937e6c620956b167bf0bf329.

Signed-off-by: Liu Hao <lh_mo...@126.com>
---
 mingw-w64-crt/stdio/mingw_wvfscanf.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/mingw-w64-crt/stdio/mingw_wvfscanf.c 
b/mingw-w64-crt/stdio/mingw_wvfscanf.c
index 045aba79..043c19b4 100644
--- a/mingw-w64-crt/stdio/mingw_wvfscanf.c
+++ b/mingw-w64-crt/stdio/mingw_wvfscanf.c
@@ -104,13 +104,19 @@ get_va_nth (va_list argp, unsigned int n)
 }
 
 static void
-optimize_alloc (int do_realloc, char **p, size_t sz, size_t need_sz, size_t 
typ_sz)
+optimize_alloc (char **p, char *end, size_t alloc_sz)
 {
+  size_t need_sz;
   char *h;
 
-  if (!do_realloc || sz == need_sz || !p || *p == NULL)
+  if (!p || !*p)
     return;
-  if ((h = (char *) realloc (*p, need_sz * typ_sz)) != NULL)
+
+  need_sz = end - *p;
+  if (need_sz == alloc_sz)
+    return;
+
+  if ((h = (char *) realloc (*p, need_sz)) != NULL)
     *p = h;
 }
 
@@ -629,7 +635,7 @@ __mingw_swformat (_IFP *s, const wchar_t *format, va_list 
argp)
 
          if ((flags & IS_SUPPRESSED) == 0)
            {
-             optimize_alloc ((flags & IS_ALLOC_USED) != 0, pstr, str_sz, (str 
- *pstr), sizeof (char));
+             optimize_alloc (pstr, str, str_sz);
              pstr = NULL;
              ++rval;
            }
@@ -708,7 +714,7 @@ __mingw_swformat (_IFP *s, const wchar_t *format, va_list 
argp)
 
          if ((flags & IS_SUPPRESSED) == 0)
            {
-             optimize_alloc ((flags & IS_ALLOC_USED) != 0, pstr, str_sz, (wstr 
- (wchar_t *) *pstr), sizeof (wchar_t));
+             optimize_alloc (pstr, (char *) wstr, str_sz * sizeof (wchar_t));
              pstr = NULL;
              ++rval;
            }
@@ -824,7 +830,7 @@ __mingw_swformat (_IFP *s, const wchar_t *format, va_list 
argp)
              }
              *str++ = 0;
 
-             optimize_alloc ((flags & IS_ALLOC_USED) != 0, pstr, str_sz, (str 
- *pstr), sizeof (char));
+             optimize_alloc (pstr, str, str_sz);
              pstr = NULL;
              ++rval;
            }
@@ -903,7 +909,7 @@ __mingw_swformat (_IFP *s, const wchar_t *format, va_list 
argp)
            {
              *wstr++ = 0;
 
-             optimize_alloc ((flags & IS_ALLOC_USED) != 0, pstr, str_sz, (wstr 
- (wchar_t *) *pstr), sizeof (wchar_t));
+             optimize_alloc (pstr, (char *) wstr, str_sz * sizeof (wchar_t));
              pstr = NULL;
              ++rval;
            }
@@ -1449,7 +1455,7 @@ __mingw_swformat (_IFP *s, const wchar_t *format, va_list 
argp)
                {
                  *wstr++ = 0;
 
-                 optimize_alloc ((flags & IS_ALLOC_USED) != 0, pstr, str_sz, 
(wstr - (wchar_t *) *pstr), sizeof (wchar_t));
+                 optimize_alloc (pstr, (char *) wstr, str_sz * sizeof 
(wchar_t));
                  pstr = NULL;
                  ++rval;
                }
@@ -1581,7 +1587,7 @@ __mingw_swformat (_IFP *s, const wchar_t *format, va_list 
argp)
                  }
                  *str++ = 0;
 
-                 optimize_alloc ((flags & IS_ALLOC_USED) != 0, pstr, str_sz, 
(str - *pstr), sizeof (char));
+                 optimize_alloc (pstr, str, str_sz);
                  pstr = NULL;
                  ++rval;
                }
-- 
2.14.2

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to