diff options
author | axtloss <axtlos@getcryst.al> | 2024-02-27 20:03:23 +0100 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2024-02-27 20:03:23 +0100 |
commit | a6e33089e20b299e1c36ab5e91c29125e890b2bc (patch) | |
tree | 4a65de9a67c9161ca8cd87c463cf221fb4005921 /fbwarn/src | |
parent | c9b119217829f976a7830a0555650401cf9df2cb (diff) | |
download | fsverify-a6e33089e20b299e1c36ab5e91c29125e890b2bc.tar.gz fsverify-a6e33089e20b299e1c36ab5e91c29125e890b2bc.tar.bz2 |
Fix memory leaks
Diffstat (limited to 'fbwarn/src')
-rw-r--r-- | fbwarn/src/extString.c | 1 | ||||
-rw-r--r-- | fbwarn/src/parseBVG.c | 76 | ||||
-rw-r--r-- | fbwarn/src/warn.c | 54 |
3 files changed, 88 insertions, 43 deletions
diff --git a/fbwarn/src/extString.c b/fbwarn/src/extString.c index 361afa7..6538dac 100644 --- a/fbwarn/src/extString.c +++ b/fbwarn/src/extString.c @@ -27,7 +27,6 @@ char *trim(char *s) end = result + strlen(result) - 1; while(end > result && isspace((unsigned char)*end)) end--; - // Write new null terminator character end[1] = '\0'; return result; diff --git a/fbwarn/src/parseBVG.c b/fbwarn/src/parseBVG.c index 4209383..5b2a511 100644 --- a/fbwarn/src/parseBVG.c +++ b/fbwarn/src/parseBVG.c @@ -22,6 +22,7 @@ void BVGDrawRoundedRectangle(BVGRoundedRectangle *rectangle) { } else { DrawRectangleRoundedLines(rectangle->rectangle.rayrectangle, rectangle->roundness, rectangle->segments, rectangle->rectangle.lineThickness, rectangle->rectangle.color); } + return; } void BVGDrawCircle(BVGCircle *circle) { @@ -31,6 +32,7 @@ void BVGDrawCircle(BVGCircle *circle) { } else { DrawCircle(circle->centerX, circle->centerY, circle->radius, circle->color); } + return; } void BVGDrawRing(BVGRing *ring) { @@ -44,10 +46,12 @@ void BVGDrawTriangle(BVGTriangle *triangle) { } else { DrawTriangleLines(triangle->corner1, triangle->corner2, triangle->corner3, triangle->color); } + return; } void BVGDrawText(BVGText *text) { DrawText(text->text, text->x, text->y, text->fontSize, text->color); + return; } // reads a given file @@ -65,13 +69,15 @@ FILE *readFile(char *path) { call into a single line call */ char *multiToSingle(char *s) { - // allocating the size of lines is safe since characters arent added - char *midresult = malloc (strlen(s)+1); - char *notab = malloc(strlen(s)+1); - char *line; + char *midresult; + char *notab; + char *result; midresult = replaceStr(s, "\n", ""); notab = replaceStr(midresult, "\t", ""); - return replaceStr(notab, ", ", ","); + result = replaceStr(midresult, ", ", ","); + free(midresult); + free(notab); + return result; } /* @@ -113,7 +119,7 @@ void orderArgs(char *res[], char *argv[], int n, char *knownArgs[]) { */ Color *parseColorFromHex(char *hex) { Color *clr = malloc(sizeof(Color)); - int r,g,b,a; + int r,g,b,a = 0; if (strlen(hex) == 6) { sscanf(hex, "%02x%02x%02x", &r, &g, &b); clr->r=r; clr->g=g; clr->b=b; clr->a=255; @@ -133,8 +139,10 @@ Color *parseColorFromHex(char *hex) { bool parseBoolValue(char *value) { char *valueLower = strdup(strlwr(value)); if (strcmp(valueLower, "false") == 0) { + free(valueLower); return false; } else { + free(valueLower); return true; } } @@ -186,6 +194,9 @@ BVGRectangle *BVGParseRectangle(char *argv[7]) { result->color=*clr; result->fill=fill; result->lineThickness=thickness; + + free(clr); + free(rectangle); return result; } @@ -237,6 +248,8 @@ BVGCircle *BVGParseCircle(char *argv[4]) { result->centerY=y; result->color=*clr; result->radius=radius; + + free(clr); return result; } @@ -268,6 +281,8 @@ BVGCircle *BVGParseCircleSegment(char *argv[7]) { result->segments=segments; result->startAngle=startAngle; result->endAngle=endAngle; + + free(clr); return result; } @@ -295,6 +310,8 @@ BVGRing *BVGParseRing(char *argv[8]) { result->startAngle=startAngle; result->endAngle=endAngle; result->segmets=segments; result->color=*clr; + + free(clr); return result; } @@ -323,6 +340,8 @@ BVGTriangle *BVGParseTriangle(char *argv[8]) { result->corner3 = (Vector2){x3, y3}; result->fill = fill; result->color = *clr; + + free(clr); return result; } @@ -349,6 +368,8 @@ BVGText *BVGParseText(char *argv[5]) { result->text=text; result->x=x; result->y=y; result->fontSize=size; result->color=*clr; + + free(clr); return result; } @@ -367,51 +388,52 @@ void matchFunctionCall(char *call) { char *argv[7]; call = call+strlen("rectangle ("); collectArgs(argv, call, 7); - BVGRectangle *rect = malloc(sizeof(BVGRectangle)); - rect = BVGParseRectangle(argv); - BVGDrawRectangle(rect); + BVGRectangle *shape = BVGParseRectangle(argv); + BVGDrawRectangle(shape); + free(shape); } else if (strcmp(function, "roundedrectangle") == 0) { char *argv[9]; call = call+strlen("roundedrectangle ("); collectArgs(argv, call, 9); - BVGRoundedRectangle *roundrect = malloc(sizeof(BVGRoundedRectangle)); - roundrect = BVGParseRoundedRectangle(argv); - BVGDrawRoundedRectangle(roundrect); + BVGRoundedRectangle *shape = BVGParseRoundedRectangle(argv); + BVGDrawRoundedRectangle(shape); + free(shape); } else if (strcmp(function, "circle") == 0) { char *argv[4]; call = call+strlen("circle ("); collectArgs(argv, call, 4); - BVGCircle *circle = malloc(sizeof(BVGCircle)); - circle = BVGParseCircle(argv); - BVGDrawCircle(circle); + BVGCircle *shape = BVGParseCircle(argv); + BVGDrawCircle(shape); + free(shape); } else if (strcmp(function, "circlesegment") == 0) { char *argv[7]; call = call+strlen("circlesegment ("); collectArgs(argv, call, 7); - BVGCircle *circle = malloc(sizeof(BVGCircle)); - circle = BVGParseCircleSegment(argv); - BVGDrawCircle(circle); + BVGCircle *shape = BVGParseCircleSegment(argv); + BVGDrawCircle(shape); + free(shape); } else if (strcmp(function, "ring") == 0) { char *argv[8]; call = call+strlen("ring ("); collectArgs(argv, call, 8); - BVGRing *ring = malloc(sizeof(BVGRing)); - ring = BVGParseRing(argv); - BVGDrawRing(ring); + BVGRing *shape = BVGParseRing(argv); + BVGDrawRing(shape); + free(shape); } else if (strcmp(function, "triangle") == 0) { char *argv[8]; call = call+strlen("triangle ("); collectArgs(argv, call, 8); - BVGTriangle *triangle = malloc(sizeof(BVGTriangle)); - triangle = BVGParseTriangle(argv); - BVGDrawTriangle(triangle); + BVGTriangle *shape = BVGParseTriangle(argv); + BVGDrawTriangle(shape); + free(shape); } else if (strcmp(function, "text") == 0) { char *argv[5]; call = call+strlen("text ("); collectArgs(argv, call, 5); - BVGText *text = malloc(sizeof(BVGText)); - text = BVGParseText(argv); - BVGDrawText(text); + BVGText *shape = BVGParseText(argv); + BVGDrawText(shape); + free(shape); } + free(function); return; } 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 <stdlib.h> #include <unistd.h> -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; i<funcCount; i++) { - printf("%s", funcs[i]); - } + //char **alloc = realloc(*ret, newFuncsMem); + //if (alloc) + *ret = funcs; + //else + // exit(2); + // free(alloc); + //memcpy(*ret, funcs, newFuncsMem); + //free(funcs); + return funcCount; +} - fclose(bvgfile); +int main(int argc, char **argv) { + + char **funcs; + int funcCount = getFuncs(argv[1], &funcs); - char *call = strdup(multiToSingle(funcs[0])); + char *call = multiToSingle(funcs[0]); char *args[2]; call=call+strlen("IMG ("); char *callTrim = trim(call); @@ -66,23 +80,33 @@ int main(void) { InitWindow (imgsize->width, 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<funcCount; i++) { - matchFunctionCall(multiToSingle(funcs[i])); + // i = 1 since the first item is always IMG + for (int i = 1; i<funcCount; i++) { + char *single = multiToSingle(funcs[i]); + matchFunctionCall(single); + free(single); } char *text = malloc(strlen("100")*100); sprintf(text, "%d", GetFPS()); DrawText(text, 2, 2, 20, MAROON); + free(text); EndDrawing (); + free(funcs); } CloseWindow (); - + return 0; } |