No specific user configuration
[platform/upstream/bash.git] / lib / glob / xmbsrtowcs.c
index 4d8c043..11a4d1b 100644 (file)
@@ -1,6 +1,6 @@
 /* xmbsrtowcs.c -- replacement function for mbsrtowcs */
 
-/* Copyright (C) 2002-2010 Free Software Foundation, Inc.
+/* Copyright (C) 2002-2013 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -166,7 +166,7 @@ xdupmbstowcs2 (destp, src)
   do
     {
       end_or_backslash = strchrnul(p, '\\');
-      nms = (end_or_backslash - p);
+      nms = end_or_backslash - p;
       if (*end_or_backslash == '\0')
        nms++;
 
@@ -216,12 +216,24 @@ xdupmbstowcs2 (destp, src)
         It may set 'p' to NULL. */
       n = mbsnrtowcs(wsbuf+wcnum, &p, nms, wsbuf_size-wcnum, &state);
 
+      if (n == 0 && p == 0)
+       {
+         wsbuf[wcnum] = L'\0';
+         break;
+       }
+
       /* Compensate for taking single byte on wcs conversion failure above. */
       if (wcslength == 1 && (n == 0 || n == (size_t)-1))
        {
          state = tmp_state;
          p = tmp_p;
-         wsbuf[wcnum++] = *p++;
+         wsbuf[wcnum] = *p;
+         if (*p == 0)
+           break;
+         else
+           {
+             wcnum++; p++;
+           }
        }
       else
         wcnum += wcslength;
@@ -271,6 +283,8 @@ xdupmbstowcs (destp, indicesp, src)
     {
       if (destp)
        *destp = NULL;
+      if (indicesp)
+       *indicesp = NULL;
       return (size_t)-1;
     }
 
@@ -286,6 +300,8 @@ xdupmbstowcs (destp, indicesp, src)
   if (wsbuf == NULL)
     {
       *destp = NULL;
+      if (indicesp)
+        *indicesp = NULL;
       return (size_t)-1;
     }
 
@@ -297,6 +313,7 @@ xdupmbstowcs (destp, indicesp, src)
        {
          free (wsbuf);
          *destp = NULL;
+         *indicesp = NULL;
          return (size_t)-1;
        }
     }
@@ -331,6 +348,8 @@ xdupmbstowcs (destp, indicesp, src)
          free (wsbuf);
          FREE (indices);
          *destp = NULL;
+         if (indicesp)
+           *indicesp = NULL;
          return (size_t)-1;
        }
 
@@ -350,18 +369,22 @@ xdupmbstowcs (destp, indicesp, src)
              free (wsbuf);
              FREE (indices);
              *destp = NULL;
+             if (indicesp)
+               *indicesp = NULL;
              return (size_t)-1;
            }
          wsbuf = wstmp;
 
          if (indicesp)
            {
-             idxtmp = (char **) realloc (indices, wsbuf_size * sizeof (char **));
+             idxtmp = (char **) realloc (indices, wsbuf_size * sizeof (char *));
              if (idxtmp == NULL)
                {
                  free (wsbuf);
                  free (indices);
                  *destp = NULL;
+                 if (indicesp)
+                   *indicesp = NULL;
                  return (size_t)-1;
                }
              indices = idxtmp;