提交 d3b97d14 编写于 作者: T Tom Lane

Fix string truncation to be multibyte-aware in text_name and bpchar_name.

Previously, casts to name could generate invalidly-encoded results.

Also, make these functions match namein() more exactly, by consistently
using palloc0() instead of ad-hoc zeroing code.

Back-patch to all supported branches.

Karl Schnaitter and Tom Lane
上级 73cc7d3b
......@@ -46,13 +46,17 @@ Datum
namein(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
NameData *result;
Name result;
int len;
len = strlen(s);
/* Truncate oversize input */
if (len >= NAMEDATALEN)
len = pg_mbcliplen(s, len, NAMEDATALEN - 1);
result = (NameData *) palloc0(NAMEDATALEN);
/* We use palloc0 here to ensure result is zero-padded */
result = (Name) palloc0(NAMEDATALEN);
memcpy(NameStr(*result), s, len);
PG_RETURN_NAME(result);
......
......@@ -372,9 +372,9 @@ bpchar_name(PG_FUNCTION_ARGS)
len = VARSIZE_ANY_EXHDR(s);
s_data = VARDATA_ANY(s);
/* Truncate to max length for a Name */
/* Truncate oversize input */
if (len >= NAMEDATALEN)
len = NAMEDATALEN - 1;
len = pg_mbcliplen(s_data, len, NAMEDATALEN - 1);
/* Remove trailing blanks */
while (len > 0)
......@@ -384,16 +384,10 @@ bpchar_name(PG_FUNCTION_ARGS)
len--;
}
result = (NameData *) palloc(NAMEDATALEN);
/* We use palloc0 here to ensure result is zero-padded */
result = (Name) palloc0(NAMEDATALEN);
memcpy(NameStr(*result), s_data, len);
/* Now null pad to full length... */
while (len < NAMEDATALEN)
{
*(NameStr(*result) + len) = '\0';
len++;
}
PG_RETURN_NAME(result);
}
......
......@@ -2256,18 +2256,12 @@ text_name(PG_FUNCTION_ARGS)
/* Truncate oversize input */
if (len >= NAMEDATALEN)
len = NAMEDATALEN - 1;
len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
result = (Name) palloc(NAMEDATALEN);
/* We use palloc0 here to ensure result is zero-padded */
result = (Name) palloc0(NAMEDATALEN);
memcpy(NameStr(*result), VARDATA_ANY(s), len);
/* now null pad to full length... */
while (len < NAMEDATALEN)
{
*(NameStr(*result) + len) = '\0';
len++;
}
PG_RETURN_NAME(result);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册