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-12578] Data.subscript() crashes on valid access #4390

Closed
swift-ci opened this issue Apr 13, 2020 · 3 comments
Closed

[SR-12578] Data.subscript() crashes on valid access #4390

swift-ci opened this issue Apr 13, 2020 · 3 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-12578
Radar None
Original Reporter nsabovic (JIRA User)
Type Bug
Status Closed
Resolution Invalid
Environment

Xcode 11.4 on macOS 10.15.4

Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
Target: x86_64-apple-darwin19.4.0

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

md5: f612f4b8f118de9fb060ad3aeb286e34

Issue Description:

On Data instances which reference other instances, subscript crashes on valid indices. For example:

import Foundation

let original = Data([1, 2, 3])
let advanced = original.advanced(by: 1) // .count = 2
let dropped = original.dropFirst().     // .count = 2

let thisWorks = advanced[0]
let thisCrashes = dropped[0]
let thisAlsoWorks = dropped.first!

I am not familiar with Foundation source code, but I see that in __DataStorage.get, the effective index is calculated as index - offset. Shouldn't that be index + offset?

@swift-ci
Copy link
Contributor Author

Comment by Nebojsa Sabovic (JIRA)

Looking at how it behaves, it's as if the offset isn't applied at all, just the start/end checks:

import Foundation

let dropped = Data([1, 2, 3]).dropFirst()

assert(dropped[1] == 2)
assert(dropped[2] == 3)

// These crash
dropped[0]
dropped[3]

@swift-ci
Copy link
Contributor Author

Comment by Nebojsa Sabovic (JIRA)

So... just found out about data.startIndex... again. By looking through my own old code.

This is so unintuitive to me, that I really think that it should be mentioned more prominently in the documentation. Feel free to close this issue.

@beccadax
Copy link
Contributor

@swift-ci create

@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
This issue was closed.
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