tr1::regex doesn't match a valid pattern with repetition - by Martin Richter

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


4
0
Sign in
to vote
ID 648543 Comments
Status Closed Workarounds
Type Bug Repros 3
Opened 3/1/2011 12:10:54 AM
Access Restriction Public

Description

The following code matches some numbers followed by a dot.
As you can see the first and the and the second test matches 1 single repetition of 1. and 56.
But the third match with a combined string and a double repetition  fails.

#include "stdafx.h" 
#include <regex> 
#include <string> 
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[]) 
{    
	{
		string ip = "56."; 
		tr1::regex ex("((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\\.){1}"); 		
		bool erg = tr1::regex_match(ip,ex); 
		cout << ip << '\t' << erg << endl;
	}
	{
		string ip = "1."; 
		tr1::regex ex("((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\\.){1}"); 		
		bool erg = tr1::regex_match(ip,ex); 
		cout << ip << '\t' << erg << endl;
	}
	{
		string ip = "56.1."; 
		tr1::regex ex("((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\\.){2}"); 		
		bool erg = tr1::regex_match(ip,ex); 
		cout << ip << '\t' << erg << endl;
	}
	
   return 0; 
} 
Sign in to post a comment.
Posted by Andreas Heyer on 3/8/2011 at 12:19 AM
"We've already fixed it, and the fix will be available in VC11."

WTF??? Guys, that's not a bug in the IDE that you can circumvent with not using it. It's a *security related* bug in the runtime! I thought it was MS's policy to always fix security bugs in its top edge products? Shame on you...

So the only fix will be: Use Boost!
Posted by Martin Richter on 3/2/2011 at 4:27 AM
In fact repetition itself causes the problem.
    test("56." , "(([0-9]{1,2})\\.){1}");
    test("1." , "(([0-9]{1,2})\\.){1}");
    test("56.1.", "(([0-9]{1,2})\\.){2}");

Why is this bug not included in SP1. It is a horrible bug!
Posted by Microsoft on 3/1/2011 at 4:29 PM
Hi,

Thanks for reporting this bug. We've already fixed it, and the fix will be available in VC11. Demonstration:

C:\Temp>type meow.cpp
#include <stdio.h>
#include <ios>
#include <iostream>
#include <ostream>
#include <regex>
#include <string>
using namespace std;

void test(const string& s, const string& reg) {
    cout << "--" << endl;
    cout << "s: " << s << endl;
    cout << "r: " << reg << endl;

    const regex r(reg);

    cout << boolalpha << regex_match(s, r) << endl;
}

int main() {
    printf("%02d.%02d.%05d.%02d\n", _MSC_VER / 100, _MSC_VER % 100, _MSC_FULL_VER % 100000, _MSC_BUILD);

    test("56." , "((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\\.){1}");
    test("1." , "((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\\.){1}");
    test("56.1.", "((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\\.){2}");
}

C:\Temp>cl /EHsc /nologo /W4 /MTd meow.cpp
meow.cpp

[VC10 RTM]
C:\Temp>meow
16.00.30319.01
--
s: 56.
r: ((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\.){1}
true
--
s: 1.
r: ((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\.){1}
true
--
s: 56.1.
r: ((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\.){2}
false

[My current build of VC11]
C:\Temp>meow
17.00.40225.00
--
s: 56.
r: ((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\.){1}
true
--
s: 1.
r: ((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\.){1}
true
--
s: 56.1.
r: ((([01]?[0-9]{1,2})|(2[0-4][0-9]|25[0-5]))\.){2}
true

By the way, it appears that several parentheses in your regex are unnecessary. Example:

C:\Temp>meow
17.00.40225.00
--
s: 56.
r: (([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){1}
true
--
s: 1.
r: (([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){1}
true
--
s: 56.1.
r: (([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){2}
true

(I could also use non-capture groups, but haven't done so.)

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by Microsoft on 3/1/2011 at 1:42 AM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by Microsoft on 3/1/2011 at 12:13 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)