From d3c2594883ff9aaadd387158b51fbad5dffb8624 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Mon, 16 Jan 2017 21:57:21 -0800 Subject: Fix test errors on string copy --- src/objective-c/GRPCClient/private/GRPCWrappedCall.m | 14 +++++++++----- src/objective-c/GRPCClient/private/NSDictionary+GRPC.m | 4 +++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m index 7e73495043..43c564552b 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m @@ -215,11 +215,15 @@ __weak typeof(self) weakSelf = self; _handler = ^{ __strong typeof(self) strongSelf = weakSelf; - NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode - details:(char*)GRPC_SLICE_START_PTR(strongSelf->_details)]; - NSDictionary *trailers = [NSDictionary - grpc_dictionaryFromMetadataArray:strongSelf->_trailers]; - handler(error, trailers); + if (strongSelf) { + char *details = grpc_slice_to_c_string(strongSelf->_details); + NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode + details:details]; + NSDictionary *trailers = [NSDictionary + grpc_dictionaryFromMetadataArray:strongSelf->_trailers]; + handler(error, trailers); + gpr_free(details); + } }; } } diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m index f834fef5e9..feb2bb5ed8 100644 --- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m +++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m @@ -88,8 +88,10 @@ + (instancetype)grpc_dictionaryFromMetadata:(grpc_metadata *)entries count:(size_t)count { NSMutableDictionary *metadata = [NSMutableDictionary dictionaryWithCapacity:count]; for (grpc_metadata *entry = entries; entry < entries + count; entry++) { - NSString *name = [NSString stringWithCString:(char*)GRPC_SLICE_START_PTR(entry->key) + char *key = grpc_slice_to_c_string(entry->key); + NSString *name = [NSString stringWithCString:key encoding:NSASCIIStringEncoding]; + gpr_free(key); if (!name || metadata[name]) { // Log if name is nil? continue; -- cgit v1.2.3