From 3ac3b0ff112bba7f0bffa651e6e1cf9275b98c0e Mon Sep 17 00:00:00 2001 From: Alexander Matthes Date: Mon, 1 May 2017 19:26:08 +0200 Subject: [PATCH] Moved some functionality into del_hare function to fix "access of freed memory" error. --- ToDo | 3 ++- bullet.c | 14 +------------- hase.c | 10 ++++------ logic.c | 12 +----------- player.c | 19 +++++++++++++++---- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/ToDo b/ToDo index 8b13789..b331ef9 100644 --- a/ToDo +++ b/ToDo @@ -1 +1,2 @@ - +* Fix bug +* Improve camera diff --git a/bullet.c b/bullet.c index 664f0db..90613ac 100644 --- a/bullet.c +++ b/bullet.c @@ -802,19 +802,7 @@ int updateBullets() hare->high_hops = 3; } if (hare->health <= 0) - { - if (hare == player[j]->activeHare || - hare == player[j]->setActiveHare) - { - player[j]->setActiveHare = hare->next; - player[j]->activeHare = NULL; - if (j == active_player)//Suicid! - next_player(); - } - hare = del_hare(hare,&(player[j]->firstHare)); - if (player[j]->firstHare == NULL) - alive_count--; - } + hare = del_hare(hare,player[j]); else hare = hare->next; } diff --git a/hase.c b/hase.c index 4e225d5..cbf59fc 100644 --- a/hase.c +++ b/hase.c @@ -1433,9 +1433,9 @@ int calc(Uint32 steps) &d); if (player[active_player]->activeHare->circle_checkpoint_hare[j]->health <= 0) { - player[active_player]->activeHare->circle_checkpoint_hare[j] = del_hare(player[active_player]->activeHare->circle_checkpoint_hare[j],&(p->firstHare)); - if (p->firstHare == NULL) - alive_count--; + del_hare(player[active_player]->activeHare->circle_checkpoint_hare[j],p); + if (alive_count < 2) + result = 2; } free(bullet); once = 1; @@ -1804,9 +1804,7 @@ int calc(Uint32 steps) &d); if (player[active_player]->activeHare->circle_checkpoint_hare[j]->health <= 0) { - player[active_player]->activeHare->circle_checkpoint_hare[j] = del_hare(player[active_player]->activeHare->circle_checkpoint_hare[j],&(p->firstHare)); - if (p->firstHare == NULL) - alive_count--; + del_hare(player[active_player]->activeHare->circle_checkpoint_hare[j],p); if (alive_count < 2) result = 2; } diff --git a/logic.c b/logic.c index 4390180..c0fa173 100644 --- a/logic.c +++ b/logic.c @@ -70,17 +70,7 @@ int do_physics() } if (((hase_game->options.bytewise.ragnarok_border & 15) == 0) && (hare->x < 0 || hare->y < 0 || hare->x >= spIntToFixed(LEVEL_WIDTH) || hare->y >= spIntToFixed(LEVEL_HEIGHT))) { - if (hare == player[j]->activeHare || - hare == player[j]->setActiveHare) - { - player[j]->setActiveHare = hare->next; - player[j]->activeHare = NULL; - if (j == active_player)//Suicid! - next_player(); - } - hare = del_hare(hare,&(player[j]->firstHare)); - if (player[j]->firstHare == NULL) - alive_count--; + hare = del_hare(hare,player[j]); if (alive_count < 2) return 1; } diff --git a/player.c b/player.c index 81f7e7a..5c3fa02 100644 --- a/player.c +++ b/player.c @@ -713,17 +713,28 @@ void hareplosion(pHare hare) } } -pHare del_hare(pHare hare,pHare* firstHare) +pHare del_hare(pHare hare,pPlayer p) { + if (hare == p->activeHare || + hare == p->setActiveHare) + { + p->setActiveHare = hare->next; + p->activeHare = NULL; + if (p == player[active_player])//Suicid! + next_player(); + } pHare next = NULL; if (hare->next == hare) - *firstHare = NULL; + { + p->firstHare = NULL; + alive_count--; + } else { hare->before->next = hare->next; hare->next->before = hare->before; - if (*firstHare == hare) - *firstHare = hare->next; + if (p->firstHare == hare) + p->firstHare = hare->next; next = hare->next; } hareplosion(hare);