Skip to content

Commit

Permalink
check for malloc failures
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoscon committed Aug 4, 2013
1 parent 568bdbe commit 523a667
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main.c
Expand Up @@ -100,6 +100,13 @@ static void game_init(state_st *state)
// generate random names for items
for (i = 0; i < NUM_SCROLLS; ++i) {
state->scroll_names[i] = calloc(ITEM_NAME_MAX, sizeof(char));
if (!state->scroll_names[i]) {
endwin();
fprintf(stderr, "%s:%d - %s() - out of memory\n", __FILE__, __LINE__, __FUNCTION__);
free_state(state);
exit(EXIT_FAILURE);
}

random_string(state->scroll_names[i], ITEM_NAME_MAX-1);
}

Expand Down
18 changes: 18 additions & 0 deletions src/rogue.c
Expand Up @@ -227,6 +227,12 @@ void rings_init(state_st *state)

for (i = 0; i < NUM_RINGS; ++i) {
state->ring_names[i] = strdup(ring_types[indices[i]]);
if (!state->ring_names[i]) {
endwin();
fprintf(stderr, "%s:%d - %s() - out of memory\n", __FILE__, __LINE__, __FUNCTION__);
free_state(state);
exit(EXIT_FAILURE);
}
}

}
Expand All @@ -245,6 +251,12 @@ void potions_init(state_st *state)

for (i = 0; i < NUM_POTIONS; ++i) {
state->potion_names[i] = strdup(potion_types[indices[i]]);
if (!state->potion_names[i]) {
endwin();
fprintf(stderr, "%s:%d - %s() - out of memory\n", __FILE__, __LINE__, __FUNCTION__);
free_state(state);
exit(EXIT_FAILURE);
}
}

}
Expand All @@ -263,6 +275,12 @@ void wands_init(state_st *state)

for (i = 0; i < NUM_WANDS; ++i) {
state->wand_names[i] = strdup(wand_types[indices[i]]);
if (!state->wand_names[i]) {
endwin();
fprintf(stderr, "%s:%d - %s() - out of memory\n", __FILE__, __LINE__, __FUNCTION__);
free_state(state);
exit(EXIT_FAILURE);
}
}

}
5 changes: 5 additions & 0 deletions src/rogue.h
Expand Up @@ -64,6 +64,11 @@
// and bottom row for stats
#define MAP_ROW MIN_ROW - 2

// min/max # of rooms, and minimum room dimensions
#define ROOMS_MAX 9
#define ROOMS_MIN 3
#define ROOM_DIM_MIN 4

// max number of doors per room
#define MAX_DOORS 4

Expand Down

0 comments on commit 523a667

Please sign in to comment.