[jvb@cyberscience.com] libc/1788: getcwd(NULL,size) with size>0 doesn't work

Andreas Jaeger aj@suse.de
Thu Jul 13 02:31:00 GMT 2000


John Buddery sent the appended bug report.  I've verified the problem
and propose the appended patch to fix this.

John, can you check the patch, please?

May I commit this?

Andreas

2000-07-13  Andreas Jaeger  <aj@suse.de>

	* sysdeps/unix/sysv/linux/getcwd.c (__getcwd): Correctly handle
	getcwd (NULL, size) with size > 0.
	Fixes PR libc/1788, reported by John Buddery
	<jvb@cyberscience.com>.


============================================================
Index: sysdeps/unix/sysv/linux/getcwd.c
--- sysdeps/unix/sysv/linux/getcwd.c	2000/07/07 02:19:05	1.14
+++ sysdeps/unix/sysv/linux/getcwd.c	2000/07/13 09:21:50
@@ -103,8 +103,9 @@
       retval = INLINE_SYSCALL (getcwd, 2, CHECK_STRING (path), alloc_size);
       if (retval >= 0)
 	{
-	  if (buf == NULL)
+	  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.  */
@@ -115,8 +116,9 @@
 
 # if __ASSUME_GETCWD_SYSCALL
       /* It should never happen that the `getcwd' syscall failed because
-	 the buffer is too small if we allocated the buffer outself.  */
-      assert (errno != ERANGE || buf != NULL);
+	 the buffer is too small if we allocated the buffer ourselves
+	 large enough.  */
+      assert (errno != ERANGE || buf != NULL || size != 0);
 
       if (buf == NULL)
 	free (path);
@@ -153,8 +155,9 @@
 	    }
 
 	  path[n] = '\0';
-	  if (buf == NULL)
+	  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.  */



More information about the Libc-alpha mailing list