Use NSData rather than NSString to make this more general-purpose, as requested in review

This commit is contained in:
Nacho Bonafonte 2020-09-09 13:22:09 +02:00
Родитель 6bb6f07d60
Коммит bfdf6ba169
11 изменённых файлов: 31 добавлений и 20 удалений

Просмотреть файл

@ -225,7 +225,7 @@ plcrash_error_t plcrash_log_writer_init (plcrash_log_writer_t *writer,
BOOL user_requested);
void plcrash_log_writer_set_exception (plcrash_log_writer_t *writer, NSException *exception);
void plcrash_log_writer_set_custom_data (plcrash_log_writer_t *writer, NSString *custom_data);
void plcrash_log_writer_set_custom_data (plcrash_log_writer_t *writer, NSData *custom_data);
plcrash_error_t plcrash_log_writer_write (plcrash_log_writer_t *writer,
thread_t crashed_thread,

Просмотреть файл

@ -252,6 +252,12 @@ enum {
PLCRASH_PROTO_CUSTOM_DATA_ID = 10,
};
static void plprotobuf_cbinary_data_init (PLProtobufCBinaryData *data, const void *pointer, size_t len) {
data->data = malloc(len);
memcpy(data->data , pointer, len);
data->len = len;
}
static void plprotobuf_cbinary_data_string_init (PLProtobufCBinaryData *data, const char *value) {
data->data = (void *)value;
data->len = strlen(value);
@ -529,7 +535,7 @@ void plcrash_log_writer_set_exception (plcrash_log_writer_t *writer, NSException
*
* @warning This function is not async safe, and must be called outside of a signal handler.
*/
void plcrash_log_writer_set_custom_data (plcrash_log_writer_t *writer, NSString *custom_data) {
void plcrash_log_writer_set_custom_data (plcrash_log_writer_t *writer, NSData *custom_data) {
/* If there is already user data, delete it */
if (writer->custom_data.data) {
plprotobuf_cbinary_data_free(&writer->custom_data);
@ -537,7 +543,7 @@ void plcrash_log_writer_set_custom_data (plcrash_log_writer_t *writer, NSString
/* Save the user data */
if (custom_data != nil) {
plprotobuf_cbinary_data_nsstring_init(&writer->custom_data, custom_data);
plprotobuf_cbinary_data_init(&writer->custom_data, custom_data.bytes, custom_data.length);
}
}

Просмотреть файл

@ -113,7 +113,7 @@ typedef struct _PLCrashReportDecoder _PLCrashReportDecoder;
__strong PLCrashReportExceptionInfo *_exceptionInfo;
/** User defined information (may be nil) */
__strong NSString *_customData;
__strong NSData *_customData;
/** Report UUID */
CFUUIDRef _uuid;
@ -197,7 +197,7 @@ typedef struct _PLCrashReportDecoder _PLCrashReportDecoder;
* Custom user data. Only available if user explicitly assigned it before crash happened,
* otherwise nil.
*/
@property(nonatomic, readonly, strong) NSString *customData;
@property(nonatomic, readonly, strong) NSData *customData;
/**
* A client-generated 16-byte UUID. May be used to filter duplicate reports submitted or generated

Просмотреть файл

@ -167,8 +167,8 @@ static void populate_nserror (NSError **error, PLCrashReporterError code, NSStri
}
/* Custom data, if it is available */
if (_decoder->crashReport->custom_data != NULL) {
_customData = [NSString stringWithUTF8String: _decoder->crashReport->custom_data];
if (_decoder->crashReport->has_custom_data) {
_customData = [NSData dataWithBytes:_decoder->crashReport->custom_data.data length:_decoder->crashReport->custom_data.len];
if (!_customData)
goto error;
}

Просмотреть файл

@ -1303,8 +1303,8 @@ static const ProtobufCFieldDescriptor plcrash__crash_report__field_descriptors[1
"custom_data",
10,
PROTOBUF_C_LABEL_OPTIONAL,
PROTOBUF_C_TYPE_STRING,
0, /* quantifier_offset */
PROTOBUF_C_TYPE_BYTES,
offsetof(Plcrash__CrashReport, has_custom_data),
offsetof(Plcrash__CrashReport, custom_data),
NULL,
NULL,

Просмотреть файл

@ -563,11 +563,12 @@ struct _Plcrash__CrashReport
/*
* Custom data. Can be used by user to store contextual information for the crash.
*/
char *custom_data;
protobuf_c_boolean has_custom_data;
ProtobufCBinaryData custom_data;
};
#define PLCRASH__CRASH_REPORT__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&plcrash__crash_report__descriptor) \
, NULL, NULL, 0,NULL, 0,NULL, NULL, NULL, NULL, NULL, NULL, NULL }
, NULL, NULL, 0,NULL, 0,NULL, NULL, NULL, NULL, NULL, NULL, 0, {0,NULL} }
/* Plcrash__CrashReport__Processor methods */

Просмотреть файл

@ -362,5 +362,5 @@ message CrashReport {
optional ReportInfo report_info = 9;
/* Custom data. Can be used by user to store contextual information for the crash. */
optional string custom_data = 10;
optional bytes custom_data = 10;
}

Просмотреть файл

@ -134,6 +134,6 @@ typedef struct PLCrashReporterCallbacks {
- (void) setCrashCallbacks: (PLCrashReporterCallbacks *) callbacks;
@property(nonatomic, strong) NSString *customData;
@property(nonatomic, strong) NSData *customData;
@end

Просмотреть файл

@ -835,7 +835,7 @@ cleanup:
*
* @param customData A string with the custom data to save.
*/
- (void) setCustomData: (NSString *) customData {
- (void) setCustomData: (NSData *) customData {
_customData = customData;
plcrash_log_writer_set_custom_data(&signal_handler_context.writer, customData);
}

Просмотреть файл

@ -272,9 +272,11 @@
}
- (void) checkCustomData: (Plcrash__CrashReport *) crashReport {
char *customData = crashReport->custom_data;
STAssertNotNULL(customData, @"No custom data was written");
STAssertTrue(strcmp(customData, "DummyInfo") == 0, @"Custom data was not correctly serialized");
STAssertTrue(crashReport->has_custom_data, @"No custom data was written");
ProtobufCBinaryData customData = crashReport->custom_data;
NSData *data = [NSData dataWithBytes:customData.data length:customData.len];
NSString *dataString =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
STAssertTrue([dataString isEqualToString:@"DummyInfo"], @"Custom data was not correctly serialized");
}
- (Plcrash__CrashReport *) loadReport {
@ -379,7 +381,7 @@
plcrash_log_writer_set_exception(&writer, e);
/* Set user defined data */
plcrash_log_writer_set_custom_data(&writer, @"DummyInfo");
plcrash_log_writer_set_custom_data(&writer, [@"DummyInfo" dataUsingEncoding:NSUTF8StringEncoding]);
/* Write the crash report */
STAssertEquals(PLCRASH_ESUCCESS, plcrash_log_writer_write(&writer, thread, &image_list, &file, &info, &thread_state), @"Crash log failed");

Просмотреть файл

@ -123,7 +123,8 @@ static plcrash_error_t plcr_live_report_callback (plcrash_async_thread_state_t *
plcrash_log_writer_set_exception(&writer, exception);
/* Set user defined data */
plcrash_log_writer_set_custom_data(&writer, @"DummyInfo");
NSData *customData = [@"DummyInfo" dataUsingEncoding:NSUTF8StringEncoding];
plcrash_log_writer_set_custom_data(&writer, customData);
/* Provide binary image info */
plcrash_nasync_image_list_init(&image_list, mach_task_self());
@ -232,7 +233,8 @@ static plcrash_error_t plcr_live_report_callback (plcrash_async_thread_state_t *
/* Custom data */
STAssertNotNil(crashLog.customData, @"No custom data");
STAssertEqualStrings(crashLog.customData, @"DummyInfo", @"Incorrect custom data");
NSString *dataString = [[NSString alloc] initWithData:crashLog.customData encoding:NSUTF8StringEncoding];
STAssertTrue([dataString isEqualToString:@"DummyInfo"], @"Incorrect custom data");
/* Thread info */
STAssertNotNil(crashLog.threads, @"Thread list is nil");