From 4d645bdf08de419b207b0064986d81f758900809 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Fri, 26 Mar 2021 20:05:10 -0300
Subject: [PATCH v30 6/7] add length check

---
 src/interfaces/libpq/libpq-trace.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/interfaces/libpq/libpq-trace.c b/src/interfaces/libpq/libpq-trace.c
index 06d8d981a3..6c58a578ac 100644
--- a/src/interfaces/libpq/libpq-trace.c
+++ b/src/interfaces/libpq/libpq-trace.c
@@ -637,6 +637,16 @@ pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)
 	}
 
 	fputc('\n', conn->Pfdebug);
+
+	/*
+	 * Verify the printing routine did it right.  Note that the one-byte
+	 * message identifier is not included in the length, but our cursor
+	 * does include it.
+	 */
+	if (LogCursor - 1 != length)
+		fprintf(conn->Pfdebug,
+				"mismatched message length: consumed %d, expected %d\n",
+				LogCursor - 1, length);
 }
 
 void
-- 
2.20.1

