PATCH: sysdeps/unix/sysv/linux/getcwd.c yet again...

Greg McGary greg@mcgary.org
Sat Jul 15 00:15:00 GMT 2000


The (buf==NULL && size > 0) case is still broken.
This fixes it:

Index: sysdeps/unix/sysv/linux/getcwd.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/unix/sysv/linux/getcwd.c,v
retrieving revision 1.16
diff -u -p -r1.16 getcwd.c
--- getcwd.c	2000/07/13 17:42:32	1.16
+++ getcwd.c	2000/07/15 07:12:09
@@ -104,13 +104,14 @@ __getcwd (char *buf, size_t size)
       if (retval >= 0)
 	{
 	  if (buf == NULL && size == 0)
-	    {
-	      /* Ensure that the buffer is only as large as necessary.  */
-	      buf = realloc (path, (size_t) retval);
-	      if (buf == NULL)
-		/* `realloc' failed but we still have the original string.  */
-		buf = path;
-	    }
+	    /* Ensure that the buffer is only as large as necessary.  */
+	    buf = realloc (path, (size_t) retval);
+
+	  if (buf == NULL)
+	    /* Either buf was NULL all along, or `realloc' failed but
+               we still have the original string.  */
+	    buf = path;
+
 	  return buf;
 	}
 
@@ -156,13 +157,13 @@ __getcwd (char *buf, size_t size)
 
 	  path[n] = '\0';
 	  if (buf == NULL && size == 0)
-	    {
-	      /* Ensure that the buffer is only as large as necessary.  */
-	      buf = realloc (path, (size_t) n + 1);
-	      if (buf == NULL)
-		/* `relloc' failed but we still have the original string.  */
-		buf = path;
-	    }
+	    /* Ensure that the buffer is only as large as necessary.  */
+	    buf = realloc (path, (size_t) n + 1);
+	  if (buf == NULL)
+	    /* Either buf was NULL all along, or `realloc' failed but
+               we still have the original string.  */
+	    buf = path;
+
 	  return buf;
 	}
 #ifndef have_new_dcache


More information about the Libc-hacker mailing list