Don't hold a reference to shared memory across locks in resolve.c

Fixes #567

Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
This commit is contained in:
Mcat12
2019-05-21 17:29:16 -07:00
parent 9e8273b748
commit b0d06eed59

View File

@@ -66,10 +66,11 @@ static char *resolveHostname(const char *addr)
// Resolve upstream destination host names // Resolve upstream destination host names
static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos) static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos)
{ {
// Get IP and host name strings // Get IP and host name strings. They are cloned in case shared memory is
// resized before the next lock
lock_shm(); lock_shm();
const char* ipaddr = getstr(ippos); char* ipaddr = strdup(getstr(ippos));
const char* oldname = getstr(oldnamepos); char* oldname = strdup(getstr(oldnamepos));
unlock_shm(); unlock_shm();
// Important: Don't hold a lock while resolving as the main thread // Important: Don't hold a lock while resolving as the main thread
@@ -86,6 +87,8 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos)
// newname has already been checked against NULL // newname has already been checked against NULL
// so we can safely free it // so we can safely free it
free(newname); free(newname);
free(ipaddr);
free(oldname);
unlock_shm(); unlock_shm();
return newnamepos; return newnamepos;
} }
@@ -95,6 +98,9 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos)
logg("Not adding \"%s\" to buffer (unchanged)", oldname); logg("Not adding \"%s\" to buffer (unchanged)", oldname);
} }
free(ipaddr);
free(oldname);
// Not changed, return old namepos // Not changed, return old namepos
return oldnamepos; return oldnamepos;
} }