Skip to content

<regex>: match_results are not filled correctly when regex_match() and regex_search() are called on a default-constructed basic_regex #6181

@muellerj2

Description

@muellerj2

Describe the bug

N5032 [re.alg.match]/2-3 specify for regex_match():

Effects: Determines whether there is some sub-sequence within [first, last) that matches the regular expression e. [...] Returns true if such a sequence exists, false otherwise.
Postconditions: m.ready() == true in all cases. If the function returns false, then the effect on [match_results] parameter m is unspecified except that m.size() returns 0 and m.empty() returns true.

Furthermore, the default constructor of basic_regex satisfies the following postcondition [re.regex.construct]/1:

Postconditions: *this does not match any character sequence.

This means that m.ready() should be true and m.size() should be 0 for argument m when regex_match() is called on a default-constructed basic_regex, but the current implementation fails to adjust the match_results internals accordingly.

Similar behavior is specified for regex_search().

Test case

#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main() {

    const string input = "abc";
    {
        smatch sm;
        regex re;
        regex_match(input, sm, re);

        cout << "match_results ready: " << sm.ready() << '\n';
        cout << "match_results size: " << sm.size() << '\n';
    }

    {
        smatch sm;
        regex re;
        regex re2("abc");
        regex_match(input, sm, re2);

        regex_match(input, sm, re);

        cout << "match_results ready: " << sm.ready() << '\n';
        cout << "match_results size: " << sm.size() << '\n';
    }
}

This program currently prints:

match_results ready: 0
match_results size: 0
match_results ready: 1
match_results size: 1

https://godbolt.org/z/5j543r517 (which also shows that the same bug is present in libstdc++)

Expected behavior

The program should print (as it does using libc++):

match_results ready: 1
match_results size: 0
match_results ready: 1
match_results size: 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!regexmeow is a substring of homeowner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions