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-13270] NSRegularExpression.numberOfMatches() with bad range doesn't fail #3976

Open
swift-ci opened this issue Jul 22, 2020 · 1 comment

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-13270
Radar None
Original Reporter NPAssoc (JIRA User)
Type Bug
Environment

Raspian Linux Swift version 5.1.5-v0.1

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 957478105529dab8d74d9fcb73634dae

Issue Description:

Given a regular expression like:

let testExp = try! NSRegularExpression(pattern: "(^| /) .", options: [])

The code:

let ss = "/Pairs Series/._Chair15.jpg"

let tRange = NSMakeRange(0, ss.count + 2)

let matches = testExp.numberOfMatches(in: ss, options: [], range: tRange)

fails on the Mac with: '...Range or index out of bounds'

but works under Raspian returning 0 matches.

Under Raspian, it should either fail or return 1 match.

@benrimmington
Copy link
Contributor

When given an invalid range, the internal prepareRegularExpression function returns NULL, which results in a silent failure with zero matches. I'm not sure if this behavior is considered a bug.

Note that your ss.count example gives the number of characters (extended grapheme clusters), but NSRegularExpression needs an NSRange of UTF-16 code units.

let tRange = NSRange(location: 0, length: ss.utf16.count)
let tRange = NSRange(ss.startIndex..., in: ss)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants