From 16ba83673db3667a0862b255ca3edcfdabd41237 Mon Sep 17 00:00:00 2001
From: japinli <japinli@hotmail.com>
Date: Thu, 16 Jan 2020 23:02:21 +0800
Subject: [PATCH] Code cleanup for build_regexp_split_result

---
 src/backend/utils/adt/regexp.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/backend/utils/adt/regexp.c b/src/backend/utils/adt/regexp.c
index 2094d50d81..c5d5a5f9db 100644
--- a/src/backend/utils/adt/regexp.c
+++ b/src/backend/utils/adt/regexp.c
@@ -1467,14 +1467,15 @@ build_regexp_split_result(regexp_matches_ctx *splitctx)
 	if (startpos < 0)
 		elog(ERROR, "invalid match ending position");
 
+	endpos = splitctx->match_locs[splitctx->next_match * 2];
+	if (endpos < startpos)
+		elog(ERROR, "invalid match starting position");
+
 	if (buf)
 	{
 		int			bufsiz PG_USED_FOR_ASSERTS_ONLY = splitctx->conv_bufsiz;
 		int			len;
 
-		endpos = splitctx->match_locs[splitctx->next_match * 2];
-		if (endpos < startpos)
-			elog(ERROR, "invalid match starting position");
 		len = pg_wchar2mb_with_len(splitctx->wide_str + startpos,
 								   buf,
 								   endpos - startpos);
@@ -1483,9 +1484,6 @@ build_regexp_split_result(regexp_matches_ctx *splitctx)
 	}
 	else
 	{
-		endpos = splitctx->match_locs[splitctx->next_match * 2];
-		if (endpos < startpos)
-			elog(ERROR, "invalid match starting position");
 		return DirectFunctionCall3(text_substr,
 								   PointerGetDatum(splitctx->orig_str),
 								   Int32GetDatum(startpos + 1),
-- 
2.17.1

