diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-06-06 11:36:51 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-06-06 11:36:51 +0800 |
commit | fc9ae26aacede490d32f0b31d314f2c8286d158a (patch) | |
tree | e47a27c91f38c4ae2b54dfca691b96cc7b62168f /pieces.c | |
parent | 6e945b3f487d92d3cdf8706164da7040b9cb375a (diff) |
Exit if malloc() fails
Diffstat (limited to 'pieces.c')
-rw-r--r-- | pieces.c | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -2,6 +2,7 @@ #include <stdbool.h> #include <string.h> #include "pieces.h" +#include "util.h" const int N_SHAPES = 17; const struct piece SHAPES[] = { @@ -27,6 +28,9 @@ const struct piece SHAPES[] = { void transpose(struct piece* pc) { // transpose blocks char* old = malloc(strlen(pc->blocks) + 1); + if (old == NULL) + mallocfail(); + strcpy(old, pc->blocks); // before transposition for (int r = 0; r < pc->h; r++) { for (int c = 0; c < pc->w; c++) { @@ -44,6 +48,9 @@ void rotate(struct piece* pc) { // rotate `pc` ccw by 90 degrees // transpose blocks char* old = malloc(strlen(pc->blocks) + 1); + if (old == NULL) + mallocfail(); + strcpy(old, pc->blocks); // before transposition for (int r = 0; r < pc->h; r++) { for (int c = 0; c < pc->w; c++) { @@ -62,8 +69,14 @@ struct piece* randpiece() { // then do random transformations to the copy struct piece shape = SHAPES[rand() % N_SHAPES]; char* blocks = malloc(strlen(shape.blocks) + 1); + if (blocks == NULL) + mallocfail(); + strcpy(blocks, shape.blocks); struct piece* pc = malloc(sizeof(struct piece)); + if (pc == NULL) + mallocfail(); + pc->h = shape.h; pc->w = shape.w; pc->points = shape.points; |