From 94ee06e01b51e86ddd7a29a5e5288b5e86130acc Mon Sep 17 00:00:00 2001 From: Mike Bell Date: Wed, 8 Jan 2025 20:01:41 +0000 Subject: [PATCH] PicoVector: Fix ppp arc memory corruption. --- .../pico_vector/pretty-poly-primitives.h | 4 ++-- libraries/pico_vector/pretty-poly.h | 21 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/libraries/pico_vector/pretty-poly-primitives.h b/libraries/pico_vector/pretty-poly-primitives.h index aa633bbb..24531028 100644 --- a/libraries/pico_vector/pretty-poly-primitives.h +++ b/libraries/pico_vector/pretty-poly-primitives.h @@ -168,7 +168,7 @@ pp_poly_t* ppp_circle(ppp_circle_def d) { pp_poly_t* ppp_arc(ppp_arc_def d) { pp_poly_t *poly = pp_poly_new(); pp_path_t *path = pp_poly_add_path(poly); - pp_path_t *inner = (pp_path_t *)(d.s == 0.0f ? NULL : calloc(1, sizeof(pp_path_t))); + pp_path_t *inner = d.s != 0.0f ? pp_path_new() : NULL; // no thickness, so add centre point to make pie shape if(!inner) pp_path_add_point(path, (pp_point_t){d.x, d.y}); @@ -185,7 +185,7 @@ pp_poly_t* ppp_arc(ppp_arc_def d) { if(inner) { // append the inner path pp_path_add_points(path, inner->points, inner->count); - free(inner->points); free(inner); + pp_path_free(inner); } return poly; diff --git a/libraries/pico_vector/pretty-poly.h b/libraries/pico_vector/pretty-poly.h index 69dcacad..f9b76287 100644 --- a/libraries/pico_vector/pretty-poly.h +++ b/libraries/pico_vector/pretty-poly.h @@ -270,14 +270,26 @@ pp_poly_t *pp_poly_new() { return poly; } +pp_path_t *pp_path_new() { + pp_path_t *path = (pp_path_t *)PP_MALLOC(sizeof(pp_path_t)); + memset(path, 0, sizeof(pp_path_t)); + path->storage = 8; + path->points = (pp_point_t *)PP_MALLOC(sizeof(pp_point_t) * path->storage); + return path; +} + +void pp_path_free(pp_path_t *path) { + PP_FREE(path->points); + PP_FREE(path); +} + void pp_poly_free(pp_poly_t *poly) { if(poly->paths) { pp_path_t *path = poly->paths; while(path) { - PP_FREE(path->points); pp_path_t *free_path = path; path = path->next; - PP_FREE(free_path); + pp_path_free(free_path); } } PP_FREE(poly); @@ -302,10 +314,7 @@ int pp_poly_path_count(pp_poly_t *poly) { } pp_path_t* pp_poly_add_path(pp_poly_t *poly) { - pp_path_t *path = (pp_path_t *)PP_MALLOC(sizeof(pp_path_t)); - memset(path, 0, sizeof(pp_path_t)); - path->storage = 8; - path->points = (pp_point_t *)PP_MALLOC(sizeof(pp_point_t) * path->storage); + pp_path_t *path = pp_path_new(); if(!poly->paths) { poly->paths = path;