Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/games Check for failure of malloc() and calloc() at various ...
details: https://anonhg.NetBSD.org/src/rev/10ad1ec55d73
branches: trunk
changeset: 476204:10ad1ec55d73
user: jsm <jsm%NetBSD.org@localhost>
date: Thu Sep 09 17:27:58 1999 +0000
description:
Check for failure of malloc() and calloc() at various places in the games.
diffstat:
games/gomoku/pickmove.c | 10 ++++++++--
games/monop/cards.c | 6 ++++--
games/monop/initdeck.c | 7 +++++--
games/monop/monop.c | 11 ++++++++---
games/monop/monop.h | 3 ++-
games/monop/prop.c | 6 ++++--
games/rogue/init.c | 8 ++++++--
games/snake/snscore/snscore.c | 9 +++++----
games/worm/worm.c | 11 +++++++++--
9 files changed, 51 insertions(+), 20 deletions(-)
diffs (truncated from 304 to 300 lines):
diff -r 147c95927da0 -r 10ad1ec55d73 games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/gomoku/pickmove.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pickmove.c,v 1.7 1999/09/08 21:45:27 jsm Exp $ */
+/* $NetBSD: pickmove.c,v 1.8 1999/09/09 17:27:58 jsm Exp $ */
/*
* Copyright (c) 1994
@@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)pickmove.c 8.2 (Berkeley) 5/3/95";
#else
-__RCSID("$NetBSD: pickmove.c,v 1.7 1999/09/08 21:45:27 jsm Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.8 1999/09/09 17:27:58 jsm Exp $");
#endif
#endif /* not lint */
@@ -479,6 +479,8 @@
/* make a new combo! */
ncbp = (struct combostr *)malloc(sizeof(struct combostr) +
2 * sizeof(struct combostr *));
+ if (ncbp == NULL)
+ panic("Out of memory!");
scbpp = (struct combostr **)(ncbp + 1);
fcbp = fsp->s_frame[r];
if (ocbp < fcbp) {
@@ -718,6 +720,8 @@
/* make a new combo! */
ncbp = (struct combostr *)malloc(sizeof(struct combostr) +
(cbp->c_nframes + 1) * sizeof(struct combostr *));
+ if (ncbp == NULL)
+ panic("Out of memory!");
scbpp = (struct combostr **)(ncbp + 1);
if (sortcombo(scbpp, (struct combostr **)(cbp + 1), ocbp)) {
free(ncbp);
@@ -918,6 +922,8 @@
/* add the combo to the list of empty spots */
nep = (struct elist *)malloc(sizeof(struct elist));
+ if (nep == NULL)
+ panic("Out of memory!");
nep->e_combo = ocbp;
nep->e_off = s;
nep->e_frameindex = i;
diff -r 147c95927da0 -r 10ad1ec55d73 games/monop/cards.c
--- a/games/monop/cards.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/monop/cards.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cards.c,v 1.9 1999/09/08 21:57:18 jsm Exp $ */
+/* $NetBSD: cards.c,v 1.10 1999/09/09 17:27:58 jsm Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: cards.c,v 1.9 1999/09/08 21:57:18 jsm Exp $");
+__RCSID("$NetBSD: cards.c,v 1.10 1999/09/09 17:27:58 jsm Exp $");
#endif
#endif /* not lint */
@@ -102,6 +102,8 @@
int i;
dp->offsets = (off_t *) calloc(sizeof (off_t), dp->num_cards);
+ if (dp->offsets == NULL)
+ errx(1, "out of memory");
if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
dp->num_cards) {
perror(cardfile);
diff -r 147c95927da0 -r 10ad1ec55d73 games/monop/initdeck.c
--- a/games/monop/initdeck.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/monop/initdeck.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: initdeck.c,v 1.9 1999/09/08 21:57:18 jsm Exp $ */
+/* $NetBSD: initdeck.c,v 1.10 1999/09/09 17:27:59 jsm Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -43,10 +43,11 @@
#if 0
static char sccsid[] = "@(#)initdeck.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: initdeck.c,v 1.9 1999/09/08 21:57:18 jsm Exp $");
+__RCSID("$NetBSD: initdeck.c,v 1.10 1999/09/09 17:27:59 jsm Exp $");
#endif
#endif /* not lint */
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -101,6 +102,8 @@
*/
CC_D.offsets = (off_t *)calloc(CC_D.num_cards + 1, sizeof (off_t));
CH_D.offsets = (off_t *)calloc(CH_D.num_cards + 1, sizeof (off_t));
+ if (CC_D.offsets == NULL || CH_D.offsets == NULL)
+ errx(1, "out of memory");
fseek(inf, 0L, SEEK_SET);
if ((outf = fopen(outfile, "w")) == NULL) {
perror(outfile);
diff -r 147c95927da0 -r 10ad1ec55d73 games/monop/monop.c
--- a/games/monop/monop.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/monop/monop.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: monop.c,v 1.7 1999/09/08 21:45:28 jsm Exp $ */
+/* $NetBSD: monop.c,v 1.8 1999/09/09 17:27:59 jsm Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -43,7 +43,7 @@
#if 0
static char sccsid[] = "@(#)monop.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: monop.c,v 1.7 1999/09/08 21:45:28 jsm Exp $");
+__RCSID("$NetBSD: monop.c,v 1.8 1999/09/09 17:27:59 jsm Exp $");
#endif
#endif /* not lint */
@@ -116,6 +116,8 @@
break;
}
cur_p = play = (PLAY *) calloc(num_play, sizeof (PLAY));
+ if (play == NULL)
+ errx(1, "out of memory");
for (i = 0; i < num_play; i++) {
over:
printf("Player %d's name: ", i + 1);
@@ -124,7 +126,10 @@
if (sp == buf)
goto over;
*sp++ = '\0';
- strcpy(name_list[i]=play[i].name=(char *)calloc(1,sp-buf),buf);
+ name_list[i] = play[i].name = (char *)calloc(1, sp - buf);
+ if (name_list[i] == NULL)
+ errx(1, "out of memory");
+ strcpy(play[i].name, buf);
play[i].money = 1500;
}
name_list[i++] = "done";
diff -r 147c95927da0 -r 10ad1ec55d73 games/monop/monop.h
--- a/games/monop/monop.h Thu Sep 09 17:12:03 1999 +0000
+++ b/games/monop/monop.h Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: monop.h,v 1.8 1999/09/08 21:17:52 jsm Exp $ */
+/* $NetBSD: monop.h,v 1.9 1999/09/09 17:27:59 jsm Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -35,6 +35,7 @@
* @(#)monop.h 8.1 (Berkeley) 5/31/93
*/
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff -r 147c95927da0 -r 10ad1ec55d73 games/monop/prop.c
--- a/games/monop/prop.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/monop/prop.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: prop.c,v 1.5 1999/08/21 10:40:04 simonb Exp $ */
+/* $NetBSD: prop.c,v 1.6 1999/09/09 17:27:59 jsm Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)prop.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: prop.c,v 1.5 1999/08/21 10:40:04 simonb Exp $");
+__RCSID("$NetBSD: prop.c,v 1.6 1999/09/09 17:27:59 jsm Exp $");
#endif
#endif /* not lint */
@@ -75,6 +75,8 @@
OWN *op;
op = (OWN *)calloc(1, sizeof (OWN));
+ if (op == NULL)
+ errx(1, "out of memory");
op->sqr = &board[op_sqr];
val = value(op->sqr);
last_tp = NULL;
diff -r 147c95927da0 -r 10ad1ec55d73 games/rogue/init.c
--- a/games/rogue/init.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/rogue/init.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: init.c,v 1.8 1999/09/08 21:45:30 jsm Exp $ */
+/* $NetBSD: init.c,v 1.9 1999/09/09 17:27:59 jsm Exp $ */
/*
* Copyright (c) 1988, 1993
@@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: init.c,v 1.8 1999/09/08 21:45:30 jsm Exp $");
+__RCSID("$NetBSD: init.c,v 1.9 1999/09/09 17:27:59 jsm Exp $");
#endif
#endif /* not lint */
@@ -332,6 +332,8 @@
}
}
*s = md_malloc(MAX_OPT_LEN + 2);
+ if (*s == NULL)
+ clean_up("out of memory");
(void) strncpy(*s, t, i);
if (add_blank) {
(*s)[i++] = ' ';
@@ -346,6 +348,8 @@
{
if (!(*str)) {
*str = md_malloc(MAX_OPT_LEN + 2);
+ if (*str == NULL)
+ clean_up("out of memory");
(void) strcpy(*str, dflt);
}
}
diff -r 147c95927da0 -r 10ad1ec55d73 games/snake/snscore/snscore.c
--- a/games/snake/snscore/snscore.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/snake/snscore/snscore.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: snscore.c,v 1.9 1999/09/08 21:18:00 jsm Exp $ */
+/* $NetBSD: snscore.c,v 1.10 1999/09/09 17:28:00 jsm Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -43,7 +43,7 @@
#if 0
static char sccsid[] = "@(#)snscore.c 8.1 (Berkeley) 7/19/93";
#else
-__RCSID("$NetBSD: snscore.c,v 1.9 1999/09/08 21:18:00 jsm Exp $");
+__RCSID("$NetBSD: snscore.c,v 1.10 1999/09/09 17:28:00 jsm Exp $");
#endif
#endif /* not lint */
@@ -98,8 +98,9 @@
if (p == NULL)
continue;
q = p -> pw_name;
- players[noplayers].name = malloc(strlen(q) + 1);
- strcpy(players[noplayers].name, q);
+ players[noplayers].name = strdup(q);
+ if (players[noplayers].name == NULL)
+ errx(1, "out of memory");
noplayers++;
}
}
diff -r 147c95927da0 -r 10ad1ec55d73 games/worm/worm.c
--- a/games/worm/worm.c Thu Sep 09 17:12:03 1999 +0000
+++ b/games/worm/worm.c Thu Sep 09 17:27:58 1999 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: worm.c,v 1.14 1999/09/08 21:45:34 jsm Exp $ */
+/* $NetBSD: worm.c,v 1.15 1999/09/09 17:28:00 jsm Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -43,7 +43,7 @@
#if 0
static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: worm.c,v 1.14 1999/09/08 21:45:34 jsm Exp $");
+__RCSID("$NetBSD: worm.c,v 1.15 1999/09/09 17:28:00 jsm Exp $");
#endif
#endif /* not lint */
@@ -54,6 +54,7 @@
#include <ctype.h>
#include <curses.h>
+#include <err.h>
#include <signal.h>
#include <stdlib.h>
#include <termios.h>
@@ -151,12 +152,16 @@
np = NULL;
head = newlink();
+ if (head == NULL)
+ errx(1, "out of memory");
head->x = start_len+2;
head->y = 12;
head->next = NULL;
display(head, HEAD);
for (i = 0, bp = head; i < start_len; i++, bp = np) {
np = newlink();
+ if (np == NULL)
+ errx(1, "out of memory");
np->next = bp;
bp->prev = np;
np->x = bp->x - 1;
@@ -277,6 +282,8 @@
}
else if(ch != ' ') crash();
nh = newlink();
+ if (nh == NULL)
Home |
Main Index |
Thread Index |
Old Index