From d86dee3eddfaec1f2faddc5b807f2b13d58adcdd Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Thu, 22 Aug 2002 04:55:05 +0000
Subject: [PATCH] This patch should fix the problem. Doesn't include my
 previous patch for repeat(). Again, somewhat off-the-cuff, so I might have
 missed something...

test=# select lpad('xxxxx',1431655765,'yyyyyyyyyyyyyyyy');
ERROR:  Requested length too large
test=# select rpad('xxxxx',1431655765,'yyyyyyyyyyyyyyyy');
ERROR:  Requested length too large

(That's on a Unicode DB, haven't tested other encodings but AFAICT
this fix should still work.)

Neil Conway
---
 src/backend/utils/adt/oracle_compat.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c
index dfeb18c551b..6236d74c9d4 100644
--- a/src/backend/utils/adt/oracle_compat.c
+++ b/src/backend/utils/adt/oracle_compat.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	$Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.39 2002/08/22 04:54:20 momjian Exp $
+ *	$Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.40 2002/08/22 04:55:05 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -199,6 +199,11 @@ lpad(PG_FUNCTION_ARGS)
 
 #ifdef MULTIBYTE
 	bytelen = pg_database_encoding_max_length() * len;
+
+	/* check for integer overflow */
+	if (len != 0 && bytelen / pg_database_encoding_max_length() != len)
+		elog(ERROR, "Requested length too large");
+
 	ret = (text *) palloc(VARHDRSZ + bytelen);
 #else
 	ret = (text *) palloc(VARHDRSZ + len);
@@ -310,6 +315,11 @@ rpad(PG_FUNCTION_ARGS)
 
 #ifdef MULTIBYTE
 	bytelen = pg_database_encoding_max_length() * len;
+
+	/* Check for integer overflow */
+	if (len != 0 && bytelen / pg_database_encoding_max_length() != len)
+		elog(ERROR, "Requested length too large");
+
 	ret = (text *) palloc(VARHDRSZ + bytelen);
 #else
 	ret = (text *) palloc(VARHDRSZ + len);
-- 
GitLab