Skip to content

Commit 89eef69

Browse files
authored
Fix freeaddrinfo(), which did not match allocation in JS (#16085)
It is risky to allocate in JS and free in C, when given matching allocate/free pairs like getaddrinfo/freeaddrinfo, and it looks like we had things wrong here. That is, the existing musl code did not match how we use that structure. The worst part is that musl appears to assume the struct is identical/can alias some other struct, and that led to memory corruption. Not sure if we can test this reliably as the issue is corruption. Fixes #16081
1 parent 6e00311 commit 89eef69

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

system/lib/libc/musl/src/network/freeaddrinfo.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66

77
void freeaddrinfo(struct addrinfo *p)
88
{
9+
#if __EMSCRIPTEN__
10+
// Emscripten's usage of this structure is very simple: we always allocate
11+
// ai_addr, and do not use the linked list aspect at all. There is also no
12+
// aliasing with aibuf.
13+
free(p->ai_addr);
14+
free(p);
15+
#else
916
size_t cnt;
1017
for (cnt=1; p->ai_next; cnt++, p=p->ai_next);
1118
struct aibuf *b = (void *)((char *)p - offsetof(struct aibuf, ai));
1219
b -= b->slot;
1320
LOCK(b->lock);
1421
if (!(b->ref -= cnt)) free(b);
1522
else UNLOCK(b->lock);
23+
#endif
1624
}

0 commit comments

Comments
 (0)