Skip to content

[libc++][regex] Fix #51028 throw exception in the case of wrong range #148231

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

insanustu
Copy link

@insanustu insanustu commented Jul 11, 2025

Starting and ending parameters are considered to decide that a range is a correct one

Fix #51028

@insanustu insanustu requested a review from a team as a code owner July 11, 2025 11:44
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 Jul 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2025

@llvm/pr-subscribers-libcxx

Author: Lazarev Alexei (insanustu)

Changes

Starting and ending parameters are considered to decide that a range is a correct one


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

2 Files Affected:

  • (modified) libcxx/include/regex (+1-1)
  • (modified) libcxx/test/std/re/re.regex/re.regex.construct/bad_range.pass.cpp (+1)
diff --git a/libcxx/include/regex b/libcxx/include/regex
index bbc21e244dd17..958168a88535d 100644
--- a/libcxx/include/regex
+++ b/libcxx/include/regex
@@ -2120,7 +2120,7 @@ public:
       __ranges_.push_back(
           std::make_pair(__traits_.transform(__b.begin(), __b.end()), __traits_.transform(__e.begin(), __e.end())));
     } else {
-      if (__b.size() != 1 || __e.size() != 1)
+      if (__b.size() != 1 || __e.size() != 1 || __b[0] > __e[0])
         std::__throw_regex_error<regex_constants::error_range>();
       if (__icase_) {
         __b[0] = __traits_.translate_nocase(__b[0]);
diff --git a/libcxx/test/std/re/re.regex/re.regex.construct/bad_range.pass.cpp b/libcxx/test/std/re/re.regex/re.regex.construct/bad_range.pass.cpp
index 023bc0ee79f44..f7ccf2d334022 100644
--- a/libcxx/test/std/re/re.regex/re.regex.construct/bad_range.pass.cpp
+++ b/libcxx/test/std/re/re.regex/re.regex.construct/bad_range.pass.cpp
@@ -33,6 +33,7 @@ int main(int, char**)
 {
     assert(error_range_thrown("([\\w-a])"));
     assert(error_range_thrown("([a-\\w])"));
+    assert(error_range_thrown("([w-a])"));
 
   return 0;
 }

@frederick-vs-ja
Copy link
Contributor

Thanks! I edited the PR description to associate this PR with the issue to fix.

Copy link

github-actions bot commented Jul 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@insanustu insanustu force-pushed the fix-regex-bug-51028 branch 2 times, most recently from 390e012 to 988130c Compare July 11, 2025 12:12
@insanustu insanustu force-pushed the fix-regex-bug-51028 branch from 988130c to 70ff63d Compare July 11, 2025 12:26
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, thanks for the fix!

@@ -2120,7 +2120,7 @@ public:
__ranges_.push_back(
std::make_pair(__traits_.transform(__b.begin(), __b.end()), __traits_.transform(__e.begin(), __e.end())));
} else {
if (__b.size() != 1 || __e.size() != 1)
if (__b.size() != 1 || __e.size() != 1 || __b[0] > __e[0])
Copy link
Member

Choose a reason for hiding this comment

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

Should we be using char_traits::lt to perform this comparison though?

Copy link
Author

Choose a reason for hiding this comment

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

Done

@insanustu insanustu force-pushed the fix-regex-bug-51028 branch from 70ff63d to 09f38bc Compare July 15, 2025 18:08
@ldionne
Copy link
Member

ldionne commented Jul 18, 2025

This can be merged once CI is green (whoever sees it first).

@ldionne ldionne added the pending-ci Merging the PR is only pending completion of CI label Jul 18, 2025
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. pending-ci Merging the PR is only pending completion of CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No exception is thrown when an invalid character range (e.g., [b-a]) is included.
4 participants