From 152d119601beae4891a19202f6bdb9f0f0220dca Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Tue, 27 Aug 2024 18:45:27 +0200 Subject: [PATCH] fix: memory leaks, add free node function, update test.c --- LEAKS | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 4 ++ README | 9 ++-- streecmp.c | 27 ++++++++++-- streecmp.h | 3 ++ test.c | 19 +++++--- 6 files changed, 174 insertions(+), 14 deletions(-) create mode 100644 LEAKS diff --git a/LEAKS b/LEAKS new file mode 100644 index 0000000..b34b439 --- /dev/null +++ b/LEAKS @@ -0,0 +1,126 @@ +==6704== Memcheck, a memory error detector +==6704== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. +==6704== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info +==6704== Command: ./test +==6704== +------------------------ +[+][1/1] Success: could find 'A-IM' in tree +[+][2/2] Success: could find 'Accept' in tree +[+][3/3] Success: could find 'Accept-Charset' in tree +[+][4/4] Success: could find 'Accept-Datetime' in tree +[+][5/5] Success: could find 'Accept-Encoding' in tree +[+][6/6] Success: could find 'Accept-Language' in tree +[+][7/7] Success: could find 'Access-Control-Request-Method' in tree +[+][8/8] Success: could find 'Access-Control-Request-Headers' in tree +[+][9/9] Success: could find 'Authorization' in tree +[+][10/10] Success: could find 'Cache-Control' in tree +[+][11/11] Success: could find 'Connection' in tree +[+][12/12] Success: could find 'Content-Encoding' in tree +[+][13/13] Success: could find 'Content-Length' in tree +[+][14/14] Success: could find 'Content-MD5' in tree +[+][15/15] Success: could find 'Content-Type' in tree +[+][16/16] Success: could find 'Cookie' in tree +[+][17/17] Success: could find 'Date' in tree +[+][18/18] Success: could find 'Expect' in tree +[+][19/19] Success: could find 'Forwarded' in tree +[+][20/20] Success: could find 'From' in tree +[+][21/21] Success: could find 'Host' in tree +[+][22/22] Success: could find 'HTTP2-Settings' in tree +[+][23/23] Success: could find 'If-Match' in tree +[+][24/24] Success: could find 'If-Modified-Since' in tree +[+][25/25] Success: could find 'If-None-Match' in tree +[+][26/26] Success: could find 'If-Range' in tree +[+][27/27] Success: could find 'If-Unmodified-Since' in tree +[+][28/28] Success: could find 'Max-Forwards' in tree +[+][29/29] Success: could find 'Origin' in tree +[+][30/30] Success: could find 'Pragma' in tree +[+][31/31] Success: could find 'Prefer' in tree +[+][32/32] Success: could find 'Proxy-Authorization' in tree +[+][33/33] Success: could find 'Range' in tree +[+][34/34] Success: could find 'Referer' in tree +[+][35/35] Success: could find 'TE' in tree +[+][36/36] Success: could find 'Trailer' in tree +[+][37/37] Success: could find 'Transfer-Encoding' in tree +[+][38/38] Success: could find 'User-Agent' in tree +[+][39/39] Success: could find 'Upgrade' in tree +[+][40/40] Success: could find 'Via' in tree +[+][41/41] Success: could find 'Warning' in tree +[+][42/42] Success: could find 'Upgrade-Insecure-Requests' in tree +[+][43/43] Success: could find 'X-Requested-With' in tree +[+][44/44] Success: could find 'DNT' in tree +[+][45/45] Success: could find 'X-Forwarded-For' in tree +[+][46/46] Success: could find 'X-Forwarded-Host' in tree +[+][47/47] Success: could find 'X-Forwarded-Proto' in tree +[+][48/48] Success: could find 'Front-End-Https' in tree +[+][49/49] Success: could find 'X-Http-Method-Override' in tree +[+][50/50] Success: could find 'X-ATT-DeviceID' in tree +[+][51/51] Success: could find 'X-Wap-Profile' in tree +[+][52/52] Success: could find 'Proxy-Connection' in tree +[+][53/53] Success: could find 'X-UIDH' in tree +[+][54/54] Success: could find 'X-Csrf-Token' in tree +[+][55/55] Success: could find 'X-Request-ID' in tree +[+][56/56] Success: could find 'X-Correlation-ID' in tree +[+][57/57] Success: could find 'Correlation-ID' in tree +[+][58/58] Success: could find 'Save-Data' in tree +[+][59/59] Success: could find 'Sec-GPC' in tree +[+][60/60] Success: could find 'Accept-CH' in tree +[+][61/61] Success: could find 'Access-Control-Allow-Origin' in tree +[+][62/62] Success: could find 'Access-Control-Allow-Credentials' in tree +[+][63/63] Success: could find 'Access-Control-Expose-Headers' in tree +[+][64/64] Success: could find 'Access-Control-Max-Age' in tree +[+][65/65] Success: could find 'Access-Control-Allow-Methods' in tree +[+][66/66] Success: could find 'Access-Control-Allow-Headers' in tree +[+][67/67] Success: could find 'Accept-Patch' in tree +[+][68/68] Success: could find 'Accept-Ranges' in tree +[+][69/69] Success: could find 'Age' in tree +[+][70/70] Success: could find 'Allow' in tree +[+][71/71] Success: could find 'Alt-Svc' in tree +[+][72/72] Success: could find 'Content-Disposition' in tree +[+][73/73] Success: could find 'Content-Language' in tree +[+][74/74] Success: could find 'Content-Location' in tree +[+][75/75] Success: could find 'Content-Range' in tree +[+][76/76] Success: could find 'Delta-Base' in tree +[+][77/77] Success: could find 'ETag' in tree +[+][78/78] Success: could find 'Expires' in tree +[+][79/79] Success: could find 'IM' in tree +[+][80/80] Success: could find 'Last-Modified' in tree +[+][81/81] Success: could find 'Link' in tree +[+][82/82] Success: could find 'Location' in tree +[+][83/83] Success: could find 'P3P' in tree +[+][84/84] Success: could find 'Preference-Applied' in tree +[+][85/85] Success: could find 'Proxy-Authenticate' in tree +[+][86/86] Success: could find 'Public-Key-Pins' in tree +[+][87/87] Success: could find 'Retry-After' in tree +[+][88/88] Success: could find 'Server' in tree +[+][89/89] Success: could find 'Set-Cookie' in tree +[+][90/90] Success: could find 'Strict-Transport-Security' in tree +[+][91/91] Success: could find 'Tk' in tree +[+][92/92] Success: could find 'Vary' in tree +[+][93/93] Success: could find 'WWW-Authenticate' in tree +[+][94/94] Success: could find 'X-Frame-Options' in tree +[+][95/95] Success: could find 'Content-Security-Policy' in tree +[+][96/96] Success: could find 'Expect-CT' in tree +[+][97/97] Success: could find 'NEL' in tree +[+][98/98] Success: could find 'Permissions-Policy' in tree +[+][99/99] Success: could find 'Refresh' in tree +[+][100/100] Success: could find 'Report-To' in tree +[+][101/101] Success: could find 'Status' in tree +[+][102/102] Success: could find 'Timing-Allow-Origin' in tree +[+][103/103] Success: could find 'X-Content-Duration' in tree +[+][104/104] Success: could find 'X-Content-Type-Options' in tree +[+][105/105] Success: could find 'X-Powered-By' in tree +[+][106/106] Success: could find 'X-Redirect-By' in tree +[+][107/107] Success: could find 'X-UA-Compatible' in tree +[+][108/108] Success: could find 'X-XSS-Protection' in tree +------------------------ +[+] Freed all memory in use +[f] Finished +==6704== +==6704== HEAP SUMMARY: +==6704== in use at exit: 0 bytes in 0 blocks +==6704== total heap usage: 2,056 allocs, 2,056 frees, 2,206,614 bytes allocated +==6704== +==6704== All heap blocks were freed -- no leaks are possible +==6704== +==6704== For lists of detected and suppressed errors, rerun with: -s +==6704== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/Makefile b/Makefile index 0d0824f..6d853f7 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ CFLAGS=-g3 LIBFLAGS=-c CFILES=streecmp.c TFILES=test.c +LEAKSFILE=LEAKS OUTPUT=streecmp TOUTPUT=test @@ -19,5 +20,8 @@ run: run_test: ./$(TOUTPUT) +memcheck: + valgrind ./$(TOUTPUT) 2>&1 | tee $(LEAKSFILE) + clean: rm -rf $(OUTPUT) $(TOUTPUT) diff --git a/README b/README index 3d5fe39..2e89972 100644 --- a/README +++ b/README @@ -29,12 +29,13 @@ TECHNICALS * traverse tree and search for str * returns: id of string or 0 if there is no match +LEAKS + + LEAKS file contains the most recent valgrind memory leaks + dump ran on test.c. + TODO - * Memory leaks - * Alloc/free functions - * NULL checking - * Memset for initialization * gentree with custom seperator COMMITS diff --git a/streecmp.c b/streecmp.c index 67e5d50..8abed44 100644 --- a/streecmp.c +++ b/streecmp.c @@ -6,13 +6,28 @@ int strs_cnt = 0; +void frenod(struct nod *nod) { + for (int i = 0; i < POOL_SIZE; i++) { + struct nod *p = nod->pool[i]; + if (p == NULL) { + continue; + } + + frenod(p); + } + + free(nod->pool); + memset(nod, 0, sizeof(struct nod)); + free(nod); +} + struct nod *allocnod(void) { struct nod *nod = (struct nod *) calloc(1, sizeof(struct nod)); if (nod == NULL) { return NULL; } - nod->pool = (struct nod **) calloc(256, sizeof(struct nod *)); + nod->pool = (struct nod **) calloc(POOL_SIZE, sizeof(struct nod *)); if (nod->pool == NULL) { free(nod); return NULL; @@ -64,18 +79,24 @@ int mkstr(struct nod *nod, char *str) { int gentree(struct nod *nod, char *strs) { char *strs_cpy = strdup(strs); if (strs_cpy == NULL) { - return -1; + goto _err; } for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok = strtok(NULL, "\n")) { int ret = mkstr(nod, tok); if (ret < 0) { - return -1; + goto _err_mkstr; } } + free(strs_cpy); return 0; + +_err_mkstr: + free(strs_cpy); +_err: + return -1; } int streecmp(struct nod *nod, char *str) { diff --git a/streecmp.h b/streecmp.h index 8793f84..ebe5aff 100644 --- a/streecmp.h +++ b/streecmp.h @@ -1,6 +1,8 @@ #ifndef STREECMP_H #define STREECMP_H +#define POOL_SIZE 256 + struct nodval { int ival; char cval; @@ -12,6 +14,7 @@ struct nod { struct nod **pool; }; +void frenod(struct nod *nod); struct nod *allocnod(void); struct nod *mknod(struct nod *nod, int loc); int mkstr(struct nod *nod, char *str); diff --git a/test.c b/test.c index bbeb4c5..d66ff3c 100644 --- a/test.c +++ b/test.c @@ -120,18 +120,18 @@ int main(void) { return -1; } - ret = gentree(rot, strs); - if (ret < 0) { - fprintf(stderr, "[-] Failed: could not generate tree\n"); - return -1; - } - char *strs_cpy = strdup(strs); if (strs_cpy == NULL) { fprintf(stderr, "[-] Failed: not enough dynamic memory\n"); return -1; } + ret = gentree(rot, strs); + if (ret < 0) { + fprintf(stderr, "[-] Failed: could not generate tree\n"); + return -1; + } + fprintf(stdout, "------------------------\n"); int cnt = 1; @@ -149,7 +149,12 @@ int main(void) { } fprintf(stdout, "------------------------\n"); - fprintf(stdout, "[*] Done\n"); + + free(strs_cpy); + frenod(rot); + + fprintf(stdout, "[+] Freed all memory in use\n"); + fprintf(stdout, "[f] Finished\n"); return 0; }