From 0cf4687de005c81097aaf5a1055488e8f6b3c279 Mon Sep 17 00:00:00 2001
From: Andrey <amborodin@acm.org>
Date: Mon, 24 Jun 2019 13:36:47 +0500
Subject: [PATCH] Use memcpy in pglz decompression

---
 src/common/pg_lzcompress.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/common/pg_lzcompress.c b/src/common/pg_lzcompress.c
index 988b3987d0..f199be109f 100644
--- a/src/common/pg_lzcompress.c
+++ b/src/common/pg_lzcompress.c
@@ -731,16 +731,19 @@ pglz_decompress(const char *source, int32 slen, char *dest,
 
 				/*
 				 * Now we copy the bytes specified by the tag from OUTPUT to
-				 * OUTPUT. It is dangerous and platform dependent to use
-				 * memcpy() here, because the copied areas could overlap
-				 * extremely!
+				 * OUTPUT. The copied areas could overlap, to preven possible
+				 * uncertanity, we copy only non-overlapping regions.
 				 */
 				len = Min(len, destend - dp);
-				while (len--)
+				while (off <= len)
 				{
-					*dp = dp[-off];
-					dp++;
+					memcpy(dp, dp - off, off);
+					len -= off;
+					dp+=off;
+					off *= 2;
 				}
+				memcpy(dp, dp - off, len);
+				dp+=len;
 			}
 			else
 			{
-- 
2.20.1

