From 71e985a07a7ae9319c00629f319a4c30a9e32fd6 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 25 May 2012 17:34:58 -0400 Subject: 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 --- src/backend/utils/adt/varlena.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/backend/utils/adt/varlena.c') diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 124000fe7f4..3e2243958a6 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -2198,18 +2198,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); } -- cgit v1.2.3