Skip to content

Prevent accessing unicode scalar element with incorrect index. #1214

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

Conversation

sofiaromorales
Copy link
Contributor

Bug/issue #, if applicable: 150760264

Summary

This change prevents a potential DocC crash by making sure that it does not access an array element using an index that is outside of the bonds of the array.

.index(after: can crash in some cases if the passed index belongs to the last element of the collection. with this change we ensure that the passed index is not the last index.

Testing

Run unit tests in Linux using Swift 5.9.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • [(N/A)] Updated documentation if necessary

Copy link
Contributor

@QuietMisdreavus QuietMisdreavus left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this! I've got a couple comments to add on.

@sofiaromorales sofiaromorales force-pushed the r150760264/percent-encoded-character branch 2 times, most recently from 4f3fc97 to 6fe73e7 Compare May 9, 2025 16:08
@sofiaromorales sofiaromorales force-pushed the r150760264/percent-encoded-character branch from 6fe73e7 to 5b39673 Compare May 9, 2025 16:12
@sofiaromorales
Copy link
Contributor Author

@swift-ci please test

// Create a new string by mapping over the character helps
// preventing crashes when handling substrings that contains
// multibyte characters.
self.map { String($0) }.joined()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change necessary?

If so, what are the performance implications of this? Reading this naively it seems like it could be horribly slow. But I'm not familiar with how Swift implements strings and sub-strings internally. We should run a benchmark to make sure this isn't a hot spot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it back. Linux environments don't like constructing strings from substrings that contains multibyte characters and sometimes it results in crashes, but it's true that this has performance issues.

Copy link
Contributor

@QuietMisdreavus QuietMisdreavus left a comment

Choose a reason for hiding this comment

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

One request, one suggestion, and i'm also interested in @patshaughnessy's question.

@sofiaromorales sofiaromorales marked this pull request as draft May 12, 2025 10:12
@sofiaromorales sofiaromorales force-pushed the r150760264/percent-encoded-character branch from b671925 to 1f92a91 Compare May 12, 2025 12:31
@sofiaromorales
Copy link
Contributor Author

@swift-ci please test

@sofiaromorales sofiaromorales force-pushed the r150760264/percent-encoded-character branch from 1f92a91 to 435b602 Compare May 12, 2025 17:53
@sofiaromorales sofiaromorales force-pushed the r150760264/percent-encoded-character branch from 435b602 to 45919d3 Compare May 13, 2025 14:02
@sofiaromorales sofiaromorales marked this pull request as ready for review May 13, 2025 14:04
Copy link
Contributor

@anferbui anferbui left a comment

Choose a reason for hiding this comment

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

Approved with a comment about the tests

This change prevents a potential DocC crash by making sure that it does not access
an array element using an index that is outside of the bonds of the array.

`.index(after:` can crash in some cases if the passed index
belongs to the last element of the collection. with this change we ensure
that the passed index is not the last index.

rdar://150760264
@sofiaromorales sofiaromorales force-pushed the r150760264/percent-encoded-character branch from 45919d3 to 4babd33 Compare May 14, 2025 13:20
@sofiaromorales
Copy link
Contributor Author

@swift-ci please test

@sofiaromorales sofiaromorales dismissed QuietMisdreavus’s stale review May 14, 2025 13:22

I added the requested changes, thx!

@sofiaromorales sofiaromorales merged commit f47942a into swiftlang:main May 15, 2025
2 checks passed
sofiaromorales added a commit to sofiaromorales/swift-docc that referenced this pull request May 15, 2025
…lang#1214)

This change prevents a potential DocC crash by making sure that it does not access
an array element using an index that is outside of the bonds of the array.

`.index(after:` can crash in some cases if the passed index
belongs to the last element of the collection. with this change we ensure
that the passed index is not the last index.

rdar://150760264
sofiaromorales added a commit that referenced this pull request May 15, 2025
#1219)

This change prevents a potential DocC crash by making sure that it does not access
an array element using an index that is outside of the bonds of the array.

`.index(after:` can crash in some cases if the passed index
belongs to the last element of the collection. with this change we ensure
that the passed index is not the last index.

rdar://150760264
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.

5 participants