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-11355] String.range(of...) matches an extra NUL (and returns range outside string boundaries) #53756

swift-ci opened this issue Aug 22, 2019 · 2 comments


Copy link

swift-ci commented Aug 22, 2019

Previous ID SR-11355
Radar None
Original Reporter yonilevy (JIRA User)
Type Bug
  • Swift 5

  • Xcode 10.3

  • MacOS 10.14.6

Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 5026fff9c23d99a0d147ad5d53b56b68

Issue Description:

This is pretty self explanatory:

let str = "bar"
if str.range(of: "r\u{0}")!.upperBound > str.endIndex {

I'd expect:

  1. the `str.range()` to return `nil`
  2. `str.range()` to never return a range outside `str` boundaries
Copy link

belkadan commented Aug 23, 2019

cc @milseman

Copy link

YOCKOW commented Aug 24, 2019

Interestingly, the result seems to depend on the environment.
Given the modified code:

// SR-11355.swift
import Foundation

let str = "bar"
let range = str.range(of: "r\u{0}")

if range == nil {
  print("Not Found.")
} else {
  if range!.upperBound > str.endIndex {
  } else {
    print("What is found?")

You can get

  • "Weird!" with playground on macOS.

  • "Not Found." with REPL on macOS.

  • "Not Found." with `swift SR-11355.swift` on macOS.

  • "Not Found." with `swiftc SR-11355.swift && ./SR-11355` on macOS.

  • "Weird!" with any tools on Linux.

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

No branches or pull requests

3 participants