Skip to content

Commit

Permalink
use null terminated string for userinfo (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
NesterovichAlexey authored Nov 11, 2024
1 parent 4b108d1 commit 3e90345
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
15 changes: 3 additions & 12 deletions Sources/KSCrashRecording/KSCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ - (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
Expand Down Expand Up @@ -320,16 +321,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;
Expand Down
51 changes: 51 additions & 0 deletions Tests/KSCrashRecordingTests/KSCrash_Tests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// 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 <XCTest/XCTest.h>

#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

0 comments on commit 3e90345

Please sign in to comment.