From fc9ae26aacede490d32f0b31d314f2c8286d158a Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 6 Jun 2022 11:36:51 +0800 Subject: Exit if malloc() fails --- sirtet.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'sirtet.c') diff --git a/sirtet.c b/sirtet.c index 0a8cc8f..61427c1 100644 --- a/sirtet.c +++ b/sirtet.c @@ -7,6 +7,7 @@ #include #include "pieces.h" #include "ui.h" +#include "util.h" void clearmap(char** map, int mapH, int mapW) { for (int r = 0; r < mapH; r++) { @@ -23,6 +24,9 @@ int clearfull(char** map, int mapH, int mapW) { // returns total number of blocks cleared bool* rows_to_clear = malloc(mapH * sizeof(bool)); bool* cols_to_clear = malloc(mapW * sizeof(bool)); + if (rows_to_clear == NULL || cols_to_clear == NULL) + mallocfail(); + int rtc_count = 0; int ctc_count = 0; // scan all rows and columns @@ -65,10 +69,18 @@ int clearfull(char** map, int mapH, int mapW) { void sirtet(int H, int W, int P) { // init memory and game state char** map = malloc(H * sizeof(char*)); + if (map == NULL) + mallocfail(); + for (int r = 0; r < H; r++) { map[r] = malloc(W * sizeof(char)); + if (map[r] == NULL) + mallocfail(); } struct piece** hand = malloc(P * sizeof(struct piece*)); + if (hand == NULL) + mallocfail(); + srand(time(NULL)); clearmap(map, H, W); refillpieces(hand, P); -- cgit v1.2.3