D54677-hurd-path_max
authorLLVM Packaging Team <pkg-llvm-team@lists.alioth.debian.org>
Thu, 19 Mar 2020 08:50:20 +0000 (08:50 +0000)
committerGianfranco Costamagna <locutusofborg@debian.org>
Thu, 19 Mar 2020 08:50:20 +0000 (08:50 +0000)
[hurd] Fix unconditional use of PATH_MAX

The GNU/Hurd system does not define an arbitrary PATH_MAX limitation, the POSIX 2001 realpath extension can be used instead, and the size of symlinks can be determined.

https://reviews.llvm.org/D54677

Gbp-Pq: Topic hurd
Gbp-Pq: Name D54677-hurd-path_max.diff

libcxx/src/filesystem/operations.cpp

index b4106188872455eaa69fb755f12c6b4c2619cb92..c09c2ede8e898c48b03a32a4b79ec37a2031c00f 100644 (file)
@@ -543,11 +543,20 @@ path __canonical(path const& orig_p, error_code* ec) {
   ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);
 
   path p = __do_absolute(orig_p, &cwd, ec);
+#if _POSIX_VERSION >= 200112 || defined(__GLIBC__)
+  char *buff;
+  if ((buff = ::realpath(p.c_str(), NULL)) == nullptr)
+    return err.report(capture_errno());
+  path ret = {buff};
+  free(buff);
+  return ret;
+#else
   char buff[PATH_MAX + 1];
   char* ret;
   if ((ret = ::realpath(p.c_str(), buff)) == nullptr)
     return err.report(capture_errno());
   return {ret};
+#endif
 }
 
 void __copy(const path& from, const path& to, copy_options options,
@@ -1089,16 +1098,27 @@ void __permissions(const path& p, perms prms, perm_options opts,
 path __read_symlink(const path& p, error_code* ec) {
   ErrorHandler<path> err("read_symlink", ec, &p);
 
-  char buff[PATH_MAX + 1];
-  error_code m_ec;
+  struct stat sb;
+  if (lstat(p.c_str(), &sb) == -1) {
+    return err.report(capture_errno());
+  }
+  size_t size = sb.st_size + 1;
+  char *buff = (char*) malloc(size);
+  if (buff == NULL) {
+    return err.report(capture_errno());
+  }
+
   ::ssize_t ret;
-  if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
+  if ((ret = ::readlink(p.c_str(), buff, size)) == -1) {
+    free(buff);
     return err.report(capture_errno());
   }
-  _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
+  _LIBCPP_ASSERT(ret < size, "TODO");
   _LIBCPP_ASSERT(ret > 0, "TODO");
   buff[ret] = 0;
-  return {buff};
+  path res = {buff};
+  free(buff);
+  return res;
 }
 
 bool __remove(const path& p, error_code* ec) {