[libc-commits] [libc] [libc][realpath] Implement symbolic path resolution (PR #204467)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Thu Jun 18 06:02:01 PDT 2026


================
@@ -0,0 +1,217 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of POSIX realpath.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/realpath.h"
+#include "hdr/errno_macros.h"
+#include "hdr/limits_macros.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/alloc-checker.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace {
+
+// Separator character for POSIX paths.
+constexpr char PATH_SEP = '/';
+
+// Dummy struct to represent success in `ErrorOr` when no value is needed.
+struct Ok {};
+
+// Whether a path is absolute.
+bool is_absolute(cpp::string_view path) { return path.starts_with(PATH_SEP); }
+
+// Container for a fully resolved, canonical path.
+//
+// The contained path is always in its canonical form. It is:
+// - Absolute
+// - Symlink-free
+// - Without a trailing separator
+// - Devoid of path traversals like "." or ".."
+class ResolvedPath {
+public:
+  ResolvedPath() { set_to_root(); }
+
+  void set_to_root() {
+    buf_[0] = PATH_SEP;
+    size_ = 1;
+  }
+
+  bool is_root() const { return size_ == 1; }
+
+  ErrorOr<Ok> set_to_cwd() { return Error(ENOSYS); }
+
+  void set_to_parent() {
+    size_t sep_index = view().find_last_of(PATH_SEP);
+
+    // Ensure we maintain the root separator.
+    size_ = sep_index == 0 ? 1 : sep_index;
+  }
+
+  // Adds a single component to the end of this path.
+  ErrorOr<Ok> push_component(cpp::string_view component) {
+    if (!is_root()) {
+      if (ErrorOr<Ok> res = push_raw(PATH_SEP); !res)
+        return res;
+    }
+
+    return push_raw(component);
+  }
+
+  cpp::string_view view() const { return cpp::string_view(buf_, size_); }
+
+private:
+  ErrorOr<Ok> push_raw(cpp::string_view value) {
+    if (value.size() > sizeof(buf_) - size_)
+      return Error(ENAMETOOLONG);
+
+    inline_memcpy(buf_ + size_, value.data(), value.size());
+    size_ += value.size();
+    return Ok{};
+  }
+
+  ErrorOr<Ok> push_raw(char value) {
+    return push_raw(cpp::string_view(&value, 1));
+  }
+
+  // Current size of the path stored in `buf_`.
+  size_t size_;
+
+  // `PATH_MAX` includes a null-terminator in its count,
+  // so use `PATH_MAX - 1` here as `ResolvedPath` is not null-terminated.
+  char buf_[PATH_MAX - 1];
----------------
kaladron wrote:

I'm finding myself wondering throughout here why we're not using cpp::string or a vector or something, and just avoiding all the manual memory management and size counting throughout.

It's not strictly C++ but it might be worth us adding a "release()" method to our container classes in particular to hand over ownership of malloc'd memory for us to return to a user without requiring a final copy.

@michaelrj-google for thoughts.

https://github.com/llvm/llvm-project/pull/204467


More information about the libc-commits mailing list