Skip to content

[libc++][TZDB] Fixes relative path resolving. #87882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

mordante
Copy link
Member

@mordante mordante commented Apr 6, 2024

The path /etc/localtime is a symlink. This symlink can be a relative path. This fixes resolving a relative symlink.

Since the path used is hard-coded based on the user's system there is no good way to test this.

Fixes: #87872

The path /etc/localtime is a symlink. This symlink can be a relative
path. This fixes resolving a relative symlink.

Since the path used is hard-coded based on the user's system there is no
good way to test this.

Fixes: llvm#87872
@mordante mordante requested a review from a team as a code owner April 6, 2024 17:02
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 6, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 6, 2024

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

The path /etc/localtime is a symlink. This symlink can be a relative path. This fixes resolving a relative symlink.

Since the path used is hard-coded based on the user's system there is no good way to test this.

Fixes: #87872


Full diff: https://github.com/llvm/llvm-project/pull/87882.diff

1 Files Affected:

  • (modified) libcxx/src/tzdb.cpp (+5-1)
diff --git a/libcxx/src/tzdb.cpp b/libcxx/src/tzdb.cpp
index 2c82a4a4317a81..9d06eb920e5cdf 100644
--- a/libcxx/src/tzdb.cpp
+++ b/libcxx/src/tzdb.cpp
@@ -717,8 +717,12 @@ void __init_tzdb(tzdb& __tzdb, __tz::__rules_storage_type& __rules) {
     std::__throw_runtime_error("tzdb: the path '/etc/localtime' is not a symlink");
 
   filesystem::path __tz = filesystem::read_symlink(__path);
-  string __name         = filesystem::relative(__tz, "/usr/share/zoneinfo/");
+  // The path may be a relative path, in that case convert it to an absolute
+  // path based on the proper initial directory.
+  if (__tz.is_relative())
+    __tz = filesystem::canonical("/etc" / __tz);
 
+  string __name = filesystem::relative(__tz, "/usr/share/zoneinfo/");
   if (const time_zone* __result = tzdb.__locate_zone(__name))
     return __result;
 

@mordante
Copy link
Member Author

mordante commented Apr 6, 2024

@mgorny I've tested this locally by using /etc/localtime -> ../usr/share/zoneinfo/Poland and this resolves the issue for me. If possible can you verify this fixes the issue for you?

@mgorny
Copy link
Member

mgorny commented Apr 6, 2024

Confirmed that the tests pass with the patch applied on top of b88a1dd. Thanks!

Copy link
Member

@mgorny mgorny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a formal LGTM, for all it's worth.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I don't know how to test that without mucking around with the base system, which is really annoying.

@mordante mordante merged commit eea3bd3 into llvm:main Apr 9, 2024
@mordante mordante deleted the review/relative_etc_localtime branch April 9, 2024 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libc++:: current_zone.pass.cpp tests fail
4 participants