[PATCH] D54677: [hurd] Fix unconditional use of PATH_MAX

Samuel Thibault via Phabricator reviews at reviews.llvm.org
Tue Nov 27 11:58:44 PST 2018


sthibaul updated this revision to Diff 175551.

Repository:
  rCXX libc++

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54677/new/

https://reviews.llvm.org/D54677

Files:
  src/filesystem/operations.cpp


Index: src/filesystem/operations.cpp
===================================================================
--- src/filesystem/operations.cpp
+++ src/filesystem/operations.cpp
@@ -530,11 +530,20 @@
   ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);
 
   path p = __do_absolute(orig_p, &cwd, ec);
+#if _POSIX_VERSION >= 200112
+  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,
@@ -1075,10 +1084,12 @@
 
 path __read_symlink(const path& p, error_code* ec) {
   ErrorHandler<path> err("read_symlink", ec, &p);
+  ::ssize_t ret;
 
+#ifdef PATH_MAX
   char buff[PATH_MAX + 1];
   error_code m_ec;
-  ::ssize_t ret;
+
   if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
     return err.report(capture_errno());
   }
@@ -1086,6 +1097,23 @@
   _LIBCPP_ASSERT(ret > 0, "TODO");
   buff[ret] = 0;
   return {buff};
+#else
+  struct stat sb;
+  if (::lstat(p.c_str(), &sb) == -1) {
+    return err.report(capture_errno());
+  }
+  size_t size = sb.st_size + 1;
+  auto buff = unique_ptr<char[]>(new char[size]);
+
+  if ((ret = ::readlink(p.c_str(), buff.get(), size)) == -1)
+    return err.report(capture_errno());
+  if (ret >= size)
+    return err.report(errc::value_too_large);
+  _LIBCPP_ASSERT(ret > 0, "TODO");
+  buff[ret] = 0;
+  path res = {buff.get()};
+  return res;
+#endif
 }
 
 bool __remove(const path& p, error_code* ec) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54677.175551.patch
Type: text/x-patch
Size: 1704 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20181127/7a516c59/attachment.bin>


More information about the libcxx-commits mailing list