From a6e33089e20b299e1c36ab5e91c29125e890b2bc Mon Sep 17 00:00:00 2001 From: axtloss Date: Tue, 27 Feb 2024 20:03:23 +0100 Subject: Fix memory leaks --- fbwarn/src/warn.c | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) (limited to 'fbwarn/src/warn.c') diff --git a/fbwarn/src/warn.c b/fbwarn/src/warn.c index a495f6b..8adda19 100644 --- a/fbwarn/src/warn.c +++ b/fbwarn/src/warn.c @@ -6,16 +6,17 @@ #include #include -int main(void) { - FILE *bvgfile = readFile("./test.bvg"); +int getFuncs(char *file, char ***ret) { + FILE *bvgfile = readFile(file); char *line = NULL; size_t len = 0; ssize_t nread = 0; ssize_t totallinesize = 0; ssize_t funcCount = 0; int inComment = 0; + int newFuncsMem = sizeof(char)*1; char *funcline = strdup(""); - char **funcs = malloc(1*sizeof(char)); + char **funcs = malloc(sizeof(char)*1); while ((nread = getline(&line, &len, bvgfile)) != -1) { if (strstr(line, "/*")) { @@ -35,28 +36,41 @@ int main(void) { exit(2); totallinesize=totallinesize+nread; sprintf(funcline, "%s%s", funcline, line); - if (line[nread-2] == ')') { + if (line[nread-2] == ')' || line[nread-1] == ')') { funcCount += 1; - void *newfuncs = realloc(funcs, sizeof(char)*(sizeof(funcs)+1+strlen(funcline)*2)); + newFuncsMem = sizeof(char)*(sizeof(funcs)+sizeof(funcline)); + void *newfuncs = realloc(funcs, newFuncsMem); if (newfuncs) funcs = newfuncs; else exit(2); - funcs[funcCount-1]=strdup(funcline); + funcs[funcCount-1]=strdup(funcline); // TODO: figure out memleak totallinesize = 0; free(funcline); funcline = strdup(""); } } + free(funcline); free(line); + fclose(bvgfile); - for (int i = 0; iwidth, imgsize->height, ":3"); + free(imgsize); + free(call-strlen("IMG (")); + free(callTrim); + free(funcs); while (!WindowShouldClose ()) { - + char **funcs; + int funcCount = getFuncs(argv[1], &funcs); BeginDrawing (); ClearBackground (RAYWHITE); - for (int i = 0; i