[PATCH] Avoid using the PATH_MAX constant, which may be undefined.

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


---
 lib/Basic/FileManager.cpp |   12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git lib/Basic/FileManager.cpp lib/Basic/FileManager.cpp
index 9cc5902..b89e3f7 100644
--- lib/Basic/FileManager.cpp
+++ lib/Basic/FileManager.cpp
@@ -632,6 +632,17 @@ StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
     return Known->second;
 
   StringRef CanonicalName(Dir->getName());
+# ifdef __GLIBC__  // POSIX.1-2008 in fact.
+  // Avoid using the PATH_MAX constant, which may be undefined.
+  char *CanonicalNameBuf = realpath(Dir->getName(), NULL);
+  if (CanonicalNameBuf) {
+    unsigned Len = strlen(CanonicalNameBuf);
+    char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1));
+    memcpy(Mem, CanonicalNameBuf, Len);
+    free(CanonicalNameBuf);
+    CanonicalName = StringRef(Mem, Len);
+  }
+# else
   char CanonicalNameBuf[PATH_MAX];
   if (realpath(Dir->getName(), CanonicalNameBuf)) {
     unsigned Len = strlen(CanonicalNameBuf);
@@ -639,6 +650,7 @@ StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
     memcpy(Mem, CanonicalNameBuf, Len);
     CanonicalName = StringRef(Mem, Len);
   }
+# endif
 
   CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
   return CanonicalName;
-- 
1.7.9.5




More information about the cfe-commits mailing list