Skip to content

Commit 4097ec7

Browse files
xiongzileWithmm
andauthored
[libc][test] Fix getcwd test for symlinked paths (#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. Co-authored-by: Zile Xiong <xiongzile99@gmail.com>
1 parent 537f124 commit 4097ec7

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

libc/test/integration/src/unistd/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ add_integration_test(
1010
DEPENDS
1111
libc.src.__support.CPP.string_view
1212
libc.src.errno.errno
13-
libc.src.stdlib.getenv
13+
libc.src.sys.stat.stat
1414
libc.src.unistd.getcwd
1515
)
1616

libc/test/integration/src/unistd/getcwd_test.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,33 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/CPP/string_view.h"
10-
#include "src/stdlib/getenv.h"
9+
#include "src/sys/stat/stat.h"
1110
#include "src/unistd/getcwd.h"
12-
1311
#include "test/IntegrationTest/test.h"
1412

1513
#include <errno.h>
1614
#include <stdlib.h> // For malloc and free
1715

18-
using LIBC_NAMESPACE::cpp::string_view;
19-
2016
TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
2117
[[maybe_unused]] char **envp) {
2218
char buffer[1024];
23-
ASSERT_TRUE(string_view(LIBC_NAMESPACE::getenv("PWD")) ==
24-
LIBC_NAMESPACE::getcwd(buffer, 1024));
19+
char *cwd = LIBC_NAMESPACE::getcwd(buffer, sizeof(buffer));
20+
ASSERT_TRUE(cwd != nullptr);
21+
22+
struct stat st_dot;
23+
struct stat st_cwd;
24+
25+
ASSERT_EQ(LIBC_NAMESPACE::stat(".", &st_dot), 0);
26+
ASSERT_EQ(LIBC_NAMESPACE::stat(cwd, &st_cwd), 0);
27+
28+
ASSERT_EQ(st_dot.st_dev, st_cwd.st_dev);
29+
ASSERT_EQ(st_dot.st_ino, st_cwd.st_ino);
2530

2631
// nullptr buffer
27-
char *cwd = LIBC_NAMESPACE::getcwd(nullptr, 0);
28-
ASSERT_TRUE(string_view(LIBC_NAMESPACE::getenv("PWD")) == cwd);
32+
cwd = LIBC_NAMESPACE::getcwd(nullptr, 0);
33+
ASSERT_EQ(LIBC_NAMESPACE::stat(cwd, &st_cwd), 0);
34+
ASSERT_EQ(st_dot.st_dev, st_cwd.st_dev);
35+
ASSERT_EQ(st_dot.st_ino, st_cwd.st_ino);
2936
free(cwd);
3037

3138
// Bad size

0 commit comments

Comments
 (0)