Commit a59afe54 authored by Pedro Gonnet's avatar Pedro Gonnet
Browse files

add range checks of engine_addlink.

parent 3be87eb5
......@@ -74,7 +74,11 @@ int engine_rank;
struct link *engine_addlink(struct engine *e, struct link *l, struct task *t) {
struct link *res = &e->links[atomic_inc(&e->nr_links)];
const int ind = atomic_inc(&e->nr_links);
if (ind >= e->size_links) {
error("Link table overflow.");
}
struct link *res = &e->links[ind];
res->next = l;
res->t = t;
return res;
......@@ -1106,7 +1110,8 @@ void engine_maketasks(struct engine *e) {
is the number of cells (s->tot_cells) times the number of neighbours (27)
times the number of interaction types (2, density and force). */
if (e->links != NULL) free(e->links);
if ((e->links = malloc(sizeof(struct link) * s->tot_cells * 27 * 2)) == NULL)
e->size_links = s->tot_cells * 27 * 2;
if ((e->links = malloc(sizeof(struct link) * e->size_links)) == NULL)
error("Failed to allocate cell-task links.");
e->nr_links = 0;
......
......@@ -124,7 +124,7 @@ struct engine {
/* Linked list for cell-task association. */
struct link *links;
int nr_links;
int nr_links, size_links;
};
/* Function prototypes. */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment