diff --git a/postgresql-14beta3/src/common/hex.c b/postgresql-14beta3_hb/src/common/hex.c
index 3b1bc8f..fa23564 100644
--- a/postgresql-14beta3/src/common/hex.c
+++ b/postgresql-14beta3_hb/src/common/hex.c
@@ -78,22 +78,22 @@ pg_hex_encode(const char *src, size_t srclen, char *dst, size_t dstlen)
 
 	p = dst;
 
-	while (src < end)
+	/*
+	 * Leave if there is an overflow in the area allocated for the encoded
+	 * string.
+	 */
+	if (dstlen < srclen << 1)
 	{
-		/*
-		 * Leave if there is an overflow in the area allocated for the encoded
-		 * string.
-		 */
-		if ((p - dst + 2) > dstlen)
-		{
 #ifdef FRONTEND
-			pg_log_fatal("overflow of destination buffer in hex encoding");
-			exit(EXIT_FAILURE);
+		pg_log_fatal("overflow of destination buffer in hex encoding");
+		exit(EXIT_FAILURE);
 #else
-			elog(ERROR, "overflow of destination buffer in hex encoding");
+		elog(ERROR, "overflow of destination buffer in hex encoding");
 #endif
-		}
+	}
 
+	while (src < end)
+	{
 		*p++ = hextbl[(*src >> 4) & 0xF];
 		*p++ = hextbl[*src & 0xF];
 		src++;
