mirror of
git://git.musl-libc.org/musl
synced 2025-03-06 20:48:29 +01:00
allow freeaddrinfo of arbitrary sublists of addrinfo list
the specification for freeaddrinfo allows it to be used to free "arbitrary sublists" of the list returned by getaddrinfo. it's not clearly stated how such sublists come into existence, but the interpretation seems to be that the application can edit the ai_next pointers to cut off a portion of the list and then free it. actual freeing of individual list slots is contrary to the design of our getaddrinfo implementation, which has no failure paths after making a single allocation, so that light callers can avoid linking realloc/free. freeing individual slots is also incompatible with sharing the string for ai_canonname, which the current implementation does despite no requirement that it be present except on the first result. so, rather than actually freeing individual slots, provide a way to find the start of the allocated array, and reference-count it, freeing the memory all at once after the last slot has been freed. since the language in the spec is "arbitrary sublists", no provision for handling other constructs like multiple lists glued together, circular links, etc. is made. presumably passing such a construct to freeaddrinfo produces undefined behavior.
This commit is contained in:
parent
7bf773a8f9
commit
d1395c43c0
3 changed files with 25 additions and 8 deletions
|
@ -1,7 +1,16 @@
|
|||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <netdb.h>
|
||||
#include "lookup.h"
|
||||
#include "lock.h"
|
||||
|
||||
void freeaddrinfo(struct addrinfo *p)
|
||||
{
|
||||
free(p);
|
||||
size_t cnt;
|
||||
for (cnt=1; p->ai_next; cnt++, p=p->ai_next);
|
||||
struct aibuf *b = (void *)((char *)p - offsetof(struct aibuf, ai));
|
||||
b -= b->slot;
|
||||
LOCK(b->lock);
|
||||
if (!(b->ref -= cnt)) free(b);
|
||||
else UNLOCK(b->lock);
|
||||
}
|
||||
|
|
|
@ -16,13 +16,7 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru
|
|||
char canon[256], *outcanon;
|
||||
int nservs, naddrs, nais, canon_len, i, j, k;
|
||||
int family = AF_UNSPEC, flags = 0, proto = 0, socktype = 0;
|
||||
struct aibuf {
|
||||
struct addrinfo ai;
|
||||
union sa {
|
||||
struct sockaddr_in sin;
|
||||
struct sockaddr_in6 sin6;
|
||||
} sa;
|
||||
} *out;
|
||||
struct aibuf *out;
|
||||
|
||||
if (!host && !serv) return EAI_NONAME;
|
||||
|
||||
|
@ -110,6 +104,7 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru
|
|||
}
|
||||
|
||||
for (k=i=0; i<naddrs; i++) for (j=0; j<nservs; j++, k++) {
|
||||
out[k].slot = i;
|
||||
out[k].ai = (struct addrinfo){
|
||||
.ai_family = addrs[i].family,
|
||||
.ai_socktype = ports[j].socktype,
|
||||
|
@ -134,6 +129,7 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru
|
|||
break;
|
||||
}
|
||||
}
|
||||
out[0].ref = nais;
|
||||
out[nais-1].ai.ai_next = 0;
|
||||
*res = &out->ai;
|
||||
return 0;
|
||||
|
|
|
@ -4,6 +4,18 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <features.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
|
||||
struct aibuf {
|
||||
struct addrinfo ai;
|
||||
union sa {
|
||||
struct sockaddr_in sin;
|
||||
struct sockaddr_in6 sin6;
|
||||
} sa;
|
||||
volatile int lock[1];
|
||||
short slot, ref;
|
||||
};
|
||||
|
||||
struct address {
|
||||
int family;
|
||||
|
|
Loading…
Add table
Reference in a new issue