Skip to content

Commit

Permalink
Make sure we release allocated memory once we don't need it any longer
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Apr 5, 2019
1 parent 4184141 commit 540dba6
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions resolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// once every hour
#define RERESOLVE_INTERVAL 3600

static const char *resolveHostname(const char *addr)
static char *resolveHostname(const char *addr)
{
// Get host name
struct hostent *he = NULL;
Expand Down Expand Up @@ -93,10 +93,11 @@ void resolveClients(bool onlynew)

// Important: Don't hold a lock while resolving as the main thread
// (dnsmasq) needs to be operable during the call to resolveHostname()
const char* hostname = resolveHostname(ipaddr);
char* hostname = resolveHostname(ipaddr);

// Finally, lock data when storing obtained hostname
lock_shm();

// Only store new hostname if it changed
if(hostname != NULL && strcmp(name, hostname) != 0)
{
Expand All @@ -106,11 +107,17 @@ void resolveClients(bool onlynew)
}
else if(config.debug & DEBUG_SHMEM)
{
// Debug output
// Debugging output
logg("Not adding \"%s\" (unchanged)", name);
}

// Release allocated memory
free(hostname);

// Mark entry as not new
clients[clientID].new = false;

// Unlock shared memory
unlock_shm();
}
}
Expand Down Expand Up @@ -138,7 +145,7 @@ void resolveForwardDestinations(bool onlynew)

// Important: Don't hold a lock while resolving as the main thread
// (dnsmasq) needs to be operable during the call to resolveHostname()
const char* hostname = resolveHostname(ipaddr);
char* hostname = resolveHostname(ipaddr);

// Finally, lock data when storing obtained hostname
lock_shm();
Expand All @@ -151,11 +158,17 @@ void resolveForwardDestinations(bool onlynew)
}
else if(config.debug & DEBUG_SHMEM)
{
// Debug output
// Debugging output
logg("Not adding \"%s\" (unchanged)", name);
}

// Release allocated memory
free(hostname);

// Mark entry as not new
forwarded[forwardID].new = false;

// Unlock shared memory
unlock_shm();
}
}
Expand Down

0 comments on commit 540dba6

Please sign in to comment.