From 0ba91776d153df55189ea1968381cc9628b84e00 Mon Sep 17 00:00:00 2001
From: Jason Volk <jason@zemos.net>
Date: Tue, 20 Mar 2018 01:23:42 -0700
Subject: [PATCH] ircd::server: Move insufficient buffer checks into the
 creator functions.

---
 ircd/server.cc | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/ircd/server.cc b/ircd/server.cc
index 2509ec63e..472b16988 100644
--- a/ircd/server.cc
+++ b/ircd/server.cc
@@ -2052,16 +2052,6 @@ ircd::server::tag::read_head(const const_buffer &buffer,
 	if(pos == string_view::npos)
 	{
 		state.head_read += size(buffer);
-
-		// Check that the user hasn't run out of head buffer space without
-		// seeing a terminator. If so, we have to throw out of here and then
-		// abort this user's request.
-		if(unlikely(state.head_read >= size(req.in.head)))
-			throw buffer_overrun
-			{
-				"Supplied buffer of %zu too small for HTTP head", size(req.in.head)
-			};
-
 		return {};
 	}
 
@@ -2277,9 +2267,11 @@ const
 	assert(request);
 	const auto &req{*request};
 	const auto &head{req.in.head};
-	const auto &content{req.in.content};
-	if(state.head_read >= size(head))
-		return {};
+	if(unlikely(state.head_read >= size(req.in.head)))
+		throw buffer_overrun
+		{
+			"Supplied buffer of %zu too small for HTTP head", size(req.in.head)
+		};
 
 	const size_t remaining
 	{
@@ -2302,9 +2294,15 @@ const
 	assert(request);
 	const auto &req{*request};
 	const auto &content{req.in.content};
+	if(unlikely(size(content) <= state.content_read))
+		throw buffer_overrun
+		{
+			"Content buffer of %zu bytes too small to read %zu bytes of content",
+			size(content),
+			state.content_length
+		};
 
 	// The amount of bytes we still have to read to for the response
-	assert(size(content) >= state.content_read);
 	const size_t remaining
 	{
 		size(content) - state.content_read