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

Rafael EspĂ­ndola rafael.espindola at gmail.com
Mon Mar 25 10:56:40 PDT 2013


The OS X manpage says it supports allocating memory too. Do you know
of any supported system that requires the caller to pass in the
buffer?

On 25 March 2013 09:13, Thomas Schwinge <thomas at codesourcery.com> wrote:
> ---
>  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
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits



More information about the cfe-commits mailing list