Skip to content

Commit

Permalink
Fix use of getaddrinfo_shared->lock
Browse files Browse the repository at this point in the history
In some locations we were using shared->lock and in others
&shared->lock, and we were leaking the allocated memory.
  • Loading branch information
jhawthorn committed Dec 3, 2024
1 parent 757303f commit e20904d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions ext/socket/ipsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,15 @@ 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;
if (arg->notify != -1 && (write(arg->notify, &notification, 1)) < 0) {
rb_syserr_fail(errno, "write(2)");
}
}
rb_nativethread_lock_unlock(arg->lock);
rb_nativethread_lock_unlock(&arg->lock);
}

struct hostname_resolution_result
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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]);
Expand Down
6 changes: 3 additions & 3 deletions ext/socket/raddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ext/socket/rubysocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

Expand Down

0 comments on commit e20904d

Please sign in to comment.