Skip to content

Commit

Permalink
Merge pull request #223 from pi-hole/fix/memory_checks
Browse files Browse the repository at this point in the history
Improve memory checks
  • Loading branch information
DL6ER authored Feb 7, 2018
2 parents 846bead + e75bd26 commit 454d968
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 238 deletions.
11 changes: 11 additions & 0 deletions FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,14 @@ bool rereadgravity;
long int lastDBimportedtimestamp;
bool ipv4telnet, ipv6telnet;
bool istelnet[MAXCONNS];

// Use out own memory handling functions that will detect possible errors
// and report accordingly in the log. This will make debugging FTL crashs
// caused by insufficient memory or by code bugs (not properly dealing
// with NULL pointers) much easier.
#define free(param) FTLfree(param, __FILE__, __FUNCTION__, __LINE__)
#define lib_strdup() strdup()
#undef strdup
#define strdup(param) FTLstrdup(param, __FILE__, __FUNCTION__, __LINE__)
#define calloc(p1,p2) FTLcalloc(p1,p2, __FILE__, __FUNCTION__, __LINE__)
#define realloc(p1,p2) FTLrealloc(p1,p2, __FILE__, __FUNCTION__, __LINE__)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Please see LICENSE file for your rights under this license.

DEPS = FTL.h routines.h api.h version.h
OBJ = main.o structs.o log.o daemon.o parser.o signals.o socket.o request.o grep.o setupVars.o args.o flush.o threads.o gc.o config.o database.o api.o msgpack.o
OBJ = main.o memory.o log.o daemon.o parser.o signals.o socket.o request.o grep.o setupVars.o args.o flush.o threads.o gc.o config.o database.o api.o msgpack.o

# Get git commit version and date
GIT_BRANCH := $(shell git branch | sed -n 's/^\* //p')
Expand Down
16 changes: 11 additions & 5 deletions api.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ void getTopDomains(char *client_message, int *sock)
ssend(*sock, "%i %i %s wildcard\n", n, domains[j].blockedcount, domains[j].domain);
else {
char *fancyWildcard = calloc(3 + strlen(domains[j].domain), sizeof(char));
if(fancyWildcard == NULL) return;
sprintf(fancyWildcard, "*.%s", domains[j].domain);

if(!pack_str32(*sock, fancyWildcard))
Expand Down Expand Up @@ -466,10 +467,8 @@ void getForwardDestinations(char *client_message, int *sock)
// Is this the "local" forward destination?
if(j == counters.forwarded)
{
ip = calloc(4,1);
strcpy(ip, "::1");
name = calloc(6,1);
strcpy(name, "local");
ip = strdup("::1");
name = strdup("local");

if(totalqueries > 0)
// Whats the percentage of (cached + blocked) queries on the total amount of queries?
Expand Down Expand Up @@ -582,15 +581,17 @@ void getAllQueries(char *client_message, int *sock)
if(command(client_message, ">getallqueries-domain")) {
// Get domain name we want to see only (limit length to 255 chars)
domainname = calloc(256, sizeof(char));
if(domainname == NULL) return;
sscanf(client_message, ">getallqueries-domain %255s", domainname);
if(debugclients)
logg("Showing only queries with domain %s", domainname);
filterdomainname = true;
}
// Client filtering?
if(command(client_message, ">getallqueries-client")) {
clientname = calloc(256, sizeof(char));
// Get client name we want to see only (limit length to 255 chars)
clientname = calloc(256, sizeof(char));
if(clientname == NULL) return;
sscanf(client_message, ">getallqueries-client %255s", clientname);
if(debugclients)
logg("Showing only queries with client %s", clientname);
Expand Down Expand Up @@ -785,6 +786,7 @@ void getMemoryUsage(int *sock)
{
unsigned long int structbytes = sizeof(countersStruct) + sizeof(ConfigStruct) + counters.queries_MAX*sizeof(queriesDataStruct) + counters.forwarded_MAX*sizeof(forwardedDataStruct) + counters.clients_MAX*sizeof(clientsDataStruct) + counters.domains_MAX*sizeof(domainsDataStruct) + counters.overTime_MAX*sizeof(overTimeDataStruct) + (counters.wildcarddomains)*sizeof(*wildcarddomains);
char *structprefix = calloc(2, sizeof(char));
if(structprefix == NULL) return;
double formated = 0.0;
format_memory_size(structprefix, structbytes, &formated);

Expand All @@ -796,6 +798,7 @@ void getMemoryUsage(int *sock)

unsigned long int dynamicbytes = memory.wildcarddomains + memory.domainnames + memory.clientips + memory.clientnames + memory.forwardedips + memory.forwardednames + memory.forwarddata;
char *dynamicprefix = calloc(2, sizeof(char));
if(dynamicprefix == NULL) return;
format_memory_size(dynamicprefix, dynamicbytes, &formated);

if(istelnet[*sock])
Expand All @@ -806,6 +809,7 @@ void getMemoryUsage(int *sock)

unsigned long int totalbytes = structbytes + dynamicbytes;
char *totalprefix = calloc(2, sizeof(char));
if(totalprefix == NULL) return;
format_memory_size(totalprefix, totalbytes, &formated);

if(istelnet[*sock])
Expand Down Expand Up @@ -1020,6 +1024,7 @@ void getVersion(int *sock)
ssend(*sock, "version vDev-%s\ntag %s\nbranch %s\ndate %s\n", hash, tag, GIT_BRANCH, GIT_DATE);
else {
char *hashVersion = calloc(6 + strlen(hash), sizeof(char));
if(hashVersion == NULL) return;
sprintf(hashVersion, "vDev-%s", hash);

if(!pack_str32(*sock, hashVersion) ||
Expand Down Expand Up @@ -1048,6 +1053,7 @@ void getDBstats(int *sock)
filesize = st.st_size;

char *prefix = calloc(2, sizeof(char));
if(prefix == NULL) return;
double formated = 0.0;
format_memory_size(prefix, filesize, &formated);

Expand Down
7 changes: 3 additions & 4 deletions daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,12 @@ char *getUserName(void)
struct passwd *pw = getpwuid(euid);
if(pw)
{
username = calloc(strlen(pw->pw_name)+1, sizeof(char));
strcpy(username, pw->pw_name);
username = strdup(pw->pw_name);
}
else
{
username = calloc(12, sizeof(char));
sprintf(username, "%i", euid);
if(asprintf(&username, "%i", euid) < 0)
return NULL;
}

return username;
Expand Down
1 change: 1 addition & 0 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ void readWildcardsList()
memory_check(WILDCARD);
// Allocate space for new domain entry and save domain
wildcarddomains[counters.wildcarddomains] = calloc(strlen(domain)+1,sizeof(char));
if(wildcarddomains[counters.wildcarddomains] == NULL) return;
memory.wildcarddomains += (strlen(domain) + 1) * sizeof(char);
strcpy(wildcarddomains[counters.wildcarddomains], domain);

Expand Down
1 change: 1 addition & 0 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void logg_struct_resize(const char* str, int to, int step)

unsigned long int bytes = structbytes + dynamicbytes;
char *prefix = calloc(2, sizeof(char));
if(prefix == NULL) return;
double formated = 0.0;
format_memory_size(prefix, bytes, &formated);

Expand Down
Loading

0 comments on commit 454d968

Please sign in to comment.