Imported from ../bash-3.2.48.tar.gz.
[platform/upstream/bash.git] / lib / sh / getcwd.c
index cd724f6..1a02794 100644 (file)
@@ -251,19 +251,21 @@ getcwd (buf, size)
 
   {
     size_t len = pathbuf + pathsize - pathp;
+    if (buf == NULL && size <= 0)
+      size = len;
+
+    if ((size_t) size < len)
+      {
+       errno = ERANGE;
+       goto lose2;
+      }
     if (buf == NULL)
       {
-       if (len < (size_t) size)
-         len = size;
-       buf = (char *) malloc (len);
+       buf = (char *) malloc (size);
        if (buf == NULL)
          goto lose2;
       }
-    else if ((size_t) size < len)
-      {
-       errno = ERANGE;
-       goto lose2;
-      }
+
     (void) memcpy((PTR_T) buf, (PTR_T) pathp, len);
   }