Skip to content

[libc++][regex] 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

Merged
merged 2 commits into from
Jul 21, 2025

Conversation

insanustu
Copy link
Contributor

@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
Contributor 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
@frederick-vs-ja frederick-vs-ja changed the title [libc++][regex] Fix #51028 throw exception in the case of wrong range [libc++][regex] Throw exception in the case of wrong range Jul 21, 2025
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.

Thanks! I've slightly modified the PR title to avoid doubly mentioning the issue to fix.

@frederick-vs-ja frederick-vs-ja merged commit 77f0a7d into llvm:main Jul 21, 2025
78 checks passed
Copy link

@insanustu 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!

@insanustu insanustu deleted the fix-regex-bug-51028 branch July 21, 2025 13:11
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
Starting and ending parameters are considered to decide that a range is
a correct one

Fix llvm#51028

Co-authored-by: alexey.lazarev <alexey.lazarev@tasking.com>
Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
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