From 3f60f7e544656eb9f701ca0cf6c4358fe9ae9400 Mon Sep 17 00:00:00 2001
From: Jeff Johnston <jjohnstn@redhat.com>
Date: Wed, 18 Feb 2009 21:28:41 +0000
Subject: [PATCH] 2009-02-18  Jeff Johnston  <jjohnstn@redhat.com>

        * libc/stdio/open_memstream.c (internal_open_memstream_r): Fix max
        buffer size to be in wchar_t units if wide == 1 is passed in.  In
        this case, also initialize the first character of the buffer to be
        wide char null.
        (_open_wmemstream_r): Cast buf to be (char **) to avoid warning.
        * libc/stdlib/mbtowc_r.c (_mbtowc_r): Change all occurences of
        incrementing the size_t value n to first check that n is not already
        size_t -1.  Fix some compiler warnings.
        * libc/stdlib/wcstod.c: Add includes for <wctype.h> and <math.h>.
---
 newlib/ChangeLog                   | 12 ++++++++++++
 newlib/libc/stdio/open_memstream.c | 11 ++++++++---
 newlib/libc/stdlib/mbtowc_r.c      | 28 ++++++++++++++--------------
 newlib/libc/stdlib/wcstod.c        |  2 ++
 4 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/newlib/ChangeLog b/newlib/ChangeLog
index e20264c87..525548a09 100644
--- a/newlib/ChangeLog
+++ b/newlib/ChangeLog
@@ -1,3 +1,15 @@
+2009-02-18  Jeff Johnston  <jjohnstn@redhat.com>
+
+	* libc/stdio/open_memstream.c (internal_open_memstream_r): Fix max
+	buffer size to be in wchar_t units if wide == 1 is passed in.  In
+	this case, also initialize the first character of the buffer to be
+	wide char null.
+	(_open_wmemstream_r): Cast buf to be (char **) to avoid warning.
+	* libc/stdlib/mbtowc_r.c (_mbtowc_r): Change all occurences of
+	incrementing the size_t value n to first check that n is not already
+	size_t -1.  Fix some compiler warnings.
+	* libc/stdlib/wcstod.c: Add includes for <wctype.h> and <math.h>.
+
 2009-02-18  Corinna Vinschen  <corinna@vinschen.de>
 
 	* libc/stdio/open_memstream.c: Add open_wmemstream to doumentation.
diff --git a/newlib/libc/stdio/open_memstream.c b/newlib/libc/stdio/open_memstream.c
index 2b94d66d6..e062ec7e9 100644
--- a/newlib/libc/stdio/open_memstream.c
+++ b/newlib/libc/stdio/open_memstream.c
@@ -325,6 +325,8 @@ _DEFUN(internal_open_memstream_r, (ptr, buf, size, wide),
      mallocs on small strings) and 64k bytes (to avoid overusing the
      heap if *size was garbage).  */
   c->max = *size;
+  if (wide == 1)
+    c->max *= sizeof(wchar_t);
   if (c->max < 64)
     c->max = 64;
   else if (c->max > 64 * 1024)
@@ -342,7 +344,10 @@ _DEFUN(internal_open_memstream_r, (ptr, buf, size, wide),
       _free_r (ptr, c);
       return NULL;
     }
-  **buf = '\0';
+  if (wide == 1)
+    **((wchar_t **)buf) = L'\0';
+  else
+    **buf = '\0';
 
   c->storage = c;
   c->pbuf = buf;
@@ -374,7 +379,7 @@ _DEFUN(_open_memstream_r, (ptr, buf, size),
        char **buf _AND
        size_t *size)
 {
-  internal_open_memstream_r (ptr, buf, size, -1);
+  return internal_open_memstream_r (ptr, buf, size, -1);
 }
 
 FILE *
@@ -383,7 +388,7 @@ _DEFUN(_open_wmemstream_r, (ptr, buf, size),
        wchar_t **buf _AND
        size_t *size)
 {
-  internal_open_memstream_r (ptr, buf, size, 1);
+  return internal_open_memstream_r (ptr, (char **)buf, size, 1);
 }
 
 #ifndef _REENT_ONLY
diff --git a/newlib/libc/stdlib/mbtowc_r.c b/newlib/libc/stdlib/mbtowc_r.c
index 7f0dd11aa..71bbf8537 100644
--- a/newlib/libc/stdlib/mbtowc_r.c
+++ b/newlib/libc/stdlib/mbtowc_r.c
@@ -65,8 +65,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
     return -2;
 
 #ifdef _MB_CAPABLE
-  if (__lc_ctype == NULL ||
-      (strlen (__lc_ctype) <= 1))
+  if (strlen (__lc_ctype) <= 1)
     { /* fall-through */ }
   else if (!strcmp (__lc_ctype, "C-UTF-8"))
     {
@@ -80,7 +79,8 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	ch = t[i++];
       else
 	{
-	  ++n;
+	  if (n < (size_t)-1)
+	    ++n;
 	  ch = state->__value.__wchb[0];
 	}
 
@@ -123,7 +123,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[0] = ch;
 	  if (state->__count == 0)
 	    state->__count = 1;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 2)
 	    return -2;
@@ -158,7 +158,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[0] = ch;
 	  if (state->__count == 0)
 	    state->__count = 1;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 2)
 	    return -2;
@@ -171,7 +171,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[1] = ch;
 	  if (state->__count == 1)
 	    state->__count = 2;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 3)
 	    return -2;
@@ -201,7 +201,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[0] = ch;
 	  if (state->__count == 0)
 	    state->__count = 1;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 2)
 	    return -2;
@@ -214,7 +214,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[1] = ch;
 	  if (state->__count == 1)
 	    state->__count = 2;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 3)
 	    return -2;
@@ -224,7 +224,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[2] = ch;
 	  if (state->__count == 2)
 	    state->__count = 3;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 4)
 	    return -2;
@@ -254,7 +254,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[0] = ch;
 	  if (state->__count == 0)
 	    state->__count = 1;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 2)
 	    return -2;
@@ -267,7 +267,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[1] = ch;
 	  if (state->__count == 1)
 	    state->__count = 2;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 3)
 	    return -2;
@@ -277,7 +277,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[2] = ch;
 	  if (state->__count == 2)
 	    state->__count = 3;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 4)
 	    return -2;
@@ -287,7 +287,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
 	  state->__value.__wchb[3] = ch;
 	  if (state->__count == 3)
 	    state->__count = 4;
-	  else
+	  else if (n < (size_t)-1)
 	    ++n;
 	  if (n < 5)
 	    return -2;
@@ -444,7 +444,7 @@ _DEFUN (_mbtowc_r, (r, pwc, s, n, state),
               *pwc = (((wchar_t)state->__value.__wchb[0]) << 8) + (wchar_t)(t[i]);
               return (i + 1);
             case MAKE_A:
-              ptr = (char *)(t + i + 1);
+              ptr = (unsigned char *)(t + i + 1);
               break;
             case ERROR:
             default:
diff --git a/newlib/libc/stdlib/wcstod.c b/newlib/libc/stdlib/wcstod.c
index 403415705..2d62a86af 100644
--- a/newlib/libc/stdlib/wcstod.c
+++ b/newlib/libc/stdlib/wcstod.c
@@ -117,6 +117,8 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
 #include <errno.h>
 #include <stdlib.h>
 #include <wchar.h>
+#include <wctype.h>
+#include <math.h>
 
 double
 _DEFUN (_wcstod_r, (ptr, nptr, endptr),