Skip to content

Conversation

lbonn
Copy link
Contributor

@lbonn lbonn commented Sep 12, 2025

The outer iterator needs to move to the next segment when calling __compose.

Without this change, find_segment_if would never reach the end of the join_view which caused erroneous result when calling ranges::find on a join_view of bidirectional ranges.

Other specializations using the segmented iterator trait were likely to be affected as well.

Fixes #158279
Fixes #93180

@lbonn lbonn requested a review from a team as a code owner September 12, 2025 18:56
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2025

@llvm/pr-subscribers-libcxx

Author: None (lbonn)

Changes

The outer iterator needs to move to the next segment when calling __compose.

Without this change, find_segment_if would never reach the end of the join_view which caused erroneous result when calling ranges::find on a join_view of bidirectional ranges.

Other specializations using the segmented iterator trait were likely to be affected as well.

Fixes #158279, #93180

cc @philnik777


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

2 Files Affected:

  • (modified) libcxx/include/__ranges/join_view.h (+6-2)
  • (added) libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp (+37)
diff --git a/libcxx/include/__ranges/join_view.h b/libcxx/include/__ranges/join_view.h
index 327b349f476a7..6097754f403ec 100644
--- a/libcxx/include/__ranges/join_view.h
+++ b/libcxx/include/__ranges/join_view.h
@@ -410,8 +410,12 @@ struct __segmented_iterator_traits<_JoinViewIterator> {
 
   static constexpr _LIBCPP_HIDE_FROM_ABI _JoinViewIterator
   __compose(__segment_iterator __seg_iter, __local_iterator __local_iter) {
-    return _JoinViewIterator(
-        std::move(__seg_iter).__get_data(), std::move(__seg_iter).__get_iter(), std::move(__local_iter));
+    auto&& __outer = std::move(__seg_iter).__get_iter();
+    if (__local_iter == ranges::end(*__outer)) {
+      ++__outer;
+      return _JoinViewIterator(*std::move(__seg_iter).__get_data(), __outer);
+    }
+    return _JoinViewIterator(std::move(__seg_iter).__get_data(), __outer, std::move(__local_iter));
   }
 };
 
diff --git a/libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp
new file mode 100644
index 0000000000000..f8bee2227d0b9
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/find.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <algorithm>
+#include <cassert>
+#include <ranges>
+#include <type_traits>
+
+#include "../types.h"
+
+constexpr bool test() {
+  // Test the segmented iterator implementation of join_view
+  // https://github.com/llvm/llvm-project/issues/158279
+  {
+    int buffer1[2][1] = {{1}, {2}};
+    auto joined       = std::views::join(buffer1);
+    assert(std::ranges::find(joined, 1) == std::ranges::begin(joined));
+    assert(std::ranges::find(joined, 2) == std::ranges::next(std::ranges::begin(joined)));
+    assert(std::ranges::find(joined, 3) == std::ranges::end(joined));
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

@lbonn lbonn force-pushed the fix-segmented-it-join_view branch from da8fb07 to 192c920 Compare September 14, 2025 19:10
@lbonn
Copy link
Contributor Author

lbonn commented Sep 14, 2025

I've moved the logic inside of the iterator itself instead of the segmented iterator trait which was meant to be kept simpler (I believe)

@lbonn
Copy link
Contributor Author

lbonn commented Sep 17, 2025

Not sure what is going on with the macos failures, I don't think they are touching these changes.

   Failed Tests (9):
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.match/awk.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.match/basic.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.match/ecma.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.match/extended.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.search/awk.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.search/basic.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.search/ecma.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.alg/re.alg.search/extended.locale.pass.cpp
    llvm-libc++-shared.cfg.in :: std/re/re.traits/lookup_collatename.pass.cpp

They already have some XFAIL on some other linux and freebsd targets.

@lbonn lbonn force-pushed the fix-segmented-it-join_view branch from 192c920 to d8fb715 Compare September 19, 2025 08:06
@lbonn lbonn requested a review from philnik777 September 19, 2025 08:08
@lbonn lbonn force-pushed the fix-segmented-it-join_view branch from d8fb715 to 5f3e1db Compare September 19, 2025 08:09
The outer iterator needs to move to the next segment when calling
__compose.

Without this change, `find_segment_if` would never reach the end of the
join_view which caused erroneous result when calling `ranges::find` on a
join_view of bidirectional ranges.
@lbonn lbonn force-pushed the fix-segmented-it-join_view branch from 5f3e1db to b41db68 Compare September 24, 2025 12:54
Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

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

Thanks! Do you need me to commit this?

@lbonn
Copy link
Contributor Author

lbonn commented Sep 24, 2025

@philnik777 yes thank you, I would need your help for that.

In my opinion it is also worth a backport, I will request it through llvmbot once it is merged.

Copy link
Contributor

@frederick-vs-ja frederick-vs-ja left a comment

Choose a reason for hiding this comment

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

LGTM! Merging as CI failures are unrelated.

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Sep 27, 2025
@frederick-vs-ja frederick-vs-ja merged commit d1b5607 into llvm:main Sep 27, 2025
76 of 78 checks passed
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Sep 27, 2025
Copy link

@lbonn Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@frederick-vs-ja
Copy link
Contributor

/cherry-pick d1b5607

@llvmbot
Copy link
Member

llvmbot commented Sep 27, 2025

/pull-request #161008

@frederick-vs-ja
Copy link
Contributor

frederick-vs-ja commented Sep 27, 2025

@lbonn Could you make you mail address public for GitHub for next contribution(s)? We conventionally expect public mail address on GitHub.

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

@lbonn
Copy link
Contributor Author

lbonn commented Sep 27, 2025

@frederick-vs-ja oh no, sorry about that! I've made the change for next time.

mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…vm#158347)

The outer iterator needs to move to the next segment when calling
__compose.

Without this change, `find_segment_if` would never reach the end of the
join_view which caused erroneous result when calling `ranges::find` on a
join_view of bidirectional ranges.

Other specializations using the segmented iterator trait were likely to
be affected as well.

Fixes llvm#158279
Fixes llvm#93180
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Oct 6, 2025
…vm#158347)

The outer iterator needs to move to the next segment when calling
__compose.

Without this change, `find_segment_if` would never reach the end of the
join_view which caused erroneous result when calling `ranges::find` on a
join_view of bidirectional ranges.

Other specializations using the segmented iterator trait were likely to
be affected as well.

Fixes llvm#158279
Fixes llvm#93180

(cherry picked from commit d1b5607)
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
Development

Successfully merging this pull request may close these issues.

[libc++] Incorrect behavior of ranges::find with join_view Invalid iterators for empty std::ranges::join_view
4 participants