From baf462f1854ffc8a38b0c4bdabb4575e36cea0f6 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 11 May 2025 13:20:59 -0400
Subject: [PATCH v1 07/11] Don't leak the startup-packet buffer in
 ProcessStartupPacket.

This is the first actual code bug fix in this patch series.

I only bothered to free the buffer in the successful-exit code
paths, not the error-exit ones, since we'd quit soon anyway in
the latter cases.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/285483.1746756246@sss.pgh.pa.us
---
 src/backend/tcop/backend_startup.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/backend/tcop/backend_startup.c b/src/backend/tcop/backend_startup.c
index a7d1fec981f..0b956627934 100644
--- a/src/backend/tcop/backend_startup.c
+++ b/src/backend/tcop/backend_startup.c
@@ -627,6 +627,8 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
 					 errmsg("received unencrypted data after SSL request"),
 					 errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
 
+		pfree(buf);
+
 		/*
 		 * regular startup packet, cancel, etc packet should follow, but not
 		 * another SSL negotiation request, and a GSS request should only
@@ -681,6 +683,8 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
 					 errmsg("received unencrypted data after GSSAPI encryption request"),
 					 errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
 
+		pfree(buf);
+
 		/*
 		 * regular startup packet, cancel, etc packet should follow, but not
 		 * another GSS negotiation request, and an SSL request should only
@@ -858,6 +862,8 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
 	if (am_walsender && !am_db_walsender)
 		port->database_name[0] = '\0';
 
+	pfree(buf);
+
 	/*
 	 * Done filling the Port structure
 	 */
-- 
2.43.5

