Module Name:    src
Committed By:   agc
Date:           Wed Oct  7 04:56:51 UTC 2009

Modified Files:
        src/crypto/external/bsd/netpgp/dist/src/lib: compress.c keyring.c
            reader.c

Log Message:
More checks for the return value from memory allocation.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 \
    src/crypto/external/bsd/netpgp/dist/src/lib/compress.c
cvs rdiff -u -r1.21 -r1.22 \
    src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c
cvs rdiff -u -r1.23 -r1.24 \
    src/crypto/external/bsd/netpgp/dist/src/lib/reader.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/crypto/external/bsd/netpgp/dist/src/lib/compress.c
diff -u src/crypto/external/bsd/netpgp/dist/src/lib/compress.c:1.13 src/crypto/external/bsd/netpgp/dist/src/lib/compress.c:1.14
--- src/crypto/external/bsd/netpgp/dist/src/lib/compress.c:1.13	Sun Oct  4 21:58:25 2009
+++ src/crypto/external/bsd/netpgp/dist/src/lib/compress.c	Wed Oct  7 04:56:51 2009
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: compress.c,v 1.13 2009/10/04 21:58:25 agc Exp $");
+__RCSID("$NetBSD: compress.c,v 1.14 2009/10/07 04:56:51 agc Exp $");
 #endif
 
 #ifdef HAVE_ZLIB_H
@@ -416,14 +416,20 @@
 		     const unsigned int len,
 		     __ops_output_t *out)
 {
-	compress_t	*zip = calloc(1, sizeof(compress_t));
+	compress_t	*zip;
 	size_t		 sz_in;
 	size_t		 sz_out;
+	int              ret;
 	int              r = 0;
 
 	/* compress the data */
 	const int       level = Z_DEFAULT_COMPRESSION;	/* \todo allow varying
 							 * levels */
+
+	if ((zip = calloc(1, sizeof(*zip))) == NULL) {
+		(void) fprintf(stderr, "__ops_writez: bad alloc\n");
+		return 0;
+	}
 	zip->stream.zalloc = Z_NULL;
 	zip->stream.zfree = Z_NULL;
 	zip->stream.opaque = NULL;
@@ -443,8 +449,17 @@
 
 	sz_in = len * sizeof(unsigned char);
 	sz_out = ((101 * sz_in) / 100) + 12;	/* from zlib webpage */
-	zip->src = calloc(1, sz_in);
-	zip->dst = calloc(1, sz_out);
+	if ((zip->src = calloc(1, sz_in)) == NULL) {
+		free(zip);
+		(void) fprintf(stderr, "__ops_writez: bad alloc2\n");
+		return 0;
+	}
+	if ((zip->dst = calloc(1, sz_out)) == NULL) {
+		free(zip->src);
+		free(zip);
+		(void) fprintf(stderr, "__ops_writez: bad alloc3\n");
+		return 0;
+	}
 	(void) memcpy(zip->src, data, len);
 
 	/* setup stream */
@@ -461,8 +476,13 @@
 	} while (r != Z_STREAM_END);
 
 	/* write it out */
-	return (__ops_write_ptag(out, OPS_PTAG_CT_COMPRESSED) &&
+	ret = __ops_write_ptag(out, OPS_PTAG_CT_COMPRESSED) &&
 		__ops_write_length(out, (unsigned)(zip->stream.total_out + 1))&&
 		__ops_write_scalar(out, OPS_C_ZLIB, 1) &&
-		__ops_write(out, zip->dst, (unsigned)zip->stream.total_out));
+		__ops_write(out, zip->dst, (unsigned)zip->stream.total_out);
+
+	free(zip->src);
+	free(zip->dst);
+	free(zip);
+	return ret;
 }

Index: src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c
diff -u src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c:1.21 src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c:1.22
--- src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c:1.21	Tue Oct  6 02:26:05 2009
+++ src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c	Wed Oct  7 04:56:51 2009
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: keyring.c,v 1.21 2009/10/06 02:26:05 agc Exp $");
+__RCSID("$NetBSD: keyring.c,v 1.22 2009/10/07 04:56:51 agc Exp $");
 #endif
 
 #ifdef HAVE_FCNTL_H
@@ -267,7 +267,10 @@
 		return OPS_FINISHED;
 
 	case OPS_PTAG_CT_SECRET_KEY:
-		decrypt->seckey = calloc(1, sizeof(*decrypt->seckey));
+		if ((decrypt->seckey = calloc(1, sizeof(*decrypt->seckey))) == NULL) {
+			(void) fprintf(stderr, "decrypt_cb: bad alloc\n");
+			return OPS_FINISHED;
+		}
 		decrypt->seckey->checkhash = calloc(1, OPS_CHECKHASH_SIZE);
 		*decrypt->seckey = content->seckey;
 		return OPS_KEEP_MEMORY;
@@ -396,8 +399,11 @@
 	if (dst->userid) {
 		free(dst->userid);
 	}
-	dst->userid = calloc(1, len + 1);
-	(void) memcpy(dst->userid, src->userid, len);
+	if ((dst->userid = calloc(1, len + 1)) == NULL) {
+		(void) fprintf(stderr, "__ops_copy_userid: bad alloc\n");
+	} else {
+		(void) memcpy(dst->userid, src->userid, len);
+	}
 	return dst;
 }
 
@@ -415,9 +421,12 @@
 	if (dst->raw) {
 		free(dst->raw);
 	}
-	dst->raw = calloc(1, src->length);
-	dst->length = src->length;
-	(void) memcpy(dst->raw, src->raw, src->length);
+	if ((dst->raw = calloc(1, src->length)) == NULL) {
+		(void) fprintf(stderr, "__ops_copy_packet: bad alloc\n");
+	} else {
+		dst->length = src->length;
+		(void) memcpy(dst->raw, src->raw, src->length);
+	}
 	return dst;
 }
 

Index: src/crypto/external/bsd/netpgp/dist/src/lib/reader.c
diff -u src/crypto/external/bsd/netpgp/dist/src/lib/reader.c:1.23 src/crypto/external/bsd/netpgp/dist/src/lib/reader.c:1.24
--- src/crypto/external/bsd/netpgp/dist/src/lib/reader.c:1.23	Wed Oct  7 04:18:47 2009
+++ src/crypto/external/bsd/netpgp/dist/src/lib/reader.c	Wed Oct  7 04:56:51 2009
@@ -54,7 +54,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: reader.c,v 1.23 2009/10/07 04:18:47 agc Exp $");
+__RCSID("$NetBSD: reader.c,v 1.24 2009/10/07 04:56:51 agc Exp $");
 #endif
 
 #include <sys/types.h>
@@ -186,17 +186,21 @@
 		__ops_reader_destroyer_t *destroyer,
 		void *vp)
 {
-	__ops_reader_t *readinfo = calloc(1, sizeof(*readinfo));
+	__ops_reader_t *readinfo;
 
-	*readinfo = stream->readinfo;
-	(void) memset(&stream->readinfo, 0x0, sizeof(stream->readinfo));
-	stream->readinfo.next = readinfo;
-	stream->readinfo.parent = stream;
+	if ((readinfo = calloc(1, sizeof(*readinfo))) == NULL) {
+		(void) fprintf(stderr, "__ops_reader_push: bad alloc\n");
+	} else {
+		*readinfo = stream->readinfo;
+		(void) memset(&stream->readinfo, 0x0, sizeof(stream->readinfo));
+		stream->readinfo.next = readinfo;
+		stream->readinfo.parent = stream;
 
-	/* should copy accumulate flags from other reader? RW */
-	stream->readinfo.accumulate = readinfo->accumulate;
+		/* should copy accumulate flags from other reader? RW */
+		stream->readinfo.accumulate = readinfo->accumulate;
 
-	__ops_reader_set(stream, reader, destroyer, vp);
+		__ops_reader_set(stream, reader, destroyer, vp);
+	}
 }
 
 /**
@@ -297,8 +301,9 @@
 
 	if (dearmour->pushback) {
 		(void) fprintf(stderr, "push_back: already pushed back\n");
+	} else if ((dearmour->pushback = calloc(1, length)) == NULL) {
+		(void) fprintf(stderr, "push_back: bad alloc\n");
 	} else {
-		dearmour->pushback = calloc(1, length);
 		for (n = 0; n < length; ++n) {
 			dearmour->pushback[n] = buf[(length - n) - 1];
 		}
@@ -532,11 +537,14 @@
 {
 	unsigned        n;
 
-	dest->headers = calloc(src->headerc, sizeof(*dest->headers));
-	dest->headerc = src->headerc;
-	for (n = 0; n < src->headerc; ++n) {
-		dest->headers[n].key = strdup(src->headers[n].key);
-		dest->headers[n].value = strdup(src->headers[n].value);
+	if ((dest->headers = calloc(src->headerc, sizeof(*dest->headers))) == NULL) {
+		(void) fprintf(stderr, "__ops_dup_headers: bad alloc\n");
+	} else {
+		dest->headerc = src->headerc;
+		for (n = 0; n < src->headerc; ++n) {
+			dest->headers[n].key = strdup(src->headers[n].key);
+			dest->headers[n].value = strdup(src->headers[n].value);
+		}
 	}
 }
 
@@ -558,7 +566,11 @@
 	__ops_hash_t     *hash;
 	int             total;
 
-	hash = calloc(1, sizeof(*hash));
+	if ((hash = calloc(1, sizeof(*hash))) == NULL) {
+		OPS_ERROR(errors, OPS_E_R_BAD_FORMAT,
+			"process_dash_escaped: bad alloc");
+		return -1;
+	}
 	hashstr = __ops_find_header(&dearmour->headers, "Hash");
 	if (hashstr) {
 		__ops_hash_alg_t alg;
@@ -1012,13 +1024,15 @@
 		     __ops_reader_t *readinfo,
 		     __ops_cbdata_t *cbinfo)
 {
-	dearmour_t *dearmour = __ops_reader_get_arg(readinfo);
-	__ops_packet_t content;
-	int             ret;
-	unsigned   first;
-	unsigned char  *dest = dest_;
-	int             saved = length;
+	__ops_packet_t	 content;
+	unsigned char	*dest = dest_;
+	dearmour_t	*dearmour;
+	unsigned	 first;
+	int		 saved;
+	int              ret;
 
+	dearmour = __ops_reader_get_arg(readinfo);
+	saved = length;
 	if (dearmour->eof64 && !dearmour->buffered) {
 		if (dearmour->state != OUTSIDE_BLOCK &&
 		    dearmour->state != AT_TRAILER_NAME) {
@@ -1313,18 +1327,21 @@
 {
 	dearmour_t *dearmour;
 
-	dearmour = calloc(1, sizeof(*dearmour));
-	dearmour->seen_nl = 1;
-	/*
-	    dearmour->allow_headers_without_gap=without_gap;
-	    dearmour->allow_no_gap=no_gap;
-	    dearmour->allow_trailing_whitespace=trailing_whitespace;
-	*/
-	dearmour->expect_sig = 0;
-	dearmour->got_sig = 0;
+	if ((dearmour = calloc(1, sizeof(*dearmour))) == NULL) {
+		(void) fprintf(stderr, "__ops_reader_push_dearmour: bad alloc\n");
+	} else {
+		dearmour->seen_nl = 1;
+		/*
+		    dearmour->allow_headers_without_gap=without_gap;
+		    dearmour->allow_no_gap=no_gap;
+		    dearmour->allow_trailing_whitespace=trailing_whitespace;
+		*/
+		dearmour->expect_sig = 0;
+		dearmour->got_sig = 0;
 
-	__ops_reader_push(parse_info, armoured_data_reader,
+		__ops_reader_push(parse_info, armoured_data_reader,
 			armoured_data_destroyer, dearmour);
+	}
 }
 
 /**
@@ -1497,12 +1514,15 @@
 {
 	encrypted_t	*encrypted;
 	
-	encrypted = calloc(1, sizeof(*encrypted));
-	encrypted->decrypt = decrypt;
-	encrypted->region = region;
-	__ops_decrypt_init(encrypted->decrypt);
-	__ops_reader_push(stream, encrypted_data_reader,
+	if ((encrypted = calloc(1, sizeof(*encrypted))) == NULL) {
+		(void) fprintf(stderr, "__ops_reader_push_decrypted: bad alloc\n");
+	} else {
+		encrypted->decrypt = decrypt;
+		encrypted->region = region;
+		__ops_decrypt_init(encrypted->decrypt);
+		__ops_reader_push(stream, encrypted_data_reader,
 			encrypted_data_destroyer, encrypted);
+	}
 }
 
 /**
@@ -1572,7 +1592,10 @@
 		__ops_init_subregion(&decrypted_region, NULL);
 		decrypted_region.length =
 			se_ip->region->length - se_ip->region->readc;
-		buf = calloc(1, decrypted_region.length);
+		if ((buf = calloc(1, decrypted_region.length)) == NULL) {
+			(void) fprintf(stderr, "se_ip_data_reader: bad alloc\n");
+			return -1;
+		}
 
 		/* read entire SE IP packet */
 		if (!__ops_stacked_limited_read(buf, decrypted_region.length,
@@ -1590,8 +1613,7 @@
 				if (!((i + 1) % 8))
 					fprintf(stderr, "\n");
 			}
-			fprintf(stderr, "\n");
-			fprintf(stderr, "\n");
+			fprintf(stderr, "\n\n");
 		}
 		/* verify leading preamble */
 
@@ -1655,7 +1677,11 @@
 				"se_ip_data_reader: bad plaintext\n");
 			return 0;
 		}
-		se_ip->plaintext = calloc(1, sz_plaintext);
+		if ((se_ip->plaintext = calloc(1, sz_plaintext)) == NULL) {
+			(void) fprintf(stderr,
+				"se_ip_data_reader: bad alloc\n");
+			return 0;
+		}
 		memcpy(se_ip->plaintext, plaintext, sz_plaintext);
 		se_ip->plaintext_available = sz_plaintext;
 
@@ -1693,12 +1719,16 @@
 __ops_reader_push_se_ip_data(__ops_stream_t *stream, __ops_crypt_t *decrypt,
 			   __ops_region_t * region)
 {
-	decrypt_se_ip_t *se_ip = calloc(1, sizeof(*se_ip));
+	decrypt_se_ip_t *se_ip;
 
-	se_ip->region = region;
-	se_ip->decrypt = decrypt;
-	__ops_reader_push(stream, se_ip_data_reader, se_ip_data_destroyer,
+	if ((se_ip = calloc(1, sizeof(*se_ip))) == NULL) {
+		(void) fprintf(stderr, "__ops_reader_push_se_ip_data: bad alloc\n");
+	} else {
+		se_ip->region = region;
+		se_ip->decrypt = decrypt;
+		__ops_reader_push(stream, se_ip_data_reader, se_ip_data_destroyer,
 				se_ip);
+	}
 }
 
 /**
@@ -1776,10 +1806,14 @@
 void 
 __ops_reader_set_fd(__ops_stream_t *stream, int fd)
 {
-	mmap_reader_t *reader = calloc(1, sizeof(*reader));
+	mmap_reader_t *reader;
 
-	reader->fd = fd;
-	__ops_reader_set(stream, fd_reader, reader_fd_destroyer, reader);
+	if ((reader = calloc(1, sizeof(*reader))) == NULL) {
+		(void) fprintf(stderr, "__ops_reader_set_fd: bad alloc\n");
+	} else {
+		reader->fd = fd;
+		__ops_reader_set(stream, fd_reader, reader_fd_destroyer, reader);
+	}
 }
 
 /**************************************************************************/
@@ -1829,12 +1863,16 @@
 __ops_reader_set_memory(__ops_stream_t *stream, const void *buffer,
 		      size_t length)
 {
-	reader_mem_t *mem = calloc(1, sizeof(*mem));
+	reader_mem_t *mem;
 
-	mem->buffer = buffer;
-	mem->length = length;
-	mem->offset = 0;
-	__ops_reader_set(stream, mem_reader, mem_destroyer, mem);
+	if ((mem = calloc(1, sizeof(*mem))) == NULL) {
+		(void) fprintf(stderr, "__ops_reader_set_memory: bad alloc\n");
+	} else {
+		mem->buffer = buffer;
+		mem->length = length;
+		mem->offset = 0;
+		__ops_reader_set(stream, mem_reader, mem_destroyer, mem);
+	}
 }
 
 /**************************************************************************/
@@ -2317,10 +2355,14 @@
 void 
 __ops_reader_set_mmap(__ops_stream_t *stream, int fd)
 {
-	mmap_reader_t	*mem = calloc(1, sizeof(*mem));
+	mmap_reader_t	*mem;
 	struct stat	 st;
 
-	if (fstat(fd, &st) == 0) {
+	if (fstat(fd, &st) != 0) {
+		(void) fprintf(stderr, "__ops_reader_set_mmap: can't fstat\n");
+	} else if ((mem = calloc(1, sizeof(*mem))) == NULL) {
+		(void) fprintf(stderr, "__ops_reader_set_mmap: bad alloc\n");
+	} else {
 		mem->size = (uint64_t)st.st_size;
 		mem->offset = 0;
 		mem->fd = fd;

Reply via email to