summaryrefslogtreecommitdiff
path: root/sirtet.c
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-06-06 11:36:51 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-06-06 11:36:51 +0800
commitfc9ae26aacede490d32f0b31d314f2c8286d158a (patch)
treee47a27c91f38c4ae2b54dfca691b96cc7b62168f /sirtet.c
parent6e945b3f487d92d3cdf8706164da7040b9cb375a (diff)
Exit if malloc() fails
Diffstat (limited to 'sirtet.c')
-rw-r--r--sirtet.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/sirtet.c b/sirtet.c
index 0a8cc8f..61427c1 100644
--- a/sirtet.c
+++ b/sirtet.c
@@ -7,6 +7,7 @@
#include <ncurses.h>
#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);