]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Correct last patch. Handle getcwd (NULL, != 0) correctly.
authorUlrich Drepper <drepper@redhat.com>
Thu, 8 Jul 1999 11:49:46 +0000 (11:49 +0000)
committerUlrich Drepper <drepper@redhat.com>
Thu, 8 Jul 1999 11:49:46 +0000 (11:49 +0000)
When resizing buffer make sure new size is large enough.

sysdeps/posix/getcwd.c

index fe094b5a35c90a66ae57c1889bfbe8834e4312e7..886a0ba8273da2f74f3fac2cdd354fd76ede0605 100644 (file)
@@ -135,6 +135,10 @@ extern void free ();
 # define memmove memcpy
 #endif /* Not ANSI_STRING.  */
 
+#ifndef MAX
+# define MAX(a, b) ((a) < (b) ? (b) : (a))
+#endif
+
 #ifdef _LIBC
 # ifndef mempcpy
 #  define mempcpy __mempcpy
@@ -220,6 +224,7 @@ __getcwd (buf, size)
   register char *pathp;
   struct stat st;
   int prev_errno = errno;
+  size_t allocated = size;
 
   if (size == 0)
     {
@@ -229,19 +234,19 @@ __getcwd (buf, size)
          return NULL;
        }
 
-      size = PATH_MAX + 1;
+      allocated = PATH_MAX + 1;
     }
 
   if (buf != NULL)
     path = buf;
   else
     {
-      path = malloc (size);
+      path = malloc (allocated);
       if (path == NULL)
        return NULL;
     }
 
-  pathp = path + size;
+  pathp = path + allocated;
   *--pathp = '\0';
 
   if (__lstat (".", &st) < 0)
@@ -358,7 +363,7 @@ __getcwd (buf, size)
 
          if ((size_t) (pathp - path) <= namlen)
            {
-             if (buf != NULL)
+             if (size != 0)
                {
                  (void) __closedir (dirstream);
                  __set_errno (ERANGE);
@@ -367,20 +372,23 @@ __getcwd (buf, size)
              else
                {
                  char *tmp;
+                 size_t oldsize = allocated;
 
-                 size *= 2;
-                 tmp = realloc (path, size);
+                 allocated = 2 * MAX (allocated, namlen);
+                 tmp = realloc (path, allocated);
                  if (tmp == NULL)
                    {
                      (void) __closedir (dirstream);
                      __set_errno (ENOMEM);/* closedir might have changed it.*/
                      goto lose;
                    }
-                 pathp = &tmp[pathp - path + size / 2];
-                 path = tmp;
+
                  /* Move current contents up to the end of the buffer.
                     This is guaranteed to be non-overlapping.  */
-                 memcpy (pathp, pathp - size / 2, path + size - pathp);
+                 pathp = memcpy (tmp + allocated - (path + oldsize - pathp),
+                                 tmp + (pathp - path),
+                                 path + oldsize - pathp);
+                 path = tmp;
                }
            }
          pathp -= namlen;
@@ -393,13 +401,13 @@ __getcwd (buf, size)
       thisino = dotino;
     }
 
-  if (pathp == &path[size - 1])
+  if (pathp == &path[allocated - 1])
     *--pathp = '/';
 
   if (dotlist != dots)
     free ((__ptr_t) dotlist);
 
-  memmove (path, pathp, path + size - pathp);
+  memmove (path, pathp, path + allocated - pathp);
 
   /* Restore errno on successful return.  */
   __set_errno (prev_errno);