Hi,

thanks for providing mingw-w64.

I found this when I tried to compile smartmontools on Cygwin with i686-w64-mingw32 and _USE_MINGW_ANSI_STDIO set:

__mingw_*scanf() always(!) segfaults if a char or string format (without malloc option) is used. This is because optimize_alloc() is always called with (pstr - str) as argument even if pstr is NULL.

Proposed patch attached, smoke-tested only with a few common cases.

optimize_alloc() parameters are simplified. The do_realloc parameter is removed, it is assumed that the following condition holds:
((flags & IS_ALLOC_USED) != 0) == (pstr != NULL)

Thanks,
Christian

Index: mingw_vfscanf.c
===================================================================
--- mingw_vfscanf.c	(revision 4227)
+++ mingw_vfscanf.c	(working copy)
@@ -95,13 +95,19 @@
 }
 
 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;
 }
 
@@ -619,8 +625,7 @@
 
 	  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;
 	    }
@@ -717,8 +722,7 @@
 
 	  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;
 	    }
@@ -796,8 +800,7 @@
 	  if ((flags & IS_SUPPRESSED) == 0)
 	    {
 	      *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;
 	    }
@@ -902,8 +905,7 @@
 	  if ((flags & IS_SUPPRESSED) == 0)
 	    {
 	      *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;
 	    }
@@ -1508,8 +1510,7 @@
 	      if ((flags & IS_SUPPRESSED) == 0)
 		{
 		  *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;
 		}
@@ -1565,8 +1566,7 @@
 	      if ((flags & IS_SUPPRESSED) == 0)
 		{
 		  *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;
 		}
------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today.
http://p.sf.net/sfu/quest-sfdev2dev
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to