900526
diff --git a/lib/dns/message.c b/lib/dns/message.c
900526
index 73def73..3d2de4f 100644
900526
--- a/lib/dns/message.c
900526
+++ b/lib/dns/message.c
900526
@@ -1736,7 +1736,7 @@ dns_message_renderbegin(dns_message_t *msg, dns_compress_t *cctx,
900526
 	if (r.length < DNS_MESSAGE_HEADERLEN)
900526
 		return (ISC_R_NOSPACE);
900526
 
900526
-	if (r.length < msg->reserved)
900526
+	if (r.length - DNS_MESSAGE_HEADERLEN < msg->reserved)
900526
 		return (ISC_R_NOSPACE);
900526
 
900526
 	/*
900526
@@ -1863,8 +1863,29 @@ norender_rdataset(const dns_rdataset_t *rdataset, unsigned int options)
900526
 
900526
 	return (ISC_TRUE);
900526
 }
900526
-
900526
 #endif
900526
+
900526
+static isc_result_t
900526
+renderset(dns_rdataset_t *rdataset, dns_name_t *owner_name,
900526
+	  dns_compress_t *cctx, isc_buffer_t *target,
900526
+	  unsigned int reserved, unsigned int options, unsigned int *countp)
900526
+{
900526
+	isc_result_t result;
900526
+
900526
+	/*
900526
+	 * Shrink the space in the buffer by the reserved amount.
900526
+	 */
900526
+	if (target->length - target->used < reserved)
900526
+		return (ISC_R_NOSPACE);
900526
+
900526
+	target->length -= reserved;
900526
+	result = dns_rdataset_towire(rdataset, owner_name,
900526
+				     cctx, target, options, countp);
900526
+	target->length += reserved;
900526
+
900526
+	return (result);
900526
+}
900526
+
900526
 isc_result_t
900526
 dns_message_rendersection(dns_message_t *msg, dns_section_t sectionid,
900526
 			  unsigned int options)
900526
@@ -1907,6 +1928,8 @@ dns_message_rendersection(dns_message_t *msg, dns_section_t sectionid,
900526
 	/*
900526
 	 * Shrink the space in the buffer by the reserved amount.
900526
 	 */
900526
+	if (msg->buffer->length - msg->buffer->used < msg->reserved)
900526
+		return (ISC_R_NOSPACE);
900526
 	msg->buffer->length -= msg->reserved;
900526
 
900526
 	total = 0;
900526
@@ -2183,9 +2206,8 @@ dns_message_renderend(dns_message_t *msg) {
900526
 		 * Render.
900526
 		 */
900526
 		count = 0;
900526
-		result = dns_rdataset_towire(msg->opt, dns_rootname,
900526
-					     msg->cctx, msg->buffer, 0,
900526
-					     &count);
900526
+		result = renderset(msg->opt, dns_rootname, msg->cctx,
900526
+				   msg->buffer, msg->reserved, 0, &count);
900526
 		msg->counts[DNS_SECTION_ADDITIONAL] += count;
900526
 		if (result != ISC_R_SUCCESS)
900526
 			return (result);
900526
@@ -2201,9 +2223,8 @@ dns_message_renderend(dns_message_t *msg) {
900526
 		if (result != ISC_R_SUCCESS)
900526
 			return (result);
900526
 		count = 0;
900526
-		result = dns_rdataset_towire(msg->tsig, msg->tsigname,
900526
-					     msg->cctx, msg->buffer, 0,
900526
-					     &count);
900526
+		result = renderset(msg->tsig, msg->tsigname, msg->cctx,
900526
+				   msg->buffer, msg->reserved, 0, &count);
900526
 		msg->counts[DNS_SECTION_ADDITIONAL] += count;
900526
 		if (result != ISC_R_SUCCESS)
900526
 			return (result);
900526
@@ -2224,9 +2245,8 @@ dns_message_renderend(dns_message_t *msg) {
900526
 		 * the owner name of a SIG(0) is irrelevant, and will not
900526
 		 * be set in a message being rendered.
900526
 		 */
900526
-		result = dns_rdataset_towire(msg->sig0, dns_rootname,
900526
-					     msg->cctx, msg->buffer, 0,
900526
-					     &count);
900526
+		result = renderset(msg->sig0, dns_rootname, msg->cctx,
900526
+				   msg->buffer, msg->reserved, 0, &count);
900526
 		msg->counts[DNS_SECTION_ADDITIONAL] += count;
900526
 		if (result != ISC_R_SUCCESS)
900526
 			return (result);