Skip to content

Commit

Permalink
Merge pull request #797 from pi-hole/fix/reacquire_client_pointer_aft…
Browse files Browse the repository at this point in the history
…er_resolve

Reacquire client and upstream pointers after a name resolution
  • Loading branch information
PromoFaux authored Jun 3, 2020
2 parents 0671eb5 + ee0790a commit 7382d7c
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/resolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,17 @@ void resolveClients(const bool onlynew)
int skipped = 0;
for(int clientID = 0; clientID < clientscount; clientID++)
{
// Get client pointer
// Memory access needs to get locked
lock_shm();
// Get client pointer for the first time (reading data)
clientsData* client = getClient(clientID, true);
if(client == NULL)
{
logg("ERROR: Unable to get client pointer with ID %i, skipping...", clientID);
logg("ERROR: Unable to get client pointer (1) with ID %i, skipping...", clientID);
skipped++;
continue;
}

// Memory access needs to get locked
lock_shm();
bool newflag = client->new;
size_t ippos = client->ippos;
size_t oldnamepos = client->namepos;
Expand All @@ -331,6 +331,18 @@ void resolveClients(const bool onlynew)
size_t newnamepos = resolveAndAddHostname(ippos, oldnamepos);

lock_shm();
// Get client pointer for the second time (writing data)
// We cannot use the same pointer again as we released
// the lock in between so we cannot know if something
// happened to the shared memory object (resize event)
client = getClient(clientID, true);
if(client == NULL)
{
logg("ERROR: Unable to get client pointer (2) with ID %i, skipping...", clientID);
skipped++;
continue;
}

// Store obtained host name (may be unchanged)
client->namepos = newnamepos;
// Mark entry as not new
Expand All @@ -356,7 +368,9 @@ void resolveForwardDestinations(const bool onlynew)
int skipped = 0;
for(int upstreamID = 0; upstreamID < upstreams; upstreamID++)
{
// Get upstream pointer
// Memory access needs to get locked
lock_shm();
// Get upstream pointer for the first time (reading data)
upstreamsData* upstream = getUpstream(upstreamID, true);
if(upstream == NULL)
{
Expand All @@ -365,8 +379,6 @@ void resolveForwardDestinations(const bool onlynew)
continue;
}

// Memory access needs to get locked
lock_shm();
bool newflag = upstream->new;
size_t ippos = upstream->ippos;
size_t oldnamepos = upstream->namepos;
Expand All @@ -384,6 +396,18 @@ void resolveForwardDestinations(const bool onlynew)
size_t newnamepos = resolveAndAddHostname(ippos, oldnamepos);

lock_shm();
// Get upstream pointer for the second time (writing data)
// We cannot use the same pointer again as we released
// the lock in between so we cannot know if something
// happened to the shared memory object (resize event)
upstream = getUpstream(upstreamID, true);
if(upstream == NULL)
{
logg("ERROR: Unable to get upstream pointer with ID %i, skipping...", upstreamID);
skipped++;
continue;
}

// Store obtained host name (may be unchanged)
upstream->namepos = newnamepos;
// Mark entry as not new
Expand Down

0 comments on commit 7382d7c

Please sign in to comment.