From b3a0d701470053e907aa3903e332c3e20ab42da1 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 8 Apr 2025 21:46:16 +0200 Subject: [PATCH] Fix memory safety errors in examples. --- example/ex-astar.c | 8 ++++---- example/ex-mph.c | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/example/ex-astar.c b/example/ex-astar.c index 421c6ea5..273fa0e8 100644 --- a/example/ex-astar.c +++ b/example/ex-astar.c @@ -61,11 +61,11 @@ static void astar(array_point_t path, const string_t maze, int width) for (size_t i = 0; i < 8; i++) { point_emplace(next, current->x + deltas[i].x, current->y + deltas[i].y, 0); if (string_get_char(maze, point_index(next, width)) != '#') { - int *new_cost = cost_point_get(costs, current); + int new_cost = *cost_point_get(costs, current); int *cost = cost_point_get(costs, next); - if (cost == NULL || *new_cost < *cost) { - cost_point_set_at(costs, next, *new_cost); - next->priority = *new_cost + abs(goal->x - next->x) + abs(goal->y - next->y); + if (cost == NULL || new_cost < *cost) { + cost_point_set_at(costs, next, new_cost); + next->priority = new_cost + abs(goal->x - next->x) + abs(goal->y - next->y); prio_point_push(front, next); step_point_set_at(from, next, current); } diff --git a/example/ex-mph.c b/example/ex-mph.c index ec4c6964..387ecb04 100644 --- a/example/ex-mph.c +++ b/example/ex-mph.c @@ -208,11 +208,14 @@ dict_read_from_file(dict_mph_t dict, array_string_t arr, const char filename[]) // Not clean as items of 'dict' points are linked to items of 'arr' // and the containers are not aware of this link. array_string_push_back (arr, str); - dict_mph_set_at(dict, string_get_cstr(*array_string_back(arr)), line); - line++; } } } + // Now 'arr' won't change anymore and we can get reliable pointers from it. + for M_EACH(item, arr, array_string_t) { + dict_mph_set_at(dict, string_get_cstr(*item), line); + line++; + } fclose(f); }