diff --git a/graph.c b/graph.c index 65e6e89..897ee51 100644 --- a/graph.c +++ b/graph.c @@ -40,16 +40,25 @@ void Graph_free(Graph *graph) Node *Node_new() { - return climblib_malloc(sizeof(Node)); + Node *node; + + if ((node = climblib_malloc(sizeof(Node)))) { + node->next = NULL; + node->edges = NULL; + node->type = NodeType_UNDEFINED; + } + + return node; } Node *Node_new_formation(Formation *formation) { Node *node; - node = Node_new(); - node->type = NodeType_FORMATION; - node->data.formation = formation; + if ((node = Node_new())) { + node->type = NodeType_FORMATION; + node->data.formation = formation; + } return node; } @@ -59,9 +68,10 @@ Node *Node_new_climb(Climb *climb) Node *node; - node = Node_new(); - node->type = NodeType_CLIMB; - node->data.climb = climb; + if ((node = Node_new())) { + node->type = NodeType_CLIMB; + node->data.climb = climb; + } return node; } diff --git a/tests/graph.c b/tests/graph.c index 35ba9e6..43bebf2 100644 --- a/tests/graph.c +++ b/tests/graph.c @@ -33,6 +33,11 @@ START_TEST(test_nodes) Graph *graph; Node *a, *b, *c, *n; + climblib_set_alloc(bad_malloc, NULL, NULL); + a = Node_new(); + ck_assert_ptr_null(a); + climblib_set_alloc(NULL, NULL, NULL); + graph = Graph_new(); a = Node_new(); @@ -70,6 +75,11 @@ START_TEST(test_formation_node) Formation *formation; Node *formation_node; + climblib_set_alloc(bad_malloc, NULL, NULL); + formation_node = Node_new_formation(NULL); + ck_assert_ptr_null(formation_node); + climblib_set_alloc(NULL, NULL, NULL); + formation = Formation_new(); ck_assert_ptr_nonnull(formation_node = Node_new_formation(formation)); @@ -86,6 +96,11 @@ START_TEST(test_climb_node) Climb *climb; Node *climb_node; + climblib_set_alloc(bad_malloc, NULL, NULL); + climb_node = Node_new_climb(NULL); + ck_assert_ptr_null(climb_node); + climblib_set_alloc(NULL, NULL, NULL); + climb = Climb_new(); ck_assert_ptr_nonnull(climb_node = Node_new_climb(climb));