Skip to content

(142076445) Allow URL.standardized to return an empty string URL #1110

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

Merged
merged 2 commits into from
Jan 3, 2025

Conversation

jrflat
Copy link
Contributor

@jrflat jrflat commented Jan 3, 2025

Calling URL.standardized on a URL like ../../../../ removes the dot segments and produces an empty string. NSURL allows the empty string, and in the old implementation, this would return a URL that wraps an empty string NSURL.

In the new implementation, we ultimately call URL(string: "") when initializing this standardized URL, which returns nil again after #1103. Since URL.standardized returns a non-nil URL, this would crash when unwrapped.

We can allow URL.standardized (and other methods that call URLComponents.url) to return an empty string URL as they did previously by using URL(string:relativeTo:) instead.

@jrflat
Copy link
Contributor Author

jrflat commented Jan 3, 2025

@swift-ci please test

@jrflat jrflat requested review from jmschonfeld and itingliu January 3, 2025 17:38
@itingliu
Copy link
Contributor

itingliu commented Jan 3, 2025

Would it make sense to avoid force unwrapping before returning from standardized at all in case there are other fallout paths? URL(string:relativeTo:) returns an optional after all, so the chance of invalid force unwrap is still there.

@jrflat
Copy link
Contributor Author

jrflat commented Jan 3, 2025

Yeah, I was thinking it could limit our ability to find other cases, but that is probably the safer approach, and I could always re-add the force-unwrap when testing inputs at desk. I'll add a ?? self.

@jrflat
Copy link
Contributor Author

jrflat commented Jan 3, 2025

@swift-ci please test

@jrflat
Copy link
Contributor Author

jrflat commented Jan 3, 2025

@swift-ci please test

@jrflat jrflat merged commit 61811e3 into swiftlang:main Jan 3, 2025
3 checks passed
iCharlesHu pushed a commit to iCharlesHu/swift-foundation that referenced this pull request Jan 7, 2025
…ftlang#1110)

* (142076445) Allow URL.standardized to return an empty string URL

* Add ?? self to prevent force-unwrap
jrflat added a commit to jrflat/swift-foundation that referenced this pull request Mar 5, 2025
…ftlang#1110)

* (142076445) Allow URL.standardized to return an empty string URL

* Add ?? self to prevent force-unwrap
parkera pushed a commit that referenced this pull request Mar 5, 2025
* (141549683) Restore behavior of URL(string: "") returning nil (#1103)

* (142076445) Allow URL.standardized to return an empty string URL (#1110)

* (142076445) Allow URL.standardized to return an empty string URL

* Add ?? self to prevent force-unwrap

* (142446243) Compatibility behaviors for Swift URL (#1113)

* (142589056) URLComponents.string should percent-encode colons in first path segment if needed (#1117)

* (142667792) URL.absoluteString crashes if baseURL starts with colon (#1119)

* (143159003) Don't encode colon if URLComponents path starts with colon (#1139)
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.

2 participants