Refactor query string truncation on top of
Commit fixed the query string truncation in dispatcher
to make it locale-aware.  This patch refactors that change so as to
avoid accessing a string beyond its length.
@@ -872,8 +872,6 @@ buildGpQueryString(DispatchCommandQueryParms *pQueryParms,
int total_query_len;
char *shared_query,
*pos;
- int cnt,
- character_len;
MemoryContext oldContext;
/*
@@ -888,19 +886,14 @@ buildGpQueryString(DispatchCommandQueryParms *pQueryParms,
*
* Here we only need to determine the truncated size, the actual work is
* done later when copying it to the result buffer.
+ *
+ * The +1 and -1 below are adjustments to accommodate terminating null
+ * character.
*/
- cnt = 0;
- if (querytree || plantree)
- {
- while (cnt < QUERY_STRING_TRUNCATE_SIZE)
- {
- character_len = pg_encoding_mblen(GetDatabaseEncoding(), command + cnt);
- cnt += character_len;
- }
- command_len = strnlen(command, cnt - character_len) + 1;
- }
- else
command_len = strlen(command) + 1;
+ if ((querytree || plantree) && command_len > QUERY_STRING_TRUNCATE_SIZE)
+ command_len = pg_mbcliplen(command, command_len,
+ QUERY_STRING_TRUNCATE_SIZE-1) + 1;
initStringInfo(&resgroupInfo);
if (IsResGroupActivated())
