diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c index 74ee68a6f11..413f7867be2 100644 --- a/src/core/iomgr/tcp_server_posix.c +++ b/src/core/iomgr/tcp_server_posix.c @@ -451,7 +451,7 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, &sockname_len)) { port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); if (port > 0) { - allocated_addr = malloc(addr_len); + allocated_addr = gpr_malloc(addr_len); memcpy(allocated_addr, addr, addr_len); grpc_sockaddr_set_port(allocated_addr, port); addr = allocated_addr; diff --git a/src/core/iomgr/tcp_server_windows.c b/src/core/iomgr/tcp_server_windows.c index a4abc5b9743..f9def9a19bb 100644 --- a/src/core/iomgr/tcp_server_windows.c +++ b/src/core/iomgr/tcp_server_windows.c @@ -467,7 +467,7 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, (struct sockaddr *)&sockname_temp, &sockname_len)) { port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); if (port > 0) { - allocated_addr = malloc(addr_len); + allocated_addr = gpr_malloc(addr_len); memcpy(allocated_addr, addr, addr_len); grpc_sockaddr_set_port(allocated_addr, port); addr = allocated_addr; diff --git a/src/core/iomgr/udp_server.c b/src/core/iomgr/udp_server.c index 174159170f3..15f3802dddc 100644 --- a/src/core/iomgr/udp_server.c +++ b/src/core/iomgr/udp_server.c @@ -337,7 +337,7 @@ int grpc_udp_server_add_port(grpc_udp_server *s, const void *addr, &sockname_len)) { port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); if (port > 0) { - allocated_addr = malloc(addr_len); + allocated_addr = gpr_malloc(addr_len); memcpy(allocated_addr, addr, addr_len); grpc_sockaddr_set_port(allocated_addr, port); addr = allocated_addr; diff --git a/src/core/profiling/basic_timers.c b/src/core/profiling/basic_timers.c index 3067f52c219..3c9557ab895 100644 --- a/src/core/profiling/basic_timers.c +++ b/src/core/profiling/basic_timers.c @@ -173,7 +173,7 @@ static void flush_logs(gpr_timer_log_list *list) { gpr_timer_log *log; while ((log = timer_log_pop_front(list)) != NULL) { write_log(log); - free(log); + gpr_free(log); } } @@ -208,7 +208,7 @@ static void init_output() { } static void rotate_log() { - gpr_timer_log *new = malloc(sizeof(*new)); + gpr_timer_log *new = gpr_malloc(sizeof(*new)); gpr_once_init(&g_once_init, init_output); new->num_entries = 0; pthread_mutex_lock(&g_mu); diff --git a/src/core/support/thd_posix.c b/src/core/support/thd_posix.c index 4d874d36566..1c703844b4d 100644 --- a/src/core/support/thd_posix.c +++ b/src/core/support/thd_posix.c @@ -81,6 +81,7 @@ int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg, thread_started = (pthread_create(&p, &attr, &thread_body, a) == 0); GPR_ASSERT(pthread_attr_destroy(&attr) == 0); if (!thread_started) { + /* don't use gpr_free, as this was allocated using malloc (see above) */ free(a); } *t = (gpr_thd_id)p; diff --git a/src/core/surface/byte_buffer.c b/src/core/surface/byte_buffer.c index fb39c4531d9..a093a37af32 100644 --- a/src/core/surface/byte_buffer.c +++ b/src/core/surface/byte_buffer.c @@ -44,7 +44,7 @@ grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices, grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create( gpr_slice *slices, size_t nslices, grpc_compression_algorithm compression) { size_t i; - grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer)); + grpc_byte_buffer *bb = gpr_malloc(sizeof(grpc_byte_buffer)); bb->type = GRPC_BB_RAW; bb->data.raw.compression = compression; gpr_slice_buffer_init(&bb->data.raw.slice_buffer); @@ -57,7 +57,7 @@ grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create( grpc_byte_buffer *grpc_raw_byte_buffer_from_reader( grpc_byte_buffer_reader *reader) { - grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer)); + grpc_byte_buffer *bb = gpr_malloc(sizeof(grpc_byte_buffer)); gpr_slice slice; bb->type = GRPC_BB_RAW; bb->data.raw.compression = GRPC_COMPRESS_NONE; @@ -85,7 +85,7 @@ void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) { gpr_slice_buffer_destroy(&bb->data.raw.slice_buffer); break; } - free(bb); + gpr_free(bb); } size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) { diff --git a/src/core/tsi/fake_transport_security.c b/src/core/tsi/fake_transport_security.c index c0106f7a33c..86ffa6b83be 100644 --- a/src/core/tsi/fake_transport_security.c +++ b/src/core/tsi/fake_transport_security.c @@ -134,12 +134,12 @@ static void tsi_fake_frame_reset(tsi_fake_frame *frame, int needs_draining) { static int tsi_fake_frame_ensure_size(tsi_fake_frame *frame) { if (frame->data == NULL) { frame->allocated_size = frame->size; - frame->data = malloc(frame->allocated_size); + frame->data = gpr_malloc(frame->allocated_size); if (frame->data == NULL) return 0; } else if (frame->size > frame->allocated_size) { unsigned char *new_data = realloc(frame->data, frame->size); if (new_data == NULL) { - free(frame->data); + gpr_free(frame->data); frame->data = NULL; return 0; } @@ -160,7 +160,7 @@ static tsi_result fill_frame_from_bytes(const unsigned char *incoming_bytes, if (frame->needs_draining) return TSI_INTERNAL_ERROR; if (frame->data == NULL) { frame->allocated_size = TSI_FAKE_FRAME_INITIAL_ALLOCATED_SIZE; - frame->data = malloc(frame->allocated_size); + frame->data = gpr_malloc(frame->allocated_size); if (frame->data == NULL) return TSI_OUT_OF_RESOURCES; } @@ -226,7 +226,7 @@ static tsi_result bytes_to_frame(unsigned char *bytes, size_t bytes_size, } static void tsi_fake_frame_destruct(tsi_fake_frame *frame) { - if (frame->data != NULL) free(frame->data); + if (frame->data != NULL) gpr_free(frame->data); } /* --- tsi_frame_protector methods implementation. ---*/ @@ -366,7 +366,7 @@ static void fake_protector_destroy(tsi_frame_protector *self) { tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self; tsi_fake_frame_destruct(&impl->protect_frame); tsi_fake_frame_destruct(&impl->unprotect_frame); - free(self); + gpr_free(self); } static const tsi_frame_protector_vtable frame_protector_vtable = { @@ -488,7 +488,7 @@ static void fake_handshaker_destroy(tsi_handshaker *self) { tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self; tsi_fake_frame_destruct(&impl->incoming); tsi_fake_frame_destruct(&impl->outgoing); - free(self); + gpr_free(self); } static const tsi_handshaker_vtable handshaker_vtable = { diff --git a/src/core/tsi/ssl_transport_security.c b/src/core/tsi/ssl_transport_security.c index 8df582609b9..acf1b0e3c1d 100644 --- a/src/core/tsi/ssl_transport_security.c +++ b/src/core/tsi/ssl_transport_security.c @@ -148,8 +148,7 @@ static void init_openssl(void) { OpenSSL_add_all_algorithms(); num_locks = CRYPTO_num_locks(); GPR_ASSERT(num_locks > 0); - openssl_mutexes = malloc((size_t)num_locks * sizeof(gpr_mu)); - GPR_ASSERT(openssl_mutexes != NULL); + openssl_mutexes = gpr_malloc((size_t)num_locks * sizeof(gpr_mu)); for (i = 0; i < CRYPTO_num_locks(); i++) { gpr_mu_init(&openssl_mutexes[i]); } @@ -701,7 +700,7 @@ static tsi_result build_alpn_protocol_name_list( } *protocol_name_list_length += (size_t)alpn_protocols_lengths[i] + 1; } - *protocol_name_list = malloc(*protocol_name_list_length); + *protocol_name_list = gpr_malloc(*protocol_name_list_length); if (*protocol_name_list == NULL) return TSI_OUT_OF_RESOURCES; current = *protocol_name_list; for (i = 0; i < num_alpn_protocols; i++) { @@ -852,9 +851,9 @@ static tsi_result ssl_protector_unprotect( static void ssl_protector_destroy(tsi_frame_protector *self) { tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self; - if (impl->buffer != NULL) free(impl->buffer); + if (impl->buffer != NULL) gpr_free(impl->buffer); if (impl->ssl != NULL) SSL_free(impl->ssl); - free(self); + gpr_free(self); } static const tsi_frame_protector_vtable frame_protector_vtable = { @@ -966,8 +965,8 @@ static tsi_result ssl_handshaker_extract_peer(tsi_handshaker *self, if (alpn_selected != NULL) { size_t i; tsi_peer_property *new_properties = - calloc(1, sizeof(tsi_peer_property) * (peer->property_count + 1)); - if (new_properties == NULL) return TSI_OUT_OF_RESOURCES; + gpr_malloc(sizeof(*new_properties) * (peer->property_count + 1)); + memset(new_properties, 0, sizeof(*new_properties) * (peer->property_count + 1)); for (i = 0; i < peer->property_count; i++) { new_properties[i] = peer->properties[i]; } @@ -975,10 +974,10 @@ static tsi_result ssl_handshaker_extract_peer(tsi_handshaker *self, TSI_SSL_ALPN_SELECTED_PROTOCOL, (const char *)alpn_selected, alpn_selected_len, &new_properties[peer->property_count]); if (result != TSI_OK) { - free(new_properties); + gpr_free(new_properties); return result; } - if (peer->properties != NULL) free(peer->properties); + if (peer->properties != NULL) gpr_free(peer->properties); peer->property_count++; peer->properties = new_properties; } @@ -992,10 +991,8 @@ static tsi_result ssl_handshaker_create_frame_protector( TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND; tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; tsi_ssl_frame_protector *protector_impl = - calloc(1, sizeof(tsi_ssl_frame_protector)); - if (protector_impl == NULL) { - return TSI_OUT_OF_RESOURCES; - } + gpr_malloc(sizeof(*protector_impl)); + memset(protector_impl, 0, sizeof(*protector_impl)); if (max_output_protected_frame_size != NULL) { if (*max_output_protected_frame_size > @@ -1011,11 +1008,11 @@ static tsi_result ssl_handshaker_create_frame_protector( } protector_impl->buffer_size = actual_max_output_protected_frame_size - TSI_SSL_MAX_PROTECTION_OVERHEAD; - protector_impl->buffer = malloc(protector_impl->buffer_size); + protector_impl->buffer = gpr_malloc(protector_impl->buffer_size); if (protector_impl->buffer == NULL) { gpr_log(GPR_ERROR, "Could not allocated buffer for tsi_ssl_frame_protector."); - free(protector_impl); + gpr_free(protector_impl); return TSI_INTERNAL_ERROR; } @@ -1034,7 +1031,7 @@ static tsi_result ssl_handshaker_create_frame_protector( static void ssl_handshaker_destroy(tsi_handshaker *self) { tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; SSL_free(impl->ssl); /* The BIO objects are owned by ssl */ - free(impl); + gpr_free(impl); } static const tsi_handshaker_vtable handshaker_vtable = { @@ -1111,11 +1108,8 @@ static tsi_result create_tsi_ssl_handshaker(SSL_CTX *ctx, int is_client, SSL_set_accept_state(ssl); } - impl = calloc(1, sizeof(tsi_ssl_handshaker)); - if (impl == NULL) { - SSL_free(ssl); - return TSI_OUT_OF_RESOURCES; - } + impl = gpr_malloc(sizeof(*impl)); + memset(impl, 0, sizeof(*impl)); impl->ssl = ssl; impl->into_ssl = into_ssl; impl->from_ssl = from_ssl; @@ -1167,8 +1161,8 @@ static void ssl_client_handshaker_factory_destroy( tsi_ssl_client_handshaker_factory *impl = (tsi_ssl_client_handshaker_factory *)self; if (impl->ssl_context != NULL) SSL_CTX_free(impl->ssl_context); - if (impl->alpn_protocol_list != NULL) free(impl->alpn_protocol_list); - free(impl); + if (impl->alpn_protocol_list != NULL) gpr_free(impl->alpn_protocol_list); + gpr_free(impl); } static int client_handshaker_factory_npn_callback(SSL *ssl, unsigned char **out, @@ -1209,12 +1203,12 @@ static void ssl_server_handshaker_factory_destroy( tsi_peer_destruct(&impl->ssl_context_x509_subject_names[i]); } } - if (impl->ssl_contexts != NULL) free(impl->ssl_contexts); + if (impl->ssl_contexts != NULL) gpr_free(impl->ssl_contexts); if (impl->ssl_context_x509_subject_names != NULL) { - free(impl->ssl_context_x509_subject_names); + gpr_free(impl->ssl_context_x509_subject_names); } - if (impl->alpn_protocol_list != NULL) free(impl->alpn_protocol_list); - free(impl); + if (impl->alpn_protocol_list != NULL) gpr_free(impl->alpn_protocol_list); + gpr_free(impl); } static int does_entry_match_name(const char *entry, size_t entry_length, @@ -1333,11 +1327,8 @@ tsi_result tsi_create_ssl_client_handshaker_factory( return TSI_INVALID_ARGUMENT; } - impl = calloc(1, sizeof(tsi_ssl_client_handshaker_factory)); - if (impl == NULL) { - SSL_CTX_free(ssl_context); - return TSI_OUT_OF_RESOURCES; - } + impl = gpr_malloc(sizeof(*impl)); + memset(impl, 0, sizeof(*impl)); impl->ssl_context = ssl_context; do { @@ -1411,14 +1402,16 @@ tsi_result tsi_create_ssl_server_handshaker_factory( return TSI_INVALID_ARGUMENT; } - impl = calloc(1, sizeof(tsi_ssl_server_handshaker_factory)); - if (impl == NULL) return TSI_OUT_OF_RESOURCES; + impl = gpr_malloc(sizeof(*impl)); + memset(impl, 0, sizeof(*impl)); impl->base.create_handshaker = ssl_server_handshaker_factory_create_handshaker; impl->base.destroy = ssl_server_handshaker_factory_destroy; - impl->ssl_contexts = calloc(key_cert_pair_count, sizeof(SSL_CTX *)); + impl->ssl_contexts = gpr_malloc(key_cert_pair_count * sizeof(SSL_CTX *)); + memset(impl->ssl_contexts, 0, key_cert_pair_count * sizeof(SSL_CTX *)); impl->ssl_context_x509_subject_names = - calloc(key_cert_pair_count, sizeof(tsi_peer)); + gpr_malloc(key_cert_pair_count * sizeof(tsi_peer)); + memset(impl->ssl_context_x509_subject_names, 0, key_cert_pair_count * sizeof(tsi_peer)); if (impl->ssl_contexts == NULL || impl->ssl_context_x509_subject_names == NULL) { tsi_ssl_handshaker_factory_destroy(&impl->base); diff --git a/src/core/tsi/transport_security.c b/src/core/tsi/transport_security.c index db219a50a67..e17749d871a 100644 --- a/src/core/tsi/transport_security.c +++ b/src/core/tsi/transport_security.c @@ -239,16 +239,10 @@ void tsi_peer_destruct(tsi_peer *self) { tsi_result tsi_construct_allocated_string_peer_property( const char *name, size_t value_length, tsi_peer_property *property) { *property = tsi_init_peer_property(); - if (name != NULL) { - property->name = tsi_strdup(name); - if (property->name == NULL) return TSI_OUT_OF_RESOURCES; - } + if (name != NULL) property->name = gpr_strdup(name); if (value_length > 0) { - property->value.data = calloc(1, value_length); - if (property->value.data == NULL) { - tsi_peer_property_destruct(property); - return TSI_OUT_OF_RESOURCES; - } + property->value.data = gpr_malloc(value_length); + memset(value.data, 0, value_length); property->value.length = value_length; } return TSI_OK; @@ -276,8 +270,8 @@ tsi_result tsi_construct_string_peer_property(const char *name, tsi_result tsi_construct_peer(size_t property_count, tsi_peer *peer) { memset(peer, 0, sizeof(tsi_peer)); if (property_count > 0) { - peer->properties = calloc(property_count, sizeof(tsi_peer_property)); - if (peer->properties == NULL) return TSI_OUT_OF_RESOURCES; + peer->properties = gpr_malloc(property_count * sizeof(tsi_peer_property)); + memset(peer->properties, 0, property_count * sizeof(tsi_peer_property)); peer->property_count = property_count; } return TSI_OK; diff --git a/test/core/iomgr/endpoint_tests.c b/test/core/iomgr/endpoint_tests.c index f689e4ba7fb..cb23f58eca5 100644 --- a/test/core/iomgr/endpoint_tests.c +++ b/test/core/iomgr/endpoint_tests.c @@ -89,7 +89,7 @@ static void end_test(grpc_endpoint_test_config config) { config.clean_up(); } static gpr_slice *allocate_blocks(size_t num_bytes, size_t slice_size, size_t *num_blocks, uint8_t *current_data) { size_t nslices = num_bytes / slice_size + (num_bytes % slice_size ? 1 : 0); - gpr_slice *slices = malloc(sizeof(gpr_slice) * nslices); + gpr_slice *slices = gpr_malloc(sizeof(gpr_slice) * nslices); size_t num_bytes_left = num_bytes; size_t i; size_t j; @@ -164,7 +164,7 @@ static void read_and_write_test_write_handler(grpc_exec_ctx *exec_ctx, gpr_slice_buffer_addn(&state->outgoing, slices, nslices); grpc_endpoint_write(exec_ctx, state->write_ep, &state->outgoing, &state->done_write); - free(slices); + gpr_free(slices); return; } } diff --git a/test/core/iomgr/tcp_posix_test.c b/test/core/iomgr/tcp_posix_test.c index 4351642ab6e..b2cf87a0c25 100644 --- a/test/core/iomgr/tcp_posix_test.c +++ b/test/core/iomgr/tcp_posix_test.c @@ -97,7 +97,7 @@ static ssize_t fill_socket(int fd) { static size_t fill_socket_partial(int fd, size_t bytes) { ssize_t write_bytes; size_t total_bytes = 0; - unsigned char *buf = malloc(bytes); + unsigned char *buf = gpr_malloc(bytes); unsigned i; for (i = 0; i < bytes; ++i) { buf[i] = (uint8_t)(i % 256); @@ -292,7 +292,7 @@ static void write_done(grpc_exec_ctx *exec_ctx, } void drain_socket_blocking(int fd, size_t num_bytes, size_t read_size) { - unsigned char *buf = malloc(read_size); + unsigned char *buf = gpr_malloc(read_size); ssize_t bytes_read; size_t bytes_left = num_bytes; int flags; diff --git a/test/core/network_benchmarks/low_level_ping_pong.c b/test/core/network_benchmarks/low_level_ping_pong.c index 7ed3372d5d1..8179ba3b4df 100644 --- a/test/core/network_benchmarks/low_level_ping_pong.c +++ b/test/core/network_benchmarks/low_level_ping_pong.c @@ -265,19 +265,19 @@ static int epoll_setup(thread_args *args) { #endif static void server_thread(thread_args *args) { - char *buf = malloc(args->msg_size); + char *buf = gpr_malloc(args->msg_size); if (args->setup(args) < 0) { gpr_log(GPR_ERROR, "Setup failed"); } for (;;) { if (args->read_bytes(args, buf) < 0) { gpr_log(GPR_ERROR, "Server read failed"); - free(buf); + gpr_free(buf); return; } if (args->write_bytes(args, buf) < 0) { gpr_log(GPR_ERROR, "Server write failed"); - free(buf); + gpr_free(buf); return; } } @@ -304,7 +304,8 @@ static double now(void) { } static void client_thread(thread_args *args) { - char *buf = calloc(args->msg_size, sizeof(char)); + char *buf = gpr_malloc(args->msg_size * sizeof(char)); + memset(buf, 0, args->msg_size * sizeof(char)); gpr_histogram *histogram = gpr_histogram_create(0.01, 60e9); double start_time; double end_time; @@ -333,7 +334,7 @@ static void client_thread(thread_args *args) { } print_histogram(histogram); error: - free(buf); + gpr_free(buf); gpr_histogram_destroy(histogram); } @@ -596,8 +597,8 @@ static int run_all_benchmarks(size_t msg_size) { test_strategy *strategy = &test_strategies[i]; size_t j; for (j = 0; j < GPR_ARRAY_SIZE(socket_types); ++j) { - thread_args *client_args = malloc(sizeof(thread_args)); - thread_args *server_args = malloc(sizeof(thread_args)); + thread_args *client_args = gpr_malloc(sizeof(thread_args)); + thread_args *server_args = gpr_malloc(sizeof(thread_args)); char *socket_type = socket_types[j]; client_args->read_bytes = strategy->read_strategy; @@ -620,8 +621,8 @@ static int run_all_benchmarks(size_t msg_size) { } int main(int argc, char **argv) { - thread_args *client_args = malloc(sizeof(thread_args)); - thread_args *server_args = malloc(sizeof(thread_args)); + thread_args *client_args = gpr_malloc(sizeof(thread_args)); + thread_args *server_args = gpr_malloc(sizeof(thread_args)); int msg_size = -1; char *read_strategy = NULL; char *socket_type = NULL; diff --git a/test/core/support/alloc_test.c b/test/core/support/alloc_test.c index e2d0c16b41d..7eaf7a14f3e 100644 --- a/test/core/support/alloc_test.c +++ b/test/core/support/alloc_test.c @@ -46,7 +46,7 @@ static void fake_free(void *addr) { static void test_custom_allocs() { const gpr_allocation_functions default_fns = gpr_get_allocation_functions(); intptr_t addr_to_free = 0; - int *i; + char *i; gpr_allocation_functions fns = {fake_malloc, fake_realloc, fake_free}; gpr_set_allocation_functions(fns); @@ -58,7 +58,7 @@ static void test_custom_allocs() { /* Restore and check we don't get funky values and that we don't leak */ gpr_set_allocation_functions(default_fns); - GPR_ASSERT((void *)1 != (i = gpr_malloc(sizeof(*i)))); + GPR_ASSERT((void *)sizeof(*i) != (i = gpr_malloc(sizeof(*i)))); GPR_ASSERT((void *)2 != (i = gpr_realloc(i, 2))); gpr_free(i); }