check memory allocations
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 13 Jun 2024 13:35:42 +0000 (15:35 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 13 Jun 2024 13:35:42 +0000 (15:35 +0200)
function.c
function.h
scrabble.c

index e53d93f0c3395dd60449d1fc58d5c32f55ce42c8..94d406f17bd07e2d115e4044a40108617b666553 100644 (file)
@@ -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;
 }
index fc29b423e3c352294c28e6488a79846b5977b014..1aab1b6a48beef462b2d1b11cd516fdb59894d75 100644 (file)
@@ -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);
index c9bdd18150cb6f64b8684183963cfc6705537e5f..c289caef84ceafb783ce633ab8fed24faf776a37 100644 (file)
@@ -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);
     }