Subject: bin/1737: make ignores malloc return value
To: None <gnats-bugs@gnats.netbsd.org>
From: Gordon W. Ross <gwr@mc.com>
List: netbsd-bugs
Date: 11/07/1995 17:04:19
>Number:         1737
>Category:       bin
>Synopsis:       make ignores malloc return value
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Nov  7 17:20:01 1995
>Last-Modified:
>Originator:     Gordon W. Ross
>Organization:
NetBSD
>Release:        NetBSD-current, 6 Nov. 1995
>Environment:
	<machine, os, target, libraries (multiple lines)>
System: NetBSD venus 1.0A NetBSD 1.0A (GENERIC) #43: Thu Sep 28 14:41:22 EDT 1995 gwr@venus:/play/gwr/netbsd/work/src/sys/arch/sun3/compile/GENERIC sun3


>Description:
	Run make in /usr/src/lib/libc with a limited amount of swap.
	When malloc returns null, make will coredump.
	("Any program that dumps core is broken." 8^)

>How-To-Repeat:
	First you have to arrange for malloc to return null.
	This is not trivial, so I'd suggest a contrivance to
	force malloc to return null where it counts (use gdb).
>Fix:
	
Here are the three files affected:
	lst.lib/lstInt.h  bit.h  util.c
[ found using: grep malloc lst.lib/*.[ch] *.[ch] |grep -v emalloc ]

Here are the changes I suggest:

% cd /usr/src/usr.bin/
% diff -rc make make.fixed
diff -rc make/bit.h make.fixed/bit.h
*** make/bit.h	Fri Oct 13 23:16:43 1995
--- make.fixed/bit.h	Tue Nov  7 16:49:02 1995
***************
*** 64,70 ****
  	(Bit_NumInts(numBits) * sizeof(int))
  
  #define Bit_Alloc(numBits, bitArrayPtr)  	\
!         bitArrayPtr = (int *)malloc((unsigned)Bit_NumBytes(numBits)); \
          Bit_Zero((numBits), (bitArrayPtr))
  
  #define Bit_Free(bitArrayPtr)	\
--- 64,70 ----
  	(Bit_NumInts(numBits) * sizeof(int))
  
  #define Bit_Alloc(numBits, bitArrayPtr)  	\
!         bitArrayPtr = (int *)emalloc((unsigned)Bit_NumBytes(numBits)); \
          Bit_Zero((numBits), (bitArrayPtr))
  
  #define Bit_Free(bitArrayPtr)	\
diff -rc make/util.c make.fixed/util.c
*** make/util.c	Fri Oct 13 23:17:10 1995
--- make.fixed/util.c	Tue Nov  7 16:51:10 1995
***************
*** 49,55 ****
  {
      register char *p;
      int len = strlen(name) + strlen(value) + 2; /* = \0 */
!     char *ptr = (char*) malloc(len);
  
      (void) dum;
  
--- 49,55 ----
  {
      register char *p;
      int len = strlen(name) + strlen(value) + 2; /* = \0 */
!     char *ptr = (char*) emalloc(len);
  
      (void) dum;
  
diff -rc make/lst.lib/lstInt.h make.fixed/lst.lib/lstInt.h
*** make/lst.lib/lstInt.h	Fri Oct 13 23:17:35 1995
--- make.fixed/lst.lib/lstInt.h	Tue Nov  7 16:48:33 1995
***************
*** 89,95 ****
   * PAlloc (var, ptype) --
   *	Allocate a pointer-typedef structure 'ptype' into the variable 'var'
   */
! #define	PAlloc(var,ptype)	var = (ptype) malloc (sizeof (*var))
  
  /*
   * LstValid (l) --
--- 89,96 ----
   * PAlloc (var, ptype) --
   *	Allocate a pointer-typedef structure 'ptype' into the variable 'var'
   */
! char *emalloc __P((size_t));
! #define	PAlloc(var,ptype)	var = (ptype) emalloc (sizeof (*var))
  
  /*
   * LstValid (l) --

>Audit-Trail:
>Unformatted: