Replace force cast with safe conditional cast#455
Open
jcarroll3 wants to merge 1 commit intodropbox:masterfrom
Open
Replace force cast with safe conditional cast#455jcarroll3 wants to merge 1 commit intodropbox:masterfrom
jcarroll3 wants to merge 1 commit intodropbox:masterfrom
Conversation
fe5dab8 to
74ea4a7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
caseInsensitiveLookupiterates over an [AnyHashable: Any] dictionary (from HTTPURLResponse.allHeaderFields) and force-casts each key toStringwithas!. While HTTP header keys are typically strings,AnyHashablecan technically hold non-string keys, and a force cast crash in production is never acceptable in an SDK consumed by third-party apps.This change uses
guard let keyString = key as? String else { continue }to safely skip any non-string keys instead of crashing.Why it should land: A force unwrap crash in a public SDK is a serious reliability risk. Even if non-string header keys are rare, the safe cast prevents a potential crash in consumer apps. This follows Swift best practices for defensive coding in library code.