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-13323] Taking a slice of a Data slice in Swift crashes #3246

Closed
pmuller mannequin opened this issue Jul 30, 2020 · 3 comments
Closed

[SR-13323] Taking a slice of a Data slice in Swift crashes #3246

pmuller mannequin opened this issue Jul 30, 2020 · 3 comments
Assignees

Comments

@pmuller
Copy link
Mannequin

pmuller mannequin commented Jul 30, 2020

Previous ID SR-13323
Radar None
Original Reporter @pmuller
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Environment

Reproduced with:

  • Xcode 11.6 (11E708)

  • Xcode 12.0 beta 3 (12A8169g)

  • Command-line Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53) Target: x86_64-apple-darwin19.6.0

  • Command-line Swift version 5.3 (swiftlang-1200.0.22.4 clang-1200.0.25.1) Target: x86_64-apple-darwin19.6.0

  • Swift Playgrounds 3.3.1 (1167.21.3) on iPadOS 13.6

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

md5: 1e5fea47d1e68dd76f317e554d2f5a49

Issue Description:

The following application code crashes on the last line when compiled with Swift:

    import Foundation
    let data = Data(repeating: 0, count: 512)
    let slice = data[52..<data.count]
    let result = slice[7..<14]

Steps:

  1. Compile the above code snippet
  2. Run the result

Expected:
The code executes without crashing.

Actually:
The code crashes on the last line.

Reported as FB8212804.

@pmuller
Copy link
Mannequin Author

pmuller mannequin commented Jul 30, 2020

data-slice-crash.py generates data-slice-crash.swift with various things I tried, and demonstrates a workaround for the crash.

To reproduce the crash run on the command line:

$ swift data-slice-crash.swift 

@pmuller
Copy link
Mannequin Author

pmuller mannequin commented Aug 11, 2020

Reading https://harshil.net/blog/swift-sequence-collection-array

One thing to keep in mind when using them is that subsequences share indices with their parent collections.

So this may indeed be intended behaviour and not a bug. Not very intuitive though.

@pmuller
Copy link
Mannequin Author

pmuller mannequin commented Aug 17, 2020

Confirmed by Apple Engineering in FB:

The lower index of a Slice is not necessarily 0. If a Slice is created by a subscript operation on Data, like data[52..<data.count], the lower index of the Slice is itself 52. So taking the subscript of that Slice with index 7 is an out-of-bounds access. That’s the crash observed here. Unfortunately, this is confusing because there is no error message displayed.

Resolving as Invalid.

@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

0 participants