1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

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
This commit is contained in:
Tom Lane
2012-05-25 17:34:51 -04:00
parent 73cc7d3b24
commit d3b97d1488
3 changed files with 14 additions and 22 deletions

View File

@ -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);
}