--- a/src/common/pg_lzcompress.c	2023-10-18 12:34:30.616000000 +0000
+++ b/src/common/pg_lzcompress.c	2023-10-18 16:42:17.856000000 +0000
@@ -738,8 +738,10 @@
 				 * source, or if we obtained off = 0, we have problems.  (We
 				 * must check this, else we risk an infinite loop below in the
 				 * face of corrupt data.)
+				 * Also check if we fell before the beginning of the
+				 * destination.
 				 */
-				if (unlikely(sp > srcend || off == 0))
+				if (unlikely(sp > srcend || off == 0 || (dp - off) < dest))
 					return -1;
 
 				/*

