From f4a8997ec08c0aaec8326b0f0dda9f3a001d5865 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyoga.ntt@gmail.com>
Date: Thu, 11 Jun 2020 20:29:23 +0900
Subject: [PATCH] Make sure to consume stream-terminating packet

When compressed stream is ended with a full stream packet, it must be
followed by a terminating normal packet with zero-length.  Make sure
consume such terminating packet at the end of stream.
---
 contrib/pgcrypto/pgp-compress.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/contrib/pgcrypto/pgp-compress.c b/contrib/pgcrypto/pgp-compress.c
index 0505bdee92..69a8d6c577 100644
--- a/contrib/pgcrypto/pgp-compress.c
+++ b/contrib/pgcrypto/pgp-compress.c
@@ -286,7 +286,20 @@ restart:
 
 	dec->buf_data = dec->buf_len - dec->stream.avail_out;
 	if (res == Z_STREAM_END)
+	{
+		uint8 *tmp;
+
+		/*
+		 * If source stream ends with a full stream packet, it is followed by
+		 * an extra normal zero-length packet, which should be consumed before
+		 * reading further. If we have already seen a terminating packet,
+		 * nothing happen by this call.
+		 */
+		res = pullf_read(src, 1, &tmp);
+		Assert(res == 0);
+		
 		dec->eof = 1;
+	}
 	goto restart;
 }
 
-- 
2.18.2

