From 678ea1bc35c83875ef563b607edab9224971a914 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 20 Dec 2018 10:55:01 +0000 Subject: Fixed some memory handling bugs; realloc() of zero bytes. --- csol.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'csol.c') diff --git a/csol.c b/csol.c index 1444616..ad46064 100644 --- a/csol.c +++ b/csol.c @@ -86,7 +86,7 @@ static void Shuffle(Deck deck) { int f; - for(f = 0; f < 500; f++) + for(f = 0; f < 1000; f++) { int c1 = rand() % 52; int c2 = rand() % 52; @@ -106,7 +106,7 @@ static void AddToPile(Pile *pile, Card card) if (!pile->card) { - Fatal("Failled to reallocate memory"); + Fatal("Failed to reallocate memory"); } else { @@ -122,7 +122,7 @@ static void InsertBottomOfPile(Pile *pile, Card card) if (!pile->card) { - Fatal("Failled to reallocate memory"); + Fatal("Failed to reallocate memory"); } else { @@ -147,11 +147,19 @@ static Card PopPile(Pile *pile) pile->no--; - pile->card = realloc(pile->card, sizeof *pile->card * pile->no); + if (pile->no) + { + pile->card = realloc(pile->card, sizeof *pile->card * pile->no); - if (!pile->card) + if (!pile->card) + { + Fatal("Failed to reallocate memory"); + } + } + else { - Fatal("Failled to reallocate memory"); + free(pile->card); + pile->card = NULL; } } else @@ -195,6 +203,15 @@ static void SwapPile(Pile *a, Pile *b) *b = t; } +static void FreePile(Pile *p) +{ + if (p->card) + { + free(p->card); + p->card = NULL; + } +} + static void Centre(int y, const char *p) { size_t l = strlen(p); @@ -796,6 +813,18 @@ static void Klondike(int draw) { WinScreen(); } + + FreePile(&pile); + FreePile(&turned); + FreePile(&hearts); + FreePile(&diamonds); + FreePile(&clubs); + FreePile(&spades); + for(f = 0; f < 7; f++) + { + FreePile(&column_down[f]); + FreePile(&column_up[f]); + } } int main(int argc, char *argv[]) -- cgit v1.2.3