Skip to content

<regex>: Character ranges with collating symbol bounds are rejected #5391

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
muellerj2 opened this issue Apr 5, 2025 · 0 comments
Open
Labels
bug Something isn't working regex meow is a substring of homeowner

Comments

@muellerj2
Copy link
Contributor

muellerj2 commented Apr 5, 2025

The regex parser currently rejects ranges in character classes the bounds of which are given by collating symbols.

Test case

#include <algorithm>
#include <locale>
#include <regex>
#include <string>

using namespace std;

class collating_symbols_regex_traits : public regex_traits<char> {

public:
    template <class FwdIt>
    string_type lookup_collatename(FwdIt first, FwdIt last) const {
        // from Hungarian
        const string_type collating_symbol1 = "cs";
        const string_type collating_symbol2 = "dzs";

        if (std::equal(first, last, begin(collating_symbol1), end(collating_symbol1))) {
            return collating_symbol1;
        }

        if (std::equal(first, last, begin(collating_symbol2), end(collating_symbol2))) {
            return collating_symbol2;
        }

        return regex_traits::lookup_collatename(first, last);
    }
};

int main() {
    basic_regex<char, collating_symbols_regex_traits> r;
    r.imbue(locale("hu_HU"));
    try {
        r.assign("[[.cs.]-[.dzs.]]");
        printf("construction succeeded");
    } catch (const regex_error& e) {
        printf("regex error thrown");
    }
    return 0;
}

No Godbolt link: This succeeds there because the latest provided MSVC STL doesn't include the fix for #4995 yet.

The construction of the regex object should succeed, but this actually throws a regex_error with code error_range.

Note: Fixing this for multi-character collating symbols requires changes to the layout of the NFA node that represents character classes. I think this is doable without breaking ABI, but the fix has to be carefully thought through.

@StephanTLavavej StephanTLavavej added bug Something isn't working regex meow is a substring of homeowner labels Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regex meow is a substring of homeowner
Projects
None yet
Development

No branches or pull requests

2 participants