Skip to content
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

SR-5557: Fix NSRegularExpression errors with invalid pattern #1164

Merged
merged 1 commit into from Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions CoreFoundation/String.subproj/CFRegularExpression.c
Expand Up @@ -133,8 +133,9 @@ _CFRegularExpressionRef _CFRegularExpressionCreate(CFAllocatorRef allocator, CFS
if (regex == NULL || U_FAILURE(errorCode)) {
// ??? do we need more detailed errors here?
if (errorPtr) {
CFStringRef keys[] = {
CFSTR("NSInvalidValue")
CFStringRef key = CFSTR("NSInvalidValue");
CFTypeRef keys[] = {
key
};
CFTypeRef values[] = {
pattern
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSRegularExpression.swift
Expand Up @@ -85,7 +85,7 @@ open class NSRegularExpression: NSObject, NSCopying, NSCoding {
if let regex = _CFRegularExpressionCreate(kCFAllocatorSystemDefault, pattern._cfObject, opt, &error) {
_internal = regex
} else {
throw error!.takeRetainedValue()._nsObject
throw error!.takeRetainedValue()
}
}

Expand Down
11 changes: 11 additions & 0 deletions TestFoundation/TestNSRegularExpression.swift
Expand Up @@ -29,6 +29,7 @@ class TestNSRegularExpression : XCTestCase {
("test_Equal", test_Equal),
("test_NSCoding", test_NSCoding),
("test_defaultOptions", test_defaultOptions),
("test_badPattern", test_badPattern),
]
}

Expand Down Expand Up @@ -372,4 +373,14 @@ class TestNSRegularExpression : XCTestCase {
let str = NSMutableString(string: text)
XCTAssertEqual(regex!.replaceMatches(in: str, range: range, withTemplate: "$1-$2-$3"), 1)
}

func test_badPattern() {
do {
_ = try NSRegularExpression(pattern: "(", options: [])
XCTFail()
} catch {
let err = String(describing: error)
XCTAssertEqual(err, "Error Domain=NSCocoaErrorDomain Code=2048 \"(null)\" UserInfo={NSInvalidValue=(}")
}
}
}