From 53edb8dc0274e0d05eb0371ce84b7fb14cb5eb5a Mon Sep 17 00:00:00 2001
From: Tatsuo Ishii <ishii@postgresql.org>
Date: Sun, 2 Dec 2012 21:11:15 +0900
Subject: [PATCH] Fix psql crash while parsing SQL file whose encoding is
 different from client encoding and the client encoding is not *safe* one.
 Such an example is, file encoding is UTF-8 and client encoding SJIS. Patch
 contributed by Jiang Guiqing.

---
 src/bin/psql/psqlscan.l | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bin/psql/psqlscan.l b/src/bin/psql/psqlscan.l
index d32a12c63c8..6c1429815f2 100644
--- a/src/bin/psql/psqlscan.l
+++ b/src/bin/psql/psqlscan.l
@@ -1807,7 +1807,7 @@ prepare_buffer(const char *txt, int len, char **txtcopy)
 			/* first byte should always be okay... */
 			newtxt[i] = txt[i];
 			i++;
-			while (--thislen > 0)
+			while (--thislen > 0 && i < len)
 				newtxt[i++] = (char) 0xFF;
 		}
 	}
-- 
GitLab