diff --git a/src/core/ext/transport/chttp2/transport/hpack_parser.c b/src/core/ext/transport/chttp2/transport/hpack_parser.c index 3c7d5e83104..5dabe272d39 100644 --- a/src/core/ext/transport/chttp2/transport/hpack_parser.c +++ b/src/core/ext/transport/chttp2/transport/hpack_parser.c @@ -1511,7 +1511,7 @@ static grpc_error *begin_parse_string(grpc_exec_ctx *exec_ctx, const uint8_t *cur, const uint8_t *end, uint8_t binary, grpc_chttp2_hpack_parser_string *str) { - if (!p->huff && binary == NOT_BINARY && (end - cur) >= p->strlen && + if (!p->huff && binary == NOT_BINARY && (end - cur) >= (intptr_t)p->strlen && p->current_slice_refcount != NULL) { str->copied = false; str->data.referenced.refcount = p->current_slice_refcount; diff --git a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs index 26449ee5393..0e4a77be81a 100644 --- a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs @@ -71,7 +71,9 @@ namespace Grpc.Core.Internal // Gets data of recv_status_on_client completion. public ClientSideStatus GetReceivedStatusOnClient() { - string details = Marshal.PtrToStringAnsi(Native.grpcsharp_batch_context_recv_status_on_client_details(this)); + UIntPtr detailsLength; + IntPtr detailsPtr = Native.grpcsharp_batch_context_recv_status_on_client_details(this, out detailsLength); + string details = Marshal.PtrToStringAnsi(detailsPtr, (int) detailsLength.ToUInt32()); var status = new Status(Native.grpcsharp_batch_context_recv_status_on_client_status(this), details); IntPtr metadataArrayPtr = Native.grpcsharp_batch_context_recv_status_on_client_trailing_metadata(this); diff --git a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs index 05dda5b1486..d5b87a6c945 100644 --- a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs @@ -79,9 +79,13 @@ namespace Grpc.Core.Internal for (ulong i = 0; i < count; i++) { var index = new UIntPtr(i); - string key = Marshal.PtrToStringAnsi(Native.grpcsharp_metadata_array_get_key(metadataArray, index)); - var bytes = new byte[Native.grpcsharp_metadata_array_get_value_length(metadataArray, index).ToUInt64()]; - Marshal.Copy(Native.grpcsharp_metadata_array_get_value(metadataArray, index), bytes, 0, bytes.Length); + UIntPtr keyLen; + IntPtr keyPtr = Native.grpcsharp_metadata_array_get_key(metadataArray, index, out keyLen); + string key = Marshal.PtrToStringAnsi(keyPtr, (int)keyLen.ToUInt32()); + UIntPtr valueLen; + IntPtr valuePtr = Native.grpcsharp_metadata_array_get_value(metadataArray, index, out valueLen); + var bytes = new byte[valueLen.ToUInt64()]; + Marshal.Copy(valuePtr, bytes, 0, bytes.Length); metadata.Add(Metadata.Entry.CreateUnsafe(key, bytes)); } return metadata; diff --git a/src/csharp/Grpc.Core/Internal/NativeMethods.cs b/src/csharp/Grpc.Core/Internal/NativeMethods.cs index ce38e370936..2f377071f79 100644 --- a/src/csharp/Grpc.Core/Internal/NativeMethods.cs +++ b/src/csharp/Grpc.Core/Internal/NativeMethods.cs @@ -128,7 +128,6 @@ namespace Grpc.Core.Internal public readonly Delegates.grpcsharp_metadata_array_count_delegate grpcsharp_metadata_array_count; public readonly Delegates.grpcsharp_metadata_array_get_key_delegate grpcsharp_metadata_array_get_key; public readonly Delegates.grpcsharp_metadata_array_get_value_delegate grpcsharp_metadata_array_get_value; - public readonly Delegates.grpcsharp_metadata_array_get_value_length_delegate grpcsharp_metadata_array_get_value_length; public readonly Delegates.grpcsharp_metadata_array_destroy_full_delegate grpcsharp_metadata_array_destroy_full; public readonly Delegates.grpcsharp_redirect_log_delegate grpcsharp_redirect_log; @@ -237,7 +236,6 @@ namespace Grpc.Core.Internal this.grpcsharp_metadata_array_count = GetMethodDelegate(library); this.grpcsharp_metadata_array_get_key = GetMethodDelegate(library); this.grpcsharp_metadata_array_get_value = GetMethodDelegate(library); - this.grpcsharp_metadata_array_get_value_length = GetMethodDelegate(library); this.grpcsharp_metadata_array_destroy_full = GetMethodDelegate(library); this.grpcsharp_redirect_log = GetMethodDelegate(library); @@ -306,15 +304,15 @@ namespace Grpc.Core.Internal public delegate IntPtr grpcsharp_batch_context_recv_message_length_delegate(BatchContextSafeHandle ctx); public delegate void grpcsharp_batch_context_recv_message_to_buffer_delegate(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen); public delegate StatusCode grpcsharp_batch_context_recv_status_on_client_status_delegate(BatchContextSafeHandle ctx); - public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_details_delegate(BatchContextSafeHandle ctx); // returns const char* + public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_details_delegate(BatchContextSafeHandle ctx, out UIntPtr detailsLength); public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata_delegate(BatchContextSafeHandle ctx); public delegate int grpcsharp_batch_context_recv_close_on_server_cancelled_delegate(BatchContextSafeHandle ctx); public delegate void grpcsharp_batch_context_destroy_delegate(IntPtr ctx); public delegate RequestCallContextSafeHandle grpcsharp_request_call_context_create_delegate(); public delegate CallSafeHandle grpcsharp_request_call_context_call_delegate(RequestCallContextSafeHandle ctx); - public delegate IntPtr grpcsharp_request_call_context_method_delegate(RequestCallContextSafeHandle ctx); // returns const char* - public delegate IntPtr grpcsharp_request_call_context_host_delegate(RequestCallContextSafeHandle ctx); // returns const char* + public delegate IntPtr grpcsharp_request_call_context_method_delegate(RequestCallContextSafeHandle ctx, out UIntPtr methodLength); + public delegate IntPtr grpcsharp_request_call_context_host_delegate(RequestCallContextSafeHandle ctx, out UIntPtr hostLength); public delegate Timespec grpcsharp_request_call_context_deadline_delegate(RequestCallContextSafeHandle ctx); public delegate IntPtr grpcsharp_request_call_context_request_metadata_delegate(RequestCallContextSafeHandle ctx); public delegate void grpcsharp_request_call_context_destroy_delegate(IntPtr ctx); @@ -384,9 +382,8 @@ namespace Grpc.Core.Internal public delegate MetadataArraySafeHandle grpcsharp_metadata_array_create_delegate(UIntPtr capacity); public delegate void grpcsharp_metadata_array_add_delegate(MetadataArraySafeHandle array, string key, byte[] value, UIntPtr valueLength); public delegate UIntPtr grpcsharp_metadata_array_count_delegate(IntPtr metadataArray); - public delegate IntPtr grpcsharp_metadata_array_get_key_delegate(IntPtr metadataArray, UIntPtr index); - public delegate IntPtr grpcsharp_metadata_array_get_value_delegate(IntPtr metadataArray, UIntPtr index); - public delegate UIntPtr grpcsharp_metadata_array_get_value_length_delegate(IntPtr metadataArray, UIntPtr index); + public delegate IntPtr grpcsharp_metadata_array_get_key_delegate(IntPtr metadataArray, UIntPtr index, out UIntPtr keyLength); + public delegate IntPtr grpcsharp_metadata_array_get_value_delegate(IntPtr metadataArray, UIntPtr index, out UIntPtr valueLength); public delegate void grpcsharp_metadata_array_destroy_full_delegate(IntPtr array); public delegate void grpcsharp_redirect_log_delegate(GprLogDelegate callback); diff --git a/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs b/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs index ea7819d7b1f..c1560bc8bf4 100644 --- a/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs @@ -66,8 +66,14 @@ namespace Grpc.Core.Internal { var call = Native.grpcsharp_request_call_context_call(this); - var method = Marshal.PtrToStringAnsi(Native.grpcsharp_request_call_context_method(this)); - var host = Marshal.PtrToStringAnsi(Native.grpcsharp_request_call_context_host(this)); + UIntPtr methodLen; + IntPtr methodPtr = Native.grpcsharp_request_call_context_method(this, out methodLen); + var method = Marshal.PtrToStringAnsi(methodPtr, (int) methodLen.ToUInt32()); + + UIntPtr hostLen; + IntPtr hostPtr = Native.grpcsharp_request_call_context_host(this, out hostLen); + var host = Marshal.PtrToStringAnsi(hostPtr, (int) hostLen.ToUInt32()); + var deadline = Native.grpcsharp_request_call_context_deadline(this); IntPtr metadataArrayPtr = Native.grpcsharp_request_call_context_request_metadata(this); diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index 618522a3b50..12c747b8008 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -73,15 +73,13 @@ typedef struct grpcsharp_batch_context { grpc_byte_buffer *send_message; struct { grpc_metadata_array trailing_metadata; - char *status_details; } send_status_from_server; grpc_metadata_array recv_initial_metadata; grpc_byte_buffer *recv_message; struct { grpc_metadata_array trailing_metadata; grpc_status_code status; - char *status_details; - size_t status_details_capacity; + grpc_slice status_details; } recv_status_on_client; int recv_close_on_server_cancelled; } grpcsharp_batch_context; @@ -178,21 +176,17 @@ grpcsharp_metadata_array_count(grpc_metadata_array *array) { } GPR_EXPORT const char *GPR_CALLTYPE -grpcsharp_metadata_array_get_key(grpc_metadata_array *array, size_t index) { +grpcsharp_metadata_array_get_key(grpc_metadata_array *array, size_t index, size_t *key_length) { GPR_ASSERT(index < array->count); - return array->metadata[index].key; + *key_length = GRPC_SLICE_LENGTH(array->metadata[index].key); + return (char *)GRPC_SLICE_START_PTR(array->metadata[index].key); } GPR_EXPORT const char *GPR_CALLTYPE -grpcsharp_metadata_array_get_value(grpc_metadata_array *array, size_t index) { +grpcsharp_metadata_array_get_value(grpc_metadata_array *array, size_t index, size_t *value_length) { GPR_ASSERT(index < array->count); - return array->metadata[index].value; -} - -GPR_EXPORT intptr_t GPR_CALLTYPE grpcsharp_metadata_array_get_value_length( - grpc_metadata_array *array, size_t index) { - GPR_ASSERT(index < array->count); - return (intptr_t)array->metadata[index].value_length; + *value_length = GRPC_SLICE_LENGTH(array->metadata[index].value); + return (char *)GRPC_SLICE_START_PTR(array->metadata[index].value); } /* Move contents of metadata array */ @@ -225,7 +219,6 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_con grpcsharp_metadata_array_destroy_metadata_including_entries( &(ctx->send_status_from_server.trailing_metadata)); - gpr_free(ctx->send_status_from_server.status_details); grpcsharp_metadata_array_destroy_metadata_only(&(ctx->recv_initial_metadata)); @@ -233,7 +226,7 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_con grpcsharp_metadata_array_destroy_metadata_only( &(ctx->recv_status_on_client.trailing_metadata)); - gpr_free((void *)ctx->recv_status_on_client.status_details); + grpc_slice_unref(ctx->recv_status_on_client.status_details); gpr_free(ctx); } @@ -305,8 +298,9 @@ grpcsharp_batch_context_recv_status_on_client_status( GPR_EXPORT const char *GPR_CALLTYPE grpcsharp_batch_context_recv_status_on_client_details( - const grpcsharp_batch_context *ctx) { - return ctx->recv_status_on_client.status_details; + const grpcsharp_batch_context *ctx, size_t *details_length) { + *details_length = GRPC_SLICE_LENGTH(ctx->recv_status_on_client.status_details); + return (char *)GRPC_SLICE_START_PTR(ctx->recv_status_on_client.status_details); } GPR_EXPORT const grpc_metadata_array *GPR_CALLTYPE @@ -322,13 +316,15 @@ GPR_EXPORT grpc_call *GPR_CALLTYPE grpcsharp_request_call_context_call( GPR_EXPORT const char *GPR_CALLTYPE grpcsharp_request_call_context_method( - const grpcsharp_request_call_context *ctx) { - return ctx->call_details.method; + const grpcsharp_request_call_context *ctx, size_t *method_length) { + *method_length = GRPC_SLICE_LENGTH(ctx->call_details.method); + return (char *)GRPC_SLICE_START_PTR(ctx->call_details.method); } GPR_EXPORT const char *GPR_CALLTYPE grpcsharp_request_call_context_host( - const grpcsharp_request_call_context *ctx) { - return ctx->call_details.host; + const grpcsharp_request_call_context *ctx, size_t *host_length) { + *host_length = GRPC_SLICE_LENGTH(ctx->call_details.host); + return (char *)GRPC_SLICE_START_PTR(ctx->call_details.host); } GPR_EXPORT gpr_timespec GPR_CALLTYPE @@ -403,9 +399,14 @@ grpcsharp_channel_create_call(grpc_channel *channel, grpc_call *parent_call, const char *method, const char *host, gpr_timespec deadline) { grpc_slice method_slice = grpc_slice_from_copied_string(method); - grpc_slice host_slice = host == NULL ? grpc_empty_string() : grpc_slice_from_copied_string(host); - grpc_call *grpc_channel_create_call(channel, parent_call, propagation_mask, cq, - method_slice, host_slice, deadline, NULL); + grpc_slice *host_slice_ptr = NULL; + grpc_slice host_slice; + if (host != NULL) { + host_slice = grpc_slice_from_copied_string(host); + host_slice_ptr = &host_slice; + } + return grpc_channel_create_call(channel, parent_call, propagation_mask, cq, + method_slice, host_slice_ptr, deadline, NULL); } GPR_EXPORT grpc_connectivity_state GPR_CALLTYPE @@ -560,11 +561,8 @@ grpcsharp_call_start_unary(grpc_call *call, grpcsharp_batch_context *ctx, &(ctx->recv_status_on_client.trailing_metadata); ops[5].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); - /* not using preallocation for status_details */ ops[5].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); - ops[5].data.recv_status_on_client.status_details_capacity = - &(ctx->recv_status_on_client.status_details_capacity); ops[5].flags = 0; ops[5].reserved = NULL; @@ -604,11 +602,8 @@ grpcsharp_call_start_client_streaming(grpc_call *call, &(ctx->recv_status_on_client.trailing_metadata); ops[3].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); - /* not using preallocation for status_details */ ops[3].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); - ops[3].data.recv_status_on_client.status_details_capacity = - &(ctx->recv_status_on_client.status_details_capacity); ops[3].flags = 0; ops[3].reserved = NULL; @@ -647,11 +642,8 @@ GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_start_server_streaming( &(ctx->recv_status_on_client.trailing_metadata); ops[3].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); - /* not using preallocation for status_details */ ops[3].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); - ops[3].data.recv_status_on_client.status_details_capacity = - &(ctx->recv_status_on_client.status_details_capacity); ops[3].flags = 0; ops[3].reserved = NULL; @@ -681,11 +673,8 @@ grpcsharp_call_start_duplex_streaming(grpc_call *call, &(ctx->recv_status_on_client.trailing_metadata); ops[1].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); - /* not using preallocation for status_details */ ops[1].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); - ops[1].data.recv_status_on_client.status_details_capacity = - &(ctx->recv_status_on_client.status_details_capacity); ops[1].flags = 0; ops[1].reserved = NULL; @@ -749,10 +738,10 @@ GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_send_status_from_server( grpc_op ops[3]; memset(ops, 0, sizeof(ops)); size_t nops = 1; + grpc_slice status_details_slice = grpc_slice_from_copied_string(status_details); ops[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER; ops[0].data.send_status_from_server.status = status_code; - ops[0].data.send_status_from_server.status_details = - gpr_strdup(status_details); + ops[0].data.send_status_from_server.status_details = &status_details_slice; grpcsharp_metadata_array_move( &(ctx->send_status_from_server.trailing_metadata), trailing_metadata); ops[0].data.send_status_from_server.trailing_metadata_count =