diff options
author | Paul Janzen <pjanzen@cvs.openbsd.org> | 1998-09-16 00:44:38 +0000 |
---|---|---|
committer | Paul Janzen <pjanzen@cvs.openbsd.org> | 1998-09-16 00:44:38 +0000 |
commit | 11fe28977822e0d301e52c65e35c79866a757bf3 (patch) | |
tree | 0dad2d9fb94b4aec0fc2b56987fc3ee3e4a27f29 /games | |
parent | 8896bd95b7432af011cd98842289c9c1124a2dc6 (diff) |
Make sure malloc() succeeds
Diffstat (limited to 'games')
-rw-r--r-- | games/rogue/init.c | 11 | ||||
-rw-r--r-- | games/worm/worm.c | 21 |
2 files changed, 24 insertions, 8 deletions
diff --git a/games/rogue/init.c b/games/rogue/init.c index f413d0c3338..c1d9639dea7 100644 --- a/games/rogue/init.c +++ b/games/rogue/init.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init.c,v 1.4 1998/08/22 08:55:27 pjanzen Exp $ */ +/* $OpenBSD: init.c,v 1.5 1998/09/16 00:44:36 pjanzen Exp $ */ /* $NetBSD: init.c,v 1.4 1995/04/28 23:49:19 mycroft Exp $ */ /* @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$OpenBSD: init.c,v 1.4 1998/08/22 08:55:27 pjanzen Exp $"; +static char rcsid[] = "$OpenBSD: init.c,v 1.5 1998/09/16 00:44:36 pjanzen Exp $"; #endif #endif /* not lint */ @@ -57,6 +57,7 @@ static char rcsid[] = "$OpenBSD: init.c,v 1.4 1998/08/22 08:55:27 pjanzen Exp $" * */ +#include <err.h> #include "rogue.h" char login_name[MAX_OPT_LEN]; @@ -334,7 +335,8 @@ env_get_value(s, e, add_blank) break; } } - *s = md_malloc(MAX_OPT_LEN + 2); + if (!(*s = md_malloc(MAX_OPT_LEN + 2))) + errx(1, "malloc failure"); (void) strncpy(*s, t, i); if (add_blank) { (*s)[i++] = ' '; @@ -347,7 +349,8 @@ init_str(str, dflt) char **str, *dflt; { if (!(*str)) { - *str = md_malloc(MAX_OPT_LEN + 2); + if (!(*str = md_malloc(MAX_OPT_LEN + 2))) + errx(1, "malloc failure"); (void) strcpy(*str, dflt); } } diff --git a/games/worm/worm.c b/games/worm/worm.c index 28d9f85aa4e..ccf1b1c7f4e 100644 --- a/games/worm/worm.c +++ b/games/worm/worm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: worm.c,v 1.6 1998/08/19 07:42:21 pjanzen Exp $ */ +/* $OpenBSD: worm.c,v 1.7 1998/09/16 00:44:37 pjanzen Exp $ */ /* * Copyright (c) 1980, 1993 @@ -43,7 +43,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$OpenBSD: worm.c,v 1.6 1998/08/19 07:42:21 pjanzen Exp $"; +static char rcsid[] = "$OpenBSD: worm.c,v 1.7 1998/09/16 00:44:37 pjanzen Exp $"; #endif #endif /* not lint */ @@ -54,12 +54,12 @@ static char rcsid[] = "$OpenBSD: worm.c,v 1.6 1998/08/19 07:42:21 pjanzen Exp $" #include <ctype.h> #include <curses.h> +#include <err.h> #include <signal.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> -#define newlink() (struct body *) malloc(sizeof (struct body)); #define HEAD '@' #define BODY 'o' #define LENGTH 7 @@ -87,6 +87,7 @@ void display __P((struct body *, char)); void leave __P((int)); void life __P((void)); void newpos __P((struct body *)); +struct body *newlink __P((void)); void process __P((char)); void prize __P((void)); int rnd __P((int)); @@ -163,7 +164,7 @@ life() np = newlink(); np->next = bp; bp->prev = np; - if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4)) && (j == -1)) { + if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4) && (j == -1))) { j *= -1; np->x = bp->x; np->y = bp->y + 1; @@ -296,6 +297,18 @@ process(ch) alarm(1); } +struct body * +newlink() +{ + struct body *tmp; + + if ((tmp = (struct body *) malloc(sizeof (struct body))) == NULL) { + endwin(); + errx(1, "out of memory"); + } + return (tmp); +} + void crash() { |