Skip to content

Commit

Permalink
SR-5557: Fix NSRegularExpression errors with invalid pattern
Browse files Browse the repository at this point in the history
- The error dictionary in CFRegularExpression was failing with the
  error key being a CFSTR inside an array so initialiase it outside
  of the array.
  • Loading branch information
spevans committed Aug 9, 2017
1 parent 3de37f7 commit a125add
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
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=(}")
}
}
}

0 comments on commit a125add

Please sign in to comment.