[libc-commits] [libc] [llvm] [libc][test] Fix getcwd test for symlinked paths (PR #191426)
Zile Xiong via libc-commits
libc-commits at lists.llvm.org
Fri Apr 10 07:22:53 PDT 2026
https://github.com/xiongzile created https://github.com/llvm/llvm-project/pull/191426
The test compared getcwd() with getenv("PWD"), which is not reliable under symlinked paths: PWD may preserve the logical path while getcwd() returns the physical path.
Use stat(2) to verify directory identity instead.
>From 144b619a94cba686ba71b509c24e3c18cea02ec1 Mon Sep 17 00:00:00 2001
From: Zile Xiong <xiongzile99 at gmail.com>
Date: Fri, 10 Apr 2026 22:21:38 +0800
Subject: [PATCH] [libc][test] Fix getcwd test for symlinked paths
The test compared getcwd() with getenv("PWD"), which is not reliable
under symlinked paths: PWD may preserve the logical path while
getcwd() returns the physical path.
Use stat(2) to verify directory identity instead.
---
1.c | 0
.../integration/src/unistd/CMakeLists.txt | 2 +-
.../integration/src/unistd/getcwd_test.cpp | 22 ++++++++++++++-----
3 files changed, 17 insertions(+), 7 deletions(-)
create mode 100644 1.c
diff --git a/1.c b/1.c
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/libc/test/integration/src/unistd/CMakeLists.txt b/libc/test/integration/src/unistd/CMakeLists.txt
index fd02c6521e2a7..3c09ae43b4dd7 100644
--- a/libc/test/integration/src/unistd/CMakeLists.txt
+++ b/libc/test/integration/src/unistd/CMakeLists.txt
@@ -10,7 +10,7 @@ add_integration_test(
DEPENDS
libc.src.__support.CPP.string_view
libc.src.errno.errno
- libc.src.stdlib.getenv
+ libc.src.sys.stat.stat
libc.src.unistd.getcwd
)
diff --git a/libc/test/integration/src/unistd/getcwd_test.cpp b/libc/test/integration/src/unistd/getcwd_test.cpp
index 10bbe2a2d097f..793533373cc81 100644
--- a/libc/test/integration/src/unistd/getcwd_test.cpp
+++ b/libc/test/integration/src/unistd/getcwd_test.cpp
@@ -7,9 +7,8 @@
//===----------------------------------------------------------------------===//
#include "src/__support/CPP/string_view.h"
-#include "src/stdlib/getenv.h"
+#include "src/sys/stat/stat.h"
#include "src/unistd/getcwd.h"
-
#include "test/IntegrationTest/test.h"
#include <errno.h>
@@ -20,12 +19,23 @@ using LIBC_NAMESPACE::cpp::string_view;
TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
[[maybe_unused]] char **envp) {
char buffer[1024];
- ASSERT_TRUE(string_view(LIBC_NAMESPACE::getenv("PWD")) ==
- LIBC_NAMESPACE::getcwd(buffer, 1024));
+ char *cwd = LIBC_NAMESPACE::getcwd(buffer, sizeof(buffer));
+ ASSERT_TRUE(cwd != nullptr);
+
+ struct stat st_dot;
+ struct stat st_cwd;
+
+ ASSERT_EQ(LIBC_NAMESPACE::stat(".", &st_dot), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::stat(cwd, &st_cwd), 0);
+
+ ASSERT_EQ(st_dot.st_dev, st_cwd.st_dev);
+ ASSERT_EQ(st_dot.st_ino, st_cwd.st_ino);
// nullptr buffer
- char *cwd = LIBC_NAMESPACE::getcwd(nullptr, 0);
- ASSERT_TRUE(string_view(LIBC_NAMESPACE::getenv("PWD")) == cwd);
+ cwd = LIBC_NAMESPACE::getcwd(nullptr, 0);
+ ASSERT_EQ(LIBC_NAMESPACE::stat(cwd, &st_cwd), 0);
+ ASSERT_EQ(st_dot.st_dev, st_cwd.st_dev);
+ ASSERT_EQ(st_dot.st_ino, st_cwd.st_ino);
free(cwd);
// Bad size
More information about the libc-commits
mailing list