From 2a4356ed6bc2933e857dbbcf727802bfc8532770 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Thu, 13 Jun 2024 15:35:42 +0200 Subject: [PATCH] check memory allocations --- function.c | 8 ++++++++ function.h | 8 ++++++++ scrabble.c | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/function.c b/function.c index e53d93f..94d406f 100644 --- a/function.c +++ b/function.c @@ -35,7 +35,9 @@ int strmaxlen (char *str, char ch) play_t *initplay (int xsize, int ysize) { play_t *play = (play_t *) malloc (sizeof (play_t)); + CHECKALLOC (play); play->tab = (char *) malloc (xsize * ysize); + CHECKALLOC (play->tab); memset (play->tab, ' ', xsize * ysize); play->xsize = xsize; play->ysize = ysize; @@ -94,7 +96,9 @@ char gessletter (play_t *play, int x, int y) draw_t *newdraw (int nbtiles) { draw_t *draw = (draw_t *) malloc (sizeof (draw_t)); + CHECKALLOC (draw); draw->tiles = (char *) calloc (1, nbtiles); + CHECKALLOC (draw->tiles); draw->nbtiles = nbtiles; return draw; } @@ -335,11 +339,15 @@ word_t *initword (int maxnbwords, int maxlength) { int i; word_t *words = (word_t *) calloc (1, sizeof (word_t)); + CHECKALLOC (words); words->tab = (char **) calloc (maxnbwords, sizeof (char *)); + CHECKALLOC (words->tab); for (i = 0; i < maxnbwords; i++) { words->tab[i] = (char *) calloc (maxlength, 1); + CHECKALLOC (words->tab[i]); } words->status = (spelling_e *) calloc (maxnbwords, sizeof (spelling_e)); + CHECKALLOC (words->status); words->maxnbwords = maxnbwords; return words; } diff --git a/function.h b/function.h index fc29b42..1aab1b6 100644 --- a/function.h +++ b/function.h @@ -3,6 +3,14 @@ #include "type.h" +#define CHECKALLOC(ptr) \ + do { \ + if ((ptr) == NULL) { \ + VERBOSE (ERROR, fprintf (stderr, "can't get enough memory for '%s'\n", #ptr)); \ + exit (1); \ + } \ + } while (0) + int contains (char *list, char *str); int strmaxlen (char *str, char ch); diff --git a/scrabble.c b/scrabble.c index c9bdd18..c289cae 100644 --- a/scrabble.c +++ b/scrabble.c @@ -165,12 +165,14 @@ int main (int argc, char *argv[]) draw_t *draw = initdraw (bag); draw_t **game = (draw_t **) calloc (nbplayers, sizeof (draw_t *)); + CHECKALLOC (game); for (n = 0; n < nbplayers; n++) { game[n] = retrievetiles (draw, board->length, NULL, 0); } VERBOSE (DEBUG, printf ("bag: 0x%p\ndraw: 0x%p\ngame: 0x%p\ngame[0]: 0x%p", bag, draw, game, game[0])); play_t **turn = (play_t **) calloc (nbplayers, sizeof (play_t *)); + CHECKALLOC (turn); for (n = 0; n < nbplayers; n++) { turn[n] = initplay (board->xsize, board->ysize); } @@ -191,7 +193,9 @@ int main (int argc, char *argv[]) int xhelpwin = xvaluewin + xoffset + 10; int yhelpwin = yoffset - 1; int *xdrawwin = (int *) calloc (nbplayers, sizeof (int)); + CHECKALLOC (xdrawwin); int *ydrawwin = (int *) calloc (nbplayers, sizeof (int)); + CHECKALLOC (ydrawwin); for (n = 0; n < nbplayers; n++) { xdrawwin[n] = (n + 1) * xoffset + n * (board->length * 2 - 1); ydrawwin[n] = 2 * yoffset + max (board->ysize, 1 + (bag->nbletters + 1) /2); @@ -212,6 +216,7 @@ int main (int argc, char *argv[]) int y = (play->ysize + 1)/ 2 - 1; int *score = (int *) calloc (nbplayers, sizeof (int)); + CHECKALLOC (score); for (n = 0; n < nbplayers; n++) { drawwindow (game[n], score[n], xdrawwin[n], ydrawwin[n], 0); } -- 2.30.2