Skip to content

Commit

Permalink
Don't hold a reference to shared memory across locks in resolve.c
Browse files Browse the repository at this point in the history
Fixes pi-hole#567

Signed-off-by: Mcat12 <[email protected]>
  • Loading branch information
AzureMarker committed May 23, 2019
1 parent 9e8273b commit b0d06ee
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions resolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ static char *resolveHostname(const char *addr)
// Resolve upstream destination host names
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();
const char* ipaddr = getstr(ippos);
const char* oldname = getstr(oldnamepos);
char* ipaddr = strdup(getstr(ippos));
char* oldname = strdup(getstr(oldnamepos));
unlock_shm();

// Important: Don't hold a lock while resolving as the main thread
Expand All @@ -86,6 +87,8 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos)
// newname has already been checked against NULL
// so we can safely free it
free(newname);
free(ipaddr);
free(oldname);
unlock_shm();
return newnamepos;
}
Expand All @@ -95,6 +98,9 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos)
logg("Not adding \"%s\" to buffer (unchanged)", oldname);
}

free(ipaddr);
free(oldname);

// Not changed, return old namepos
return oldnamepos;
}
Expand Down

0 comments on commit b0d06ee

Please sign in to comment.