Subject: bin/6136: [PATCH] Adventure should check for malloc failure
To: None <gnats-bugs@gnats.netbsd.org>
From: Joseph Myers <jsm@octomino.demon.co.uk>
List: netbsd-bugs
Date: 09/10/1998 13:02:17
>Number:         6136
>Category:       bin
>Synopsis:       [PATCH] Adventure should check for malloc failure
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Sep 10 10:05:00 1998
>Last-Modified:
>Originator:     Joseph Samuel Myers
>Organization:
Trinity College, University of Cambridge, UK
>Release:        NetBSD-current of 1998-08-30
>Environment:
[
System: Linux octomino 2.0.35 #1 Wed Aug 12 15:54:21 UTC 1998 i586 unknown
Architecture: i586
]
>Description:

In a few places, the game adventure uses malloc() but does not check
whether it succeeded.  The patch below fixes this.  In one place,
malloc() failure is checked for but reported to the user as a bug
rather than a low memory condition; this patch fixes that place also.

>How-To-Repeat:

Play adventure in sufficiently low memory conditions.

>Fix:

diff -ruN adventure/io.c adventure+/io.c
--- adventure/io.c	Sun Aug 30 11:05:04 1998
+++ adventure+/io.c	Thu Sep 10 12:50:39 1998
@@ -49,6 +49,7 @@
 
 /*      Re-coding of advent in C: file i/o and user i/o                 */
 
+#include <err.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -355,6 +356,8 @@
 			return;
 		if (locc != oldloc) {	/* getting a new entry         */
 			t = travel[locc] = (struct travlist *) malloc(sizeof(struct travlist));
+			if (t == NULL)
+				errx(1, "Out of memory!");
 			/* printf("New travel list for %d\n",locc);        */
 			entries = 0;
 			oldloc = locc;
@@ -374,8 +377,11 @@
 			m = atoi(buf);
 		}
 		while (breakch != LF) {	/* only do one line at a time   */
-			if (entries++)
+			if (entries++) {
 				t = t->next = (struct travlist *) malloc(sizeof(struct travlist));
+				if (t == NULL)
+					errx(1, "Out of memory!");
+			}
 			t->tverb = rnum();	/* get verb from the file       */
 			t->tloc = n;	/* table entry mod 1000         */
 			t->conditions = m;	/* table entry / 1000           */
@@ -549,7 +555,7 @@
 
 	msg = &ptext[m];
 	if ((tbuf = (char *) malloc(msg->txtlen + 1)) == 0)
-		bug(108);
+		errx(1, "Out of memory!");
 	memcpy(tbuf, msg->seekadr, msg->txtlen + 1);	/* Room to null */
 	s = tbuf;
 
diff -ruN adventure/vocab.c adventure+/vocab.c
--- adventure/vocab.c	Tue Aug 25 11:05:17 1998
+++ adventure+/vocab.c	Thu Sep 10 12:50:21 1998
@@ -49,6 +49,7 @@
 
 /*      Re-coding of advent in C: data structure routines               */
 
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "hdr.h"
@@ -162,6 +163,8 @@
 				goto exitloop2;
 			h->val = value;
 			h->atab = malloc(length(word));
+			if (h->atab == NULL)
+				errx(1, "Out of memory!");
 			for (s = word, t = h->atab; *s;)
 				*t++ = *s++ ^ '=';
 			*t = 0 ^ '=';
>Audit-Trail:
>Unformatted: