Skip to content

Commit e20904d

Browse files
committed
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.
1 parent 757303f commit e20904d

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

ext/socket/ipsocket.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,15 @@ cancel_fast_fallback(void *ptr)
309309

310310
struct fast_fallback_getaddrinfo_shared *arg = (struct fast_fallback_getaddrinfo_shared *)ptr;
311311

312-
rb_nativethread_lock_lock(arg->lock);
312+
rb_nativethread_lock_lock(&arg->lock);
313313
{
314314
arg->cancelled = true;
315315
char notification = SELECT_CANCELLED;
316316
if (arg->notify != -1 && (write(arg->notify, &notification, 1)) < 0) {
317317
rb_syserr_fail(errno, "write(2)");
318318
}
319319
}
320-
rb_nativethread_lock_unlock(arg->lock);
320+
rb_nativethread_lock_unlock(&arg->lock);
321321
}
322322

323323
struct hostname_resolution_result
@@ -595,9 +595,7 @@ init_fast_fallback_inetsock_internal(VALUE v)
595595
arg->getaddrinfo_shared = allocate_fast_fallback_getaddrinfo_shared(arg->family_size);
596596
if (!arg->getaddrinfo_shared) rb_syserr_fail(errno, "calloc(3)");
597597

598-
arg->getaddrinfo_shared->lock = calloc(1, sizeof(rb_nativethread_lock_t));
599-
if (!arg->getaddrinfo_shared->lock) rb_syserr_fail(errno, "calloc(3)");
600-
rb_nativethread_lock_initialize(arg->getaddrinfo_shared->lock);
598+
rb_nativethread_lock_initialize(&arg->getaddrinfo_shared->lock);
601599

602600
arg->getaddrinfo_shared->notify = hostname_resolution_notifier;
603601
arg->getaddrinfo_shared->cancelled = false;
@@ -1198,7 +1196,7 @@ fast_fallback_inetsock_cleanup(VALUE v)
11981196
int shared_need_free = 0;
11991197
int need_free[2] = { 0, 0 };
12001198

1201-
rb_nativethread_lock_lock(getaddrinfo_shared->lock);
1199+
rb_nativethread_lock_lock(&getaddrinfo_shared->lock);
12021200
{
12031201
for (int i = 0; i < arg->family_size; i++) {
12041202
if (arg->getaddrinfo_entries[i] && --(arg->getaddrinfo_entries[i]->refcount) == 0) {
@@ -1209,7 +1207,7 @@ fast_fallback_inetsock_cleanup(VALUE v)
12091207
shared_need_free = 1;
12101208
}
12111209
}
1212-
rb_nativethread_lock_unlock(getaddrinfo_shared->lock);
1210+
rb_nativethread_lock_unlock(&getaddrinfo_shared->lock);
12131211

12141212
for (int i = 0; i < arg->family_size; i++) {
12151213
if (need_free[i]) free_fast_fallback_getaddrinfo_entry(&arg->getaddrinfo_entries[i]);

ext/socket/raddrinfo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,7 @@ free_fast_fallback_getaddrinfo_shared(struct fast_fallback_getaddrinfo_shared **
30333033
(*shared)->node = NULL;
30343034
free((*shared)->service);
30353035
(*shared)->service = NULL;
3036-
rb_nativethread_lock_destroy((*shared)->lock);
3036+
rb_nativethread_lock_destroy(&(*shared)->lock);
30373037
free(*shared);
30383038
*shared = NULL;
30393039
}
@@ -3092,7 +3092,7 @@ do_fast_fallback_getaddrinfo(void *ptr)
30923092
}
30933093
}
30943094

3095-
rb_nativethread_lock_lock(shared->lock);
3095+
rb_nativethread_lock_lock(&shared->lock);
30963096
{
30973097
entry->err = err;
30983098
if (shared->cancelled) {
@@ -3112,7 +3112,7 @@ do_fast_fallback_getaddrinfo(void *ptr)
31123112
if (--(entry->refcount) == 0) need_free = 1;
31133113
if (--(shared->refcount) == 0) shared_need_free = 1;
31143114
}
3115-
rb_nativethread_lock_unlock(shared->lock);
3115+
rb_nativethread_lock_unlock(&shared->lock);
31163116

31173117
if (need_free && entry) {
31183118
free_fast_fallback_getaddrinfo_entry(&entry);

ext/socket/rubysocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ struct fast_fallback_getaddrinfo_shared
443443
int notify, refcount;
444444
int cancelled;
445445
char *node, *service;
446-
rb_nativethread_lock_t *lock;
446+
rb_nativethread_lock_t lock;
447447
struct fast_fallback_getaddrinfo_entry getaddrinfo_entries[FLEX_ARY_LEN];
448448
};
449449

0 commit comments

Comments
 (0)