From e20904d7cff727f14559acd39f5279fed816553d Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 2 Dec 2024 19:41:12 -0800 Subject: [PATCH] Fix use of getaddrinfo_shared->lock In some locations we were using shared->lock and in others &shared->lock, and we were leaking the allocated memory. --- ext/socket/ipsocket.c | 12 +++++------- ext/socket/raddrinfo.c | 6 +++--- ext/socket/rubysocket.h | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c index 013294480f25a1..5e5b4472669588 100644 --- a/ext/socket/ipsocket.c +++ b/ext/socket/ipsocket.c @@ -309,7 +309,7 @@ cancel_fast_fallback(void *ptr) struct fast_fallback_getaddrinfo_shared *arg = (struct fast_fallback_getaddrinfo_shared *)ptr; - rb_nativethread_lock_lock(arg->lock); + rb_nativethread_lock_lock(&arg->lock); { arg->cancelled = true; char notification = SELECT_CANCELLED; @@ -317,7 +317,7 @@ cancel_fast_fallback(void *ptr) rb_syserr_fail(errno, "write(2)"); } } - rb_nativethread_lock_unlock(arg->lock); + rb_nativethread_lock_unlock(&arg->lock); } struct hostname_resolution_result @@ -595,9 +595,7 @@ init_fast_fallback_inetsock_internal(VALUE v) arg->getaddrinfo_shared = allocate_fast_fallback_getaddrinfo_shared(arg->family_size); if (!arg->getaddrinfo_shared) rb_syserr_fail(errno, "calloc(3)"); - arg->getaddrinfo_shared->lock = calloc(1, sizeof(rb_nativethread_lock_t)); - if (!arg->getaddrinfo_shared->lock) rb_syserr_fail(errno, "calloc(3)"); - rb_nativethread_lock_initialize(arg->getaddrinfo_shared->lock); + rb_nativethread_lock_initialize(&arg->getaddrinfo_shared->lock); arg->getaddrinfo_shared->notify = hostname_resolution_notifier; arg->getaddrinfo_shared->cancelled = false; @@ -1198,7 +1196,7 @@ fast_fallback_inetsock_cleanup(VALUE v) int shared_need_free = 0; int need_free[2] = { 0, 0 }; - rb_nativethread_lock_lock(getaddrinfo_shared->lock); + rb_nativethread_lock_lock(&getaddrinfo_shared->lock); { for (int i = 0; i < arg->family_size; i++) { if (arg->getaddrinfo_entries[i] && --(arg->getaddrinfo_entries[i]->refcount) == 0) { @@ -1209,7 +1207,7 @@ fast_fallback_inetsock_cleanup(VALUE v) shared_need_free = 1; } } - rb_nativethread_lock_unlock(getaddrinfo_shared->lock); + rb_nativethread_lock_unlock(&getaddrinfo_shared->lock); for (int i = 0; i < arg->family_size; i++) { if (need_free[i]) free_fast_fallback_getaddrinfo_entry(&arg->getaddrinfo_entries[i]); diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index 5759960e5a1a09..ea24884680bc9a 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -3033,7 +3033,7 @@ free_fast_fallback_getaddrinfo_shared(struct fast_fallback_getaddrinfo_shared ** (*shared)->node = NULL; free((*shared)->service); (*shared)->service = NULL; - rb_nativethread_lock_destroy((*shared)->lock); + rb_nativethread_lock_destroy(&(*shared)->lock); free(*shared); *shared = NULL; } @@ -3092,7 +3092,7 @@ do_fast_fallback_getaddrinfo(void *ptr) } } - rb_nativethread_lock_lock(shared->lock); + rb_nativethread_lock_lock(&shared->lock); { entry->err = err; if (shared->cancelled) { @@ -3112,7 +3112,7 @@ do_fast_fallback_getaddrinfo(void *ptr) if (--(entry->refcount) == 0) need_free = 1; if (--(shared->refcount) == 0) shared_need_free = 1; } - rb_nativethread_lock_unlock(shared->lock); + rb_nativethread_lock_unlock(&shared->lock); if (need_free && entry) { free_fast_fallback_getaddrinfo_entry(&entry); diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h index 16cc75aa7b8abe..a7a4bae95bcd6e 100644 --- a/ext/socket/rubysocket.h +++ b/ext/socket/rubysocket.h @@ -443,7 +443,7 @@ struct fast_fallback_getaddrinfo_shared int notify, refcount; int cancelled; char *node, *service; - rb_nativethread_lock_t *lock; + rb_nativethread_lock_t lock; struct fast_fallback_getaddrinfo_entry getaddrinfo_entries[FLEX_ARY_LEN]; };