Skip to content

Commit 22928dd

Browse files
authored
Fix ubsan issues in GDT (#3910)
1 parent ec63b66 commit 22928dd

File tree

6 files changed

+22
-14
lines changed

6 files changed

+22
-14
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCOREvent.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
@implementation GDTCOREvent
2525

26-
- (instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteger)target {
26+
- (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteger)target {
2727
GDTCORAssert(mappingID.length > 0, @"Please give a valid mapping ID");
2828
GDTCORAssert(target > 0, @"A target cannot be negative or 0");
2929
if (mappingID == nil || mappingID.length == 0 || target <= 0) {

GoogleDataTransport/GDTCORLibrary/GDTCORStorage.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ - (void)storeEvent:(GDTCOREvent *)event {
120120
// Write state to disk.
121121
if (self->_runningInBackground) {
122122
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
123+
NSError *error;
123124
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self
124125
requiringSecureCoding:YES
125-
error:nil];
126+
error:&error];
126127
[data writeToFile:[GDTCORStorage archivePath] atomically:YES];
127128
} else {
128129
#if !defined(TARGET_OS_MACCATALYST)
@@ -217,8 +218,9 @@ - (void)addEventToTrackingCollections:(GDTCORStoredEvent *)event {
217218

218219
- (void)appWillForeground:(GDTCORApplication *)app {
219220
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
221+
NSError *error;
220222
NSData *data = [NSData dataWithContentsOfFile:[GDTCORStorage archivePath]];
221-
[NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCORStorage class] fromData:data error:nil];
223+
[NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCORStorage class] fromData:data error:&error];
222224
} else {
223225
#if !defined(TARGET_OS_MACCATALYST)
224226
[NSKeyedUnarchiver unarchiveObjectWithFile:[GDTCORStorage archivePath]];
@@ -230,9 +232,10 @@ - (void)appWillBackground:(GDTCORApplication *)app {
230232
self->_runningInBackground = YES;
231233
dispatch_async(_storageQueue, ^{
232234
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
235+
NSError *error;
233236
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self
234237
requiringSecureCoding:YES
235-
error:nil];
238+
error:&error];
236239
[data writeToFile:[GDTCORStorage archivePath] atomically:YES];
237240
} else {
238241
#if !defined(TARGET_OS_MACCATALYST)
@@ -258,9 +261,10 @@ - (void)appWillBackground:(GDTCORApplication *)app {
258261

259262
- (void)appWillTerminate:(GDTCORApplication *)application {
260263
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
264+
NSError *error;
261265
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self
262266
requiringSecureCoding:YES
263-
error:nil];
267+
error:&error];
264268
[data writeToFile:[GDTCORStorage archivePath] atomically:YES];
265269
} else {
266270
#if !defined(TARGET_OS_MACCATALYST)

GoogleDataTransport/GDTCORLibrary/GDTCORTransport.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525

2626
@implementation GDTCORTransport
2727

28-
- (instancetype)initWithMappingID:(NSString *)mappingID
29-
transformers:(nullable NSArray<id<GDTCOREventTransformer>> *)transformers
30-
target:(NSInteger)target {
28+
- (nullable instancetype)initWithMappingID:(NSString *)mappingID
29+
transformers:
30+
(nullable NSArray<id<GDTCOREventTransformer>> *)transformers
31+
target:(NSInteger)target {
3132
GDTCORAssert(mappingID.length > 0, @"A mapping ID cannot be nil or empty");
3233
GDTCORAssert(target > 0, @"A target cannot be negative or 0");
3334
if (mappingID == nil || mappingID.length == 0 || target <= 0) {

GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ - (void)packageDelivered:(GDTCORUploadPackage *)package successful:(BOOL)success
242242
[prioritizer packageDelivered:package successful:successful];
243243
}
244244
}
245-
[self.storage removeEvents:package.events];
245+
if (package.events != nil) {
246+
[self.storage removeEvents:package.events];
247+
}
246248
});
247249
}
248250

GoogleDataTransport/GDTCORLibrary/Public/GDTCOREvent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ typedef NS_ENUM(NSInteger, GDTCOREventQoS) {
7979
* @param target The event's target identifier.
8080
* @return An instance of this class.
8181
*/
82-
- (instancetype)initWithMappingID:(NSString *)mappingID
83-
target:(NSInteger)target NS_DESIGNATED_INITIALIZER;
82+
- (nullable instancetype)initWithMappingID:(NSString *)mappingID
83+
target:(NSInteger)target NS_DESIGNATED_INITIALIZER;
8484

8585
/** Returns the GDTCORStoredEvent equivalent of self.
8686
*

GoogleDataTransport/GDTCORLibrary/Public/GDTCORTransport.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ NS_ASSUME_NONNULL_BEGIN
3535
* @param target The target backend of this transport.
3636
* @return A transport that will send events.
3737
*/
38-
- (instancetype)initWithMappingID:(NSString *)mappingID
39-
transformers:(nullable NSArray<id<GDTCOREventTransformer>> *)transformers
40-
target:(NSInteger)target NS_DESIGNATED_INITIALIZER;
38+
- (nullable instancetype)initWithMappingID:(NSString *)mappingID
39+
transformers:
40+
(nullable NSArray<id<GDTCOREventTransformer>> *)transformers
41+
target:(NSInteger)target NS_DESIGNATED_INITIALIZER;
4142

4243
/** Copies and sends an internal telemetry event. Events sent using this API are lower in priority,
4344
* and sometimes won't be sent on their own.

0 commit comments

Comments
 (0)