Skip to content

Commit f0ce60c

Browse files
authored
fix: remove unsafe casts to prevent runtime exceptions (#234)
1 parent 3ca86cb commit f0ce60c

File tree

12 files changed

+122
-13
lines changed

12 files changed

+122
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ fastlane/fastlane-buildlog
9191
iOSInjectionProject/
9292
.DS_Store
9393
*.DS_Store
94+
95+
**/contents.xcworkspacedata

DevCycle.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
52E693EA27567E2600B52375 /* DevCycleService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52E693E927567E2600B52375 /* DevCycleService.swift */; };
5151
52E693ED2756816A00B52375 /* DVCConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52E693EC2756816A00B52375 /* DVCConfig.swift */; };
5252
52E693F62758032500B52375 /* DevCycleServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52E693F52758032500B52375 /* DevCycleServiceTests.swift */; };
53+
52E8C1F12DF87C5A0044783D /* DevCycleEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52E8C1F02DF87C560044783D /* DevCycleEventTests.swift */; };
5354
D9341C12276967DE00BC753F /* DevCycleEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9341C11276967DE00BC753F /* DevCycleEvent.swift */; };
5455
D9341C14276A3C8A00BC753F /* ObjCDevCycleEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9341C13276A3C8A00BC753F /* ObjCDevCycleEvent.swift */; };
5556
E66CF1C82901E9C7008734FD /* LDSwiftEventSource.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = E66CF1C72901E9C7008734FD /* LDSwiftEventSource.xcframework */; };
@@ -127,6 +128,7 @@
127128
52E693E927567E2600B52375 /* DevCycleService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DevCycleService.swift; sourceTree = "<group>"; };
128129
52E693EC2756816A00B52375 /* DVCConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DVCConfig.swift; sourceTree = "<group>"; };
129130
52E693F52758032500B52375 /* DevCycleServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DevCycleServiceTests.swift; sourceTree = "<group>"; };
131+
52E8C1F02DF87C560044783D /* DevCycleEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DevCycleEventTests.swift; sourceTree = "<group>"; };
130132
D9341C11276967DE00BC753F /* DevCycleEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DevCycleEvent.swift; sourceTree = "<group>"; };
131133
D9341C13276A3C8A00BC753F /* ObjCDevCycleEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjCDevCycleEvent.swift; sourceTree = "<group>"; };
132134
E66CF1C72901E9C7008734FD /* LDSwiftEventSource.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = LDSwiftEventSource.xcframework; path = Carthage/Build/LDSwiftEventSource.xcframework; sourceTree = "<group>"; };
@@ -201,6 +203,7 @@
201203
5264A78C2768E87C00FEDB43 /* Models */ = {
202204
isa = PBXGroup;
203205
children = (
206+
52E8C1F02DF87C560044783D /* DevCycleEventTests.swift */,
204207
5268DB68275020D900D17A40 /* DevCycleUserTest.swift */,
205208
524F4E61276D20A500CB9069 /* DVCVariableTests.swift */,
206209
5276C9F1275E6E0D00B9A324 /* DevCycleOptionsTest.swift */,
@@ -463,6 +466,7 @@
463466
buildActionMask = 2147483647;
464467
files = (
465468
529F0CA52774D6BE0075AAB4 /* URLSessionMock.swift in Sources */,
469+
52E8C1F12DF87C5A0044783D /* DevCycleEventTests.swift in Sources */,
466470
52133B2028DDFB260007691D /* RequestConsolidatorTests.swift in Sources */,
467471
5276C9F2275E6E0D00B9A324 /* DevCycleOptionsTest.swift in Sources */,
468472
5264A7902768E9F400FEDB43 /* UserConfigTests.swift in Sources */,

DevCycle/DevCycleClient.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@ public class DevCycleClient {
273273
return
274274
}
275275
do {
276-
let messageDictionary =
276+
guard let messageDictionary =
277277
try JSONSerialization.jsonObject(
278-
with: messageData, options: .fragmentsAllowed) as! [String: Any]
278+
with: messageData, options: .fragmentsAllowed) as? [String: Any] else {
279+
throw SSEMessage.SSEMessageError.messageError("Error serializing sse message to JSON")
280+
}
279281
let sseMessage = try SSEMessage(from: messageDictionary)
280282
if sseMessage.data.type == nil || sseMessage.data.type == "refetchConfig" {
281283
if self?.config?.userConfig?.etag == nil

DevCycle/Models/DevCycleEvent.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ public class DevCycleEvent {
3737
return self
3838
}
3939

40-
public func target(_ target: String) -> EventBuilder {
40+
public func target(_ target: String?) -> EventBuilder {
4141
self.event.target = target
4242
return self
4343
}
4444

45-
public func clientDate(_ clientDate: Date) -> EventBuilder {
45+
public func clientDate(_ clientDate: Date?) -> EventBuilder {
4646
self.event.clientDate = clientDate
4747
return self
4848
}
4949

50-
public func value(_ value: Double) -> EventBuilder {
50+
public func value(_ value: Double?) -> EventBuilder {
5151
self.event.value = value
5252
return self
5353
}
5454

55-
public func metaData(_ metaData: [String:Any]) -> EventBuilder {
55+
public func metaData(_ metaData: [String:Any]?) -> EventBuilder {
5656
self.event.metaData = metaData
5757
return self
5858
}

DevCycle/Models/UserConfig.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum UserConfigError: Error {
1111
case MissingProperty(String)
1212
case InvalidVariableType(String)
1313
case InvalidProperty(String)
14+
case InvalidJson(String)
1415
}
1516

1617
public struct UserConfig {

DevCycle/Networking/DevCycleService.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,14 @@ class DevCycleService: DevCycleServiceProtocol {
164164
// Guard below checks if statusCode exists or not in the response body.
165165
// Only API Errors (http status codes of 4xx/5xx) have the statusCode in the response body, successful API Requests (http status codes of 2xx/3xx) calls will not.
166166
guard responseDataJson["statusCode"] == nil else {
167-
let status = responseDataJson["statusCode"] as! Int
168-
var errorResponse: String
167+
guard let status = responseDataJson["statusCode"] as? Int else {
168+
return
169+
}
170+
var errorResponse: String = ""
169171
if (responseDataJson["message"] is [String]) {
170-
errorResponse = (responseDataJson["message"] as! [String]).joined(separator: ", ")
172+
if let errorArray = (responseDataJson["message"] as? [String]) {
173+
errorResponse = errorArray.joined(separator: ", ")
174+
}
171175
} else {
172176
errorResponse = String(describing: responseDataJson["message"])
173177
}

DevCycle/ObjC/ObjCDevCycleEvent.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public class ObjCDevCycleEvent: NSObject {
4949
eventBuilder = eventBuilder.clientDate(eventDate as Date)
5050
}
5151
if let eventValue = self.value {
52-
eventBuilder = eventBuilder.value(eventValue as! Double)
52+
eventBuilder = eventBuilder.value(eventValue as? Double)
5353
}
5454
if let eventMetaData = self.metaData {
55-
eventBuilder = eventBuilder.metaData(eventMetaData as! [String : Any])
55+
eventBuilder = eventBuilder.metaData(eventMetaData as? [String : Any])
5656
}
5757

5858
do {

DevCycle/SSEConnection.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Handler: EventHandler {
9494
public struct SSEMessage {
9595
enum SSEMessageError: Error, Equatable {
9696
case initError(String)
97+
case messageError(String)
9798
}
9899
struct Data {
99100
var etag: String?

DevCycle/Utils/ProcessConfig.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ internal func processConfig(_ responseData: Data?) -> UserConfig? {
1313
return nil
1414
}
1515
do {
16-
let dictionary = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as! [String:Any]
16+
guard let dictionary = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as? [String:Any] else {
17+
throw UserConfigError.InvalidJson("Error with serializing config data to JSON")
18+
}
1719
return try UserConfig(from: dictionary)
1820
} catch {
1921
Log.error("Failed to decode config: \(error)", tags: ["service", "request"])

DevCycle/Utils/RequestConsolidator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class RequestConsolidator {
6060

6161
func makeLastRequestInQueue(user: DevCycleUser, complete: (() -> Void)?) {
6262
guard let lastRequest = self.requestCallbacks.last?.request else {
63-
print("No last request to make in queue")
63+
Log.debug("No last request to make in queue")
6464
return
6565
}
6666
service.makeRequest(request: lastRequest) { response in

0 commit comments

Comments
 (0)