[PATCH] D54677: [hurd] Fix unconditional use of PATH_MAX
Samuel Thibault via Phabricator
reviews at reviews.llvm.org
Tue Dec 18 15:40:28 PST 2018
sthibaul updated this revision to Diff 178808.
sthibaul added a comment.
Right, that looks better indeed :)
Here is an updated patch.
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,19 @@
ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);
path p = __do_absolute(orig_p, &cwd, ec);
+#if _POSIX_VERSION >= 200112
+ std::unique_ptr<char, decltype(&::free)>
+ hold(::realpath(p.c_str(), nullptr), &::free);
+ if (hold.get() == nullptr)
+ return err.report(capture_errno());
+ return {hold.get()};
+#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,
@@ -1076,16 +1084,27 @@
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;
+#ifdef PATH_MAX
+ struct NullDeleter { void operator()(void*) const {} };
+ const size_t size = PATH_MAX + 1;
+ char stack_buff[size];
+ auto buff = std::unique_ptr<char[], NullDeleter>(stack_buff);
+#else
+ StatT sb;
+ if (::lstat(p.c_str(), &sb) == -1) {
+ return err.report(capture_errno());
+ }
+ const size_t size = sb.st_size + 1;
+ auto buff = unique_ptr<char[]>(new char[size]);
+#endif
::ssize_t ret;
- if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
+ if ((ret = ::readlink(p.c_str(), buff.get(), size)) == -1)
return err.report(capture_errno());
- }
- _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
_LIBCPP_ASSERT(ret > 0, "TODO");
+ if (static_cast<size_t>(ret) >= size)
+ return err.report(errc::value_too_large);
buff[ret] = 0;
- return {buff};
+ return {buff.get()};
}
bool __remove(const path& p, error_code* ec) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54677.178808.patch
Type: text/x-patch
Size: 1822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20181218/53b03ef0/attachment.bin>
More information about the libcxx-commits
mailing list