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

[stdlib] String.index(before:): Fix index validation issues #41598

Merged
merged 2 commits into from Mar 2, 2022

Conversation

lorentey
Copy link
Member

@lorentey lorentey commented Mar 1, 2022

String.index(before:) (and methods that rely on it, such as Substring.index(before:), .distance(from:to:) etc.) does not currently verify that the given index falls before the endIndex before aligning it to a scalar boundary. This allows an out-of-bounds memory access when the provided index points to a position beyond the end of self’s storage.

Additionally, the i > startIndex check needs to be done after scalar alignment, not before, as alignment can round the index down to startIndex.

rdar://89497074&89495373

`String.index(before:)` (and methods that rely on it, such as `Substring.index(before:)`, `.distance(from:to:)` etc.) does not currently verify that the given index falls before the `endIndex` before aligning it to a scalar boundary. This allows an out-of-bounds memory access when the provided index points to a position beyond the end of `self`’s storage.

Additionally, the `i > startIndex` check needs to be done after scalar alignment, not before, as alignment can round the index down to `startIndex`.

rdar://89497074&89495373
@lorentey lorentey requested review from milseman and Azoy March 1, 2022 02:13
@lorentey
Copy link
Member Author

lorentey commented Mar 1, 2022

@swift-ci test

@lorentey
Copy link
Member Author

lorentey commented Mar 1, 2022

@swift-ci test

Copy link
Member

@Azoy Azoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look good to me, thanks!

lorentey added a commit to lorentey/swift that referenced this pull request Mar 1, 2022
`String.UnicodeScalarView` currently lacks proper index validation in its `index(after:)` and `index(before:)` methods, leading to out-of-bounds memory accesses when index navigation methods in this view are given invalid indices.

(Also see apple#41598 and apple#41417)

rdar://89498541
@lorentey
Copy link
Member Author

lorentey commented Mar 1, 2022

Ah, macOS failed due to an infrastructure issue.

@swift-ci test macOS platform

@lorentey
Copy link
Member Author

lorentey commented Mar 1, 2022

@swift-ci test macOS platform

@lorentey lorentey merged commit 20c293e into apple:main Mar 2, 2022
@lorentey lorentey deleted the string-index-validation branch March 2, 2022 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants