hex_encode_length_check_outside_loop.patch

application/octet-stream

Filename: hex_encode_length_check_outside_loop.patch
Type: application/octet-stream
Part: 0
Message: PG14: Avoid checking output-buffer-length for every encoded byte during pg_hex_encode

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: unified
File+
src/common/hex.c 11 11
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++;