From 0a46c72bc0b0e5866cebe6f0492ee30f685efd5d Mon Sep 17 00:00:00 2001 From: Alexey Nesterovich Date: Mon, 11 Nov 2024 13:25:14 +0300 Subject: [PATCH 1/3] use null terminated string for userinfo --- Sources/KSCrashRecording/KSCrash.m | 14 +----- Tests/KSCrashRecordingTests/KSCrash_Tests.m | 52 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 Tests/KSCrashRecordingTests/KSCrash_Tests.m diff --git a/Sources/KSCrashRecording/KSCrash.m b/Sources/KSCrashRecording/KSCrash.m index 027d8832..73ba2888 100644 --- a/Sources/KSCrashRecording/KSCrash.m +++ b/Sources/KSCrashRecording/KSCrash.m @@ -170,8 +170,8 @@ - (void)setUserInfo:(NSDictionary *)userInfo } } - const char *userInfoCString = userInfoJSON ? [userInfoJSON bytes] : NULL; - kscrash_setUserInfoJSON(userInfoCString); + NSString *userInfoString = userInfoJSON ? [[NSString alloc] initWithData:userInfoJSON encoding:NSUTF8StringEncoding] : nil; + kscrash_setUserInfoJSON(userInfoString.UTF8String); } - (BOOL)reportsMemoryTerminations @@ -320,16 +320,6 @@ -(TYPE)NAME { return kscrashstate_currentState()->NAME; } #pragma mark - Utility - // ============================================================================ -- (NSMutableData *)nullTerminated:(NSData *)data -{ - if (data == nil) { - return NULL; - } - NSMutableData *mutable = [NSMutableData dataWithData:data]; - [mutable appendBytes:"\0" length:1]; - return mutable; -} - + (NSError *)errorForInstallErrorCode:(KSCrashInstallErrorCode)errorCode { NSString *errorDescription; diff --git a/Tests/KSCrashRecordingTests/KSCrash_Tests.m b/Tests/KSCrashRecordingTests/KSCrash_Tests.m new file mode 100644 index 00000000..b6c6c157 --- /dev/null +++ b/Tests/KSCrashRecordingTests/KSCrash_Tests.m @@ -0,0 +1,52 @@ +// +// KSCrash_Tests.m +// +// Created by Aliaksei Nestsiarovich on 11.11.2024. +// +// Copyright (c) 2012 Karl Stenerud. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall remain in place +// in this source code. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +#import + +#import "KSCrash.h" + +@interface KSCrash_Tests : XCTestCase +@end + +@implementation KSCrash_Tests + +- (void)testUserInfo { + NSDictionary *userInfo = @{ + @"key1": @"value1", + @"key2": @123, + }; + [[KSCrash sharedInstance] setUserInfo:userInfo]; + + XCTAssertEqualObjects([[KSCrash sharedInstance] userInfo], userInfo); +} + +- (void)testUserInfoIfNul { + [[KSCrash sharedInstance] setUserInfo:nil]; + + XCTAssertNil([[KSCrash sharedInstance] userInfo]); +} + +@end From 24746320266a5861da8d02e0de0b1a6d9b941b02 Mon Sep 17 00:00:00 2001 From: Alexey Nesterovich Date: Mon, 11 Nov 2024 14:07:43 +0300 Subject: [PATCH 2/3] fix codestyle --- Sources/KSCrashRecording/KSCrash.m | 4 +++- Tests/KSCrashRecordingTests/KSCrash_Tests.m | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/KSCrashRecording/KSCrash.m b/Sources/KSCrashRecording/KSCrash.m index 73ba2888..858d2d2c 100644 --- a/Sources/KSCrashRecording/KSCrash.m +++ b/Sources/KSCrashRecording/KSCrash.m @@ -170,7 +170,9 @@ - (void)setUserInfo:(NSDictionary *)userInfo } } - NSString *userInfoString = userInfoJSON ? [[NSString alloc] initWithData:userInfoJSON encoding:NSUTF8StringEncoding] : nil; + NSString *userInfoString = userInfoJSON + ? [[NSString alloc] initWithData:userInfoJSON encoding:NSUTF8StringEncoding] + : nil; kscrash_setUserInfoJSON(userInfoString.UTF8String); } diff --git a/Tests/KSCrashRecordingTests/KSCrash_Tests.m b/Tests/KSCrashRecordingTests/KSCrash_Tests.m index b6c6c157..4a1f6595 100644 --- a/Tests/KSCrashRecordingTests/KSCrash_Tests.m +++ b/Tests/KSCrashRecordingTests/KSCrash_Tests.m @@ -33,17 +33,16 @@ @interface KSCrash_Tests : XCTestCase @implementation KSCrash_Tests -- (void)testUserInfo { - NSDictionary *userInfo = @{ - @"key1": @"value1", - @"key2": @123, - }; +- (void)testUserInfo +{ + NSDictionary *userInfo = @{@"key1": @"value1", @"key2": @123}; [[KSCrash sharedInstance] setUserInfo:userInfo]; XCTAssertEqualObjects([[KSCrash sharedInstance] userInfo], userInfo); } -- (void)testUserInfoIfNul { +- (void)testUserInfoIfNul +{ [[KSCrash sharedInstance] setUserInfo:nil]; XCTAssertNil([[KSCrash sharedInstance] userInfo]); From a1b45ef999568e14a1b58cffd94c164866c9e19b Mon Sep 17 00:00:00 2001 From: Alexey Nesterovich Date: Mon, 11 Nov 2024 14:14:48 +0300 Subject: [PATCH 3/3] fix codestyle --- Sources/KSCrashRecording/KSCrash.m | 5 ++--- Tests/KSCrashRecordingTests/KSCrash_Tests.m | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Sources/KSCrashRecording/KSCrash.m b/Sources/KSCrashRecording/KSCrash.m index 858d2d2c..a772cb6b 100644 --- a/Sources/KSCrashRecording/KSCrash.m +++ b/Sources/KSCrashRecording/KSCrash.m @@ -170,9 +170,8 @@ - (void)setUserInfo:(NSDictionary *)userInfo } } - NSString *userInfoString = userInfoJSON - ? [[NSString alloc] initWithData:userInfoJSON encoding:NSUTF8StringEncoding] - : nil; + NSString *userInfoString = + userInfoJSON ? [[NSString alloc] initWithData:userInfoJSON encoding:NSUTF8StringEncoding] : nil; kscrash_setUserInfoJSON(userInfoString.UTF8String); } diff --git a/Tests/KSCrashRecordingTests/KSCrash_Tests.m b/Tests/KSCrashRecordingTests/KSCrash_Tests.m index 4a1f6595..397fd53e 100644 --- a/Tests/KSCrashRecordingTests/KSCrash_Tests.m +++ b/Tests/KSCrashRecordingTests/KSCrash_Tests.m @@ -35,7 +35,7 @@ @implementation KSCrash_Tests - (void)testUserInfo { - NSDictionary *userInfo = @{@"key1": @"value1", @"key2": @123}; + NSDictionary *userInfo = @{ @"key1" : @"value1", @"key2" : @123 }; [[KSCrash sharedInstance] setUserInfo:userInfo]; XCTAssertEqualObjects([[KSCrash sharedInstance] userInfo], userInfo);