On 2017/11/14 9:58, Liu Hao wrote:
> On 2017/11/13 11:11, David Lee wrote:
>> Built and tested a cross compiler (i686-w64-mingw32-gcc 6.4.0) on
>> debian stretch with master mingw-w64. Same crash.
>>
> Yeah I just updated MSYS2's repos and observed the crash... can't imagine why
> it didn't crash a few days ago.
>
> The patch for mingw_vfscanf.c on SF is a bit complex so let me take a deep
> look at it.
>
>
AFAICT the C99 standard doesn't treat the format strings of *scanf and w*canf
and differently.
That is, %s always designates a narrow string and %ls always designates a wide
string. So are %c and %lc.
Basing on that, I think the fix in 72d60c1a06490ec5937e6c620956b167bf0bf329 can
be applied cleanly to its wide variant.
The patch attached is confirmed to fix the crash on x86_64 and i686. Please
review.
--
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