diff -urNw clamav-devel.jm/clamav-milter/clamav-milter.c clamav-devel.jm-pl2/clamav-milter/clamav-milter.c
--- clamav-devel.jm/clamav-milter/clamav-milter.c	Thu Apr 29 00:18:05 2004
+++ clamav-devel.jm-pl2/clamav-milter/clamav-milter.c	Thu Apr 29 00:17:18 2004
@@ -684,6 +684,27 @@
 typedef	unsigned short	in_port_t;
 #endif
 
+#define ALLOC_CHECK(_function_,_pointer_,_cleanup_)	{\
+	int save_errno = errno;				\
+	if(!_pointer_){					\
+		fprintf(stderr,"%s: %s() line %d, %s: ",\
+			__FILE__,			\
+			__func__,			\
+			__LINE__,			\
+			_function_ );			\
+		errno = save_errno;			\
+		perror("");				\
+		if(!_cleanup_)				\
+			exit(save_errno);		\
+		else {					\
+			ret_code = save_errno;		\
+			if(!ret_code)			\
+				ret_code = 1;		\
+			goto cleanup;			\
+		}					\
+	}						\
+}
+
 /*
  * TODO: optional: xmessage on console when virus stopped (SNMP would be real nice!)
  *	Having said that, with LogSysLog you can (on Linux) configure the system
@@ -900,6 +921,7 @@
 int
 main(int argc, char **argv)
 {
+	int	ret_code = 0;
 	extern char *optarg;
 	const char *cfgfile = CL_DEFAULT_CFG;
 	struct cfgstruct *cpt;
@@ -1272,6 +1294,8 @@
 		umask(077);
 
 		serverIPs = (long *)cli_malloc(sizeof(long));
+		ALLOC_CHECK("cli_malloc",serverIPs,0);
+			
 		serverIPs[0] = inet_addr("127.0.0.1");
 	} else if((cpt = cfgopt(copt, "TCPSocket")) != NULL) {
 		int i, activeServers;
@@ -1302,6 +1326,7 @@
 		cli_dbgmsg("numServers: %d\n", numServers);
 
 		serverIPs = (long *)cli_malloc(numServers * sizeof(long));
+		ALLOC_CHECK("cli_malloc",serverIPs,0);
 		activeServers = 0;
 
 		for(i = 0; i < numServers; i++) {
@@ -1368,17 +1393,19 @@
 		if((open("/dev/console", O_WRONLY) == 1) ||
 		   (open("/dev/null", O_WRONLY) == 1))
 			dup(1);
+
 #ifdef HAVE_SETPGRP
 #ifdef SETPGRP_VOID
 		setpgrp();
-#else
+#else /* SETPGRP_VOID */
 		setpgrp(0,0);
-#endif
-#else
+#endif /* SETPGRP_VOID */
+#else  /* HAVE_SETPGRP */
 #ifdef HAVE_SETSID
 		 setsid();
-#endif
-#endif
+#endif /* HAVE_SETSID */
+#endif /* HAVE_SETPGRP */
+#endif /* CL_DEBUG */
 	}
 
 	if((cpt = cfgopt(copt, "PidFile")) != NULL)
@@ -1452,6 +1479,8 @@
 	signal(SIGPIPE, SIG_IGN);
 
 	return smfi_main();
+cleanup:
+	return(ret_code);
 }
 
 /*
@@ -1574,6 +1603,7 @@
 	fd_set rfds;
 	struct timeval tv;
 	int retval;
+	int ret_code = 0;
 
 	assert(tcpSocket != 0);
 	assert(numServers > 0);
@@ -1582,7 +1612,9 @@
 		return 0;
 
 	servers = (struct sockaddr_in *)cli_calloc(numServers, sizeof(struct sockaddr_in));
+	ALLOC_CHECK("cli_calloc",servers,1);
 	socks = (int *)cli_malloc(numServers * sizeof(int));
+	ALLOC_CHECK("cli_malloc",socks,1);
 
 	FD_ZERO(&rfds);
 
@@ -1662,7 +1694,15 @@
 	cli_dbgmsg("findServer: No response from any server\n");
 	if(use_syslog)
 		syslog(LOG_WARNING, "findServer: No response from any server");
+cleanup:
+	if(ret_code){
+		if(servers)
+			free(servers);
+		if(socks)
+			free(socks);
+	}
 	return 0;
+	
 }
 
 /*
@@ -1816,6 +1856,7 @@
 clamfi_envfrom(SMFICTX *ctx, char **argv)
 {
 	struct privdata *privdata;
+	int	ret_code = 0;
 
 	if(logVerbose)
 		syslog(LOG_DEBUG, "clamfi_envfrom: %s", argv[0]);
@@ -1874,9 +1915,7 @@
 	}
 
 	privdata = (struct privdata *)cli_calloc(1, sizeof(struct privdata));
-	if(privdata == NULL)
-		return cl_error;
-
+ 	ALLOC_CHECK("cli_calloc",privdata,1);
 	privdata->dataSocket = -1;	/* 0.4 */
 	privdata->cmdSocket = -1;	/* 0.4 */
 
@@ -1893,13 +1932,13 @@
 		privdata->envid = "Uknown";
 
 	if(hflag)
-		privdata->headers = header_list_new();
+		if(!(privdata->headers = header_list_new()))
+			goto cleanup;
 
 	if(smfi_setpriv(ctx, privdata) == MI_SUCCESS)
 		return SMFIS_CONTINUE;
-
+cleanup:
 	clamfi_free(privdata);
-
 	return cl_error;
 }
 
@@ -1907,19 +1946,26 @@
 clamfi_envrcpt(SMFICTX *ctx, char **argv)
 {
 	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
+	int ret_code = 0;
+	void * p = NULL;
 
 	if(logVerbose)
 		syslog(LOG_DEBUG, "clamfi_envrcpt: %s", argv[0]);
-
 	cli_dbgmsg("clamfi_envrcpt: %s\n", argv[0]);
 
+ 	if(!privdata)
+ 		return cl_error;
+
 	if(privdata->to == NULL) {
 		privdata->to = cli_malloc(sizeof(char *) * 2);
+		ALLOC_CHECK("cli_malloc",privdata->to,1);
 
 		assert(privdata->numTo == 0);
-	} else
-		privdata->to = cli_realloc(privdata->to, sizeof(char *) * (privdata->numTo + 2));
-
+	} else {
+		p = cli_realloc(privdata->to, (sizeof(char *) * (privdata->numTo + 2)));
+		ALLOC_CHECK("cli_realloc",p,1);
+		privdata->to = p;
+	}
 	if(privdata->to == NULL)
 		return cl_error;
 
@@ -1927,6 +1973,8 @@
 	privdata->to[++privdata->numTo] = NULL;
 
 	return SMFIS_CONTINUE;
+cleanup:
+	return cl_error;
 }
 
 static sfsistat
@@ -2062,12 +2110,16 @@
 clamfi_body(SMFICTX *ctx, u_char *bodyp, size_t len)
 {
 	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
+	void * p = NULL;
+	int ret_code = 0;
 
 	if(logVerbose)
 		syslog(LOG_DEBUG, "clamfi_envbody: %u bytes", len);
 #ifdef	CL_DEBUG
 	cli_dbgmsg("clamfi_envbody: %u bytes\n", len);
 #endif
+	if(!privdata)
+		return cl_error;
 
 	if(clamfi_send(privdata, len, (char *)bodyp) < 0) {
 		clamfi_cleanup(ctx);	/* not needed, but just to be safe */
@@ -2076,17 +2128,22 @@
 	if(Sflag) {
 		if(privdata->body) {
 			assert(privdata->bodyLen > 0);
-			privdata->body = cli_realloc(privdata->body, privdata->bodyLen + len);
+			p = cli_realloc(privdata->body, privdata->bodyLen + len);
+			ALLOC_CHECK("cli_realloc",p,1);
+			privdata->body = p;
 			memcpy(&privdata->body[privdata->bodyLen], bodyp, len);
 			privdata->bodyLen += len;
 		} else {
 			assert(privdata->bodyLen == 0);
 			privdata->body = cli_malloc(len);
+ 			ALLOC_CHECK("cli_malloc",privdata->body,1);
 			memcpy(privdata->body, bodyp, len);
 			privdata->bodyLen = len;
 		}
 	}
 	return SMFIS_CONTINUE;
+cleanup:
+	return cl_error;
 }
 
 static sfsistat
@@ -2096,9 +2153,13 @@
 	char *ptr;
 	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
 	char mess[128];
+	int ret_code = 0;
+	void * p = NULL;
+	char **to, *err = NULL;
 
 	if(logVerbose)
 		syslog(LOG_DEBUG, "clamfi_eom");
+
 #ifdef	CL_DEBUG
 	cli_dbgmsg("clamfi_eom\n");
 	assert(privdata != NULL);
@@ -2106,6 +2167,8 @@
 	assert(!((privdata->cmdSocket >= 0) && (privdata->filename != NULL)));
 	assert(privdata->dataSocket >= 0);
 #endif
+	if(!privdata)
+		goto cleanup;
 
 	close(privdata->dataSocket);
 	privdata->dataSocket = -1;
@@ -2197,6 +2260,7 @@
 	}
 
 	if((ptr = strstr(mess, "FOUND")) == NULL) {
+		rc = SMFIS_ACCEPT;
 		if(!nflag)
 			smfi_addheader(ctx, "X-Virus-Status", "Clean");
 
@@ -2217,18 +2281,21 @@
 			off_t len = updateSigFile();
 
 			if(len) {
+				p = NULL;
+
 				assert(Sflag != 0);
 
-				privdata->body = cli_realloc(privdata->body, privdata->bodyLen + len);
-				if(privdata->body) {
+				p = cli_realloc(privdata->body, privdata->bodyLen + len);
+				ALLOC_CHECK("cli_realloc",p,1);
+				privdata->body = p;
 					memcpy(&privdata->body[privdata->bodyLen], signature, len);
 					smfi_replacebody(ctx, privdata->body, privdata->bodyLen + len);
-				}
+				
 			}
 		}
 	} else {
 		char reject[1024];
-		char **to, *virusname;
+		char *virusname;
 
 		*ptr = '\0';	/* Remove the "FOUND" word */
 
@@ -2242,16 +2309,12 @@
 			smfi_addheader(ctx, "X-Virus-Status", "Infected");
 
 		if(use_syslog) {
+			int i;
 			/*
 			 * Setup err as a list of recipients
 			 */
-			char *err = (char *)cli_malloc(1024);
-			int i;
-
-			if(err == NULL) {
-				clamfi_cleanup(ctx);
-				return cl_error;
-			}
+			err = (char *)cli_malloc(1024);
+			ALLOC_CHECK("cli_malloc",err,1);
 
 			/*
 			 * Use snprintf rather than printf since we don't know the
@@ -2270,11 +2333,10 @@
 				 */
 				if(&ptr[strlen(*to) + 2] >= &err[i]) {
 					i += 1024;
-					err = cli_realloc(err, i);
-					if(err == NULL) {
-						clamfi_cleanup(ctx);
-						return cl_error;
-					}
+					p = NULL;
+					p = cli_realloc(err, i);
+					ALLOC_CHECK("cli_realloc",p,1);
+					err = ptr;
 					ptr = strchr(err, '\0');
 				}
 				ptr = strrcpy(ptr, " ");
@@ -2443,8 +2505,12 @@
 		snprintf(reject, sizeof(reject) - 1, "%sdetected by ClamAV - http://www.clamav.net", virusname);
 		smfi_setreply(ctx, (char *)privdata->rejectCode, "5.7.1", reject);
 	}
+cleanup:
 	clamfi_cleanup(ctx);
-
+	if(ret_code){
+		if(err)
+			free(err);
+	}
 	return rc;
 }
 
@@ -2738,6 +2804,8 @@
 {
 	struct stat statb;
 	int fd;
+	int ret_code = 0;
+	void * p = NULL;
 
 	if(sigFilename == NULL)
 		/* nothing to read */
@@ -2762,25 +2830,32 @@
 	}
 
 	signatureStamp = statb.st_mtime;
+	p = cli_realloc(signature, statb.st_size + 1);
+	ALLOC_CHECK("cli_realloc",p,1);
+	signature = p;
 
-	signature = cli_realloc(signature, statb.st_size);
 	read(fd, signature, statb.st_size);
 	close(fd);
 
 	return statb.st_size;
+cleanup:
+	return strlen(signature);
 }
 
 static header_list_t
 header_list_new(void)
 {
+	int ret_code = 0;
+	void * p = NULL;
 	header_list_t ret;
 
 	ret = (header_list_t)cli_malloc(sizeof(struct header_list_struct));
-	if(ret) {
+	ALLOC_CHECK("cli_malloc",ret,1);
 		ret->first = NULL;
 		ret->last = NULL;
-	}
 	return ret;
+cleanup:
+	return NULL;
 }
 
 static void
@@ -2804,19 +2879,16 @@
 	char *header;
 	size_t len;
 	struct header_node_t *new_node;
+	int ret_code = 0;
 
 	len = strlen(headerf) + strlen(headerv) + 3;
 
 	header = (char *)cli_malloc(len);
-	if(header == NULL)
-		return;
+	ALLOC_CHECK("cli_malloc",header,1);
 
 	sprintf(header, "%s: %s", headerf, headerv);
 	new_node = (struct header_node_t *)cli_malloc(sizeof(struct header_node_t));
-	if(new_node == NULL) {
-		free(header);
-		return;
-	}
+	ALLOC_CHECK("cli_malloc",new_node,1);
 	new_node->header = header;
 	new_node->next = NULL;
 	if(!list->first)
@@ -2825,6 +2897,13 @@
 		list->last->next = new_node;
 
 	list->last = new_node;
+cleanup:
+	if(ret_code){
+		if(header)
+			free(header);
+		if(new_node)
+			free(new_node);
+	}	
 }
 
 static void
@@ -2846,6 +2925,9 @@
 static int
 connect2clamd(struct privdata *privdata)
 {
+	int  ret = 0;
+	int	ret_code = 0;
+
 	char **to;
 
 	assert(privdata != NULL);
@@ -2882,7 +2964,7 @@
 		DD = tm->tm_mday;
 
 		privdata->filename = (char *)cli_malloc(strlen(quarantine_dir) + 19);
-
+		ALLOC_CHECK("cli_malloc",privdata->filename,1);
 		sprintf(privdata->filename, "%s/%02d%02d%02d", quarantine_dir,
 			YY, MM, DD);
 
@@ -3053,6 +3135,8 @@
 	cli_dbgmsg("connect2clamd OK\n");
 
 	return 1;
+cleanup:
+	return 0;
 }
 
 /*
