[PATCH] Avoid using the MAXPATHLEN and PATH_MAX constants, which may be undefined.

Thomas Schwinge thomas at codesourcery.com
Mon Mar 25 09:01:30 PDT 2013


---
 lib/Support/Unix/Path.inc   |   30 +++++++++++++++++++++++-------
 lib/Support/Unix/PathV2.inc |   20 +++++++++++++-------
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git lib/Support/Unix/Path.inc lib/Support/Unix/Path.inc
index 6a5ebb8..aa724e9 100644
--- lib/Support/Unix/Path.inc
+++ lib/Support/Unix/Path.inc
@@ -60,11 +60,6 @@
 #include <mach-o/dyld.h>
 #endif
 
-// For GNU Hurd
-#if defined(__GNU__) && !defined(MAXPATHLEN)
-# define MAXPATHLEN 4096
-#endif
-
 // Put in a hack for Cygwin which falsely reports that the mkdtemp function
 // is available when it is not.
 #ifdef __CYGWIN__
@@ -251,13 +246,24 @@ Path::GetUserHomeDirectory() {
 
 Path
 Path::GetCurrentDirectory() {
+#ifdef __GLIBC__ // POSIX.1-2001 in fact.
+  // Avoid using the MAXPATHLEN constant, which may be undefined.
+  char *pathname = getcwd(NULL, 0);
+  if (!pathname)
+#else
   char pathname[MAXPATHLEN];
-  if (!getcwd(pathname, MAXPATHLEN)) {
+  if (!getcwd(pathname, MAXPATHLEN))
+#endif
+  {
     assert(false && "Could not query current working directory.");
     return Path();
   }
 
-  return Path(pathname);
+  Path result(pathname);
+#ifdef __GLIBC__
+  free(pathname);
+#endif
+  return result;
 }
 
 #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
@@ -358,9 +364,19 @@ Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
 
   // If the filename is a symlink, we need to resolve and return the location of
   // the actual executable.
+# ifdef __GLIBC__  // POSIX.1-2008 in fact.
+  // Avoid using the MAXPATHLEN constant, which may be undefined.
+  char *link_path = realpath(DLInfo.dli_fname, NULL);
+  if (link_path) {
+    Path result(link_path);
+    free(link_path);
+    return result;
+  }
+# else
   char link_path[MAXPATHLEN];
   if (realpath(DLInfo.dli_fname, link_path))
     return Path(link_path);
+# endif
 #else
 #error GetMainExecutable is not implemented on this host yet.
 #endif
diff --git lib/Support/Unix/PathV2.inc lib/Support/Unix/PathV2.inc
index a3dfd4b..fc95ff5 100644
--- lib/Support/Unix/PathV2.inc
+++ lib/Support/Unix/PathV2.inc
@@ -56,11 +56,6 @@
 #undef ferror
 #undef feof
 
-// For GNU Hurd
-#if defined(__GNU__) && !defined(PATH_MAX)
-# define PATH_MAX 4096
-#endif
-
 using namespace llvm;
 
 namespace {
@@ -114,7 +109,8 @@ error_code current_path(SmallVectorImpl<char> &result) {
 #ifdef MAXPATHLEN
   result.reserve(MAXPATHLEN);
 #else
-// For GNU Hurd
+  // If the operating system doesn't specify an upper limit, begin probing with
+  // the following default length.
   result.reserve(1024);
 #endif
 
@@ -459,8 +455,15 @@ rety_open_create:
   }
 
    // Make the path absolute.
+#ifdef __GLIBC__  // POSIX.1-2008 in fact.
+  // Avoid using the PATH_MAX constant, which may be undefined.
+  char *real_path_buff = realpath(RandomPath.c_str(), NULL);
+  if (!real_path_buff)
+#else
   char real_path_buff[PATH_MAX + 1];
-  if (realpath(RandomPath.c_str(), real_path_buff) == NULL) {
+  if (realpath(RandomPath.c_str(), real_path_buff) == NULL)
+#endif
+  {
     int error = errno;
     ::close(RandomFD);
     ::unlink(RandomPath.c_str());
@@ -470,6 +473,9 @@ rety_open_create:
   result_path.clear();
   StringRef d(real_path_buff);
   result_path.append(d.begin(), d.end());
+#ifdef __GLIBC__
+  free(real_path_buff);
+#endif
 
   result_fd = RandomFD;
   return error_code::success();
-- 
1.7.9.5




More information about the llvm-commits mailing list