From 3f5b48b76b7f6bb4a124f8cb987316df085600fa Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Sat, 23 Dec 2023 11:39:22 -0800 Subject: [PATCH 1/2] Use line control in merged .c file --- contrib/ruby/ext/trilogy-ruby/extconf.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/contrib/ruby/ext/trilogy-ruby/extconf.rb b/contrib/ruby/ext/trilogy-ruby/extconf.rb index 05ad2aec..f618ef21 100644 --- a/contrib/ruby/ext/trilogy-ruby/extconf.rb +++ b/contrib/ruby/ext/trilogy-ruby/extconf.rb @@ -2,8 +2,12 @@ # concatenate trilogy library sources to allow the compiler to optimise across # source files + +trilogy_src_dir = File.realpath("src", __dir__) File.binwrite("trilogy.c", - Dir["#{__dir__}/src/**/*.c"].map { |src| File.binread(src) }.join) + Dir["#{trilogy_src_dir}/**/*.c"].map { |src| + %{#line 1 "#{src}"\n} + File.binread(src) + }.join) $objs = %w[trilogy.o cast.o cext.o] $CFLAGS << " -I #{__dir__}/inc -std=gnu99 -fvisibility=hidden" From 108b9d39015ddfcfa866a09b8fe21d3b61df3868 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Sat, 23 Dec 2023 12:02:01 -0800 Subject: [PATCH 2/2] Fix memory leaks --- contrib/ruby/ext/trilogy-ruby/cext.c | 9 +++++---- src/buffer.c | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/contrib/ruby/ext/trilogy-ruby/cext.c b/contrib/ruby/ext/trilogy-ruby/cext.c index 3a8bddcd..ea350fdc 100644 --- a/contrib/ruby/ext/trilogy-ruby/cext.c +++ b/contrib/ruby/ext/trilogy-ruby/cext.c @@ -43,9 +43,7 @@ static void mark_trilogy(void *ptr) static void free_trilogy(void *ptr) { struct trilogy_ctx *ctx = ptr; - if (ctx->conn.socket != NULL) { - trilogy_free(&ctx->conn); - } + trilogy_free(&ctx->conn); xfree(ptr); } @@ -309,6 +307,7 @@ static int try_connect(struct trilogy_ctx *ctx, trilogy_handshake_t *handshake, int rc = args.rc; if (rc != TRILOGY_OK) { + trilogy_sock_close(sock); return rc; } @@ -316,8 +315,10 @@ static int try_connect(struct trilogy_ctx *ctx, trilogy_handshake_t *handshake, escape the GVL on each wait operation without going through call_without_gvl */ sock->wait_cb = _cb_ruby_wait; rc = trilogy_connect_send_socket(&ctx->conn, sock); - if (rc < 0) + if (rc < 0) { + trilogy_sock_close(sock); return rc; + } while (1) { rc = trilogy_connect_recv(&ctx->conn, handshake); diff --git a/src/buffer.c b/src/buffer.c index cdb95216..a4c78393 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -57,4 +57,9 @@ int trilogy_buffer_putc(trilogy_buffer_t *buffer, uint8_t c) return TRILOGY_OK; } -void trilogy_buffer_free(trilogy_buffer_t *buffer) { free(buffer->buff); } +void trilogy_buffer_free(trilogy_buffer_t *buffer) +{ + free(buffer->buff); + buffer->buff = NULL; + buffer->len = buffer->cap = 0; +}