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/name.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/backend/utils/adt/name.c') diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c index 09c12efd8d5..a2fee8e4d68 100644 --- a/src/backend/utils/adt/name.c +++ b/src/backend/utils/adt/name.c @@ -46,13 +46,17 @@ Datum namein(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); - NameData *result; + Name result; int len; len = strlen(s); - len = pg_mbcliplen(s, len, NAMEDATALEN - 1); - result = (NameData *) palloc0(NAMEDATALEN); + /* Truncate oversize input */ + if (len >= NAMEDATALEN) + len = pg_mbcliplen(s, len, NAMEDATALEN - 1); + + /* We use palloc0 here to ensure result is zero-padded */ + result = (Name) palloc0(NAMEDATALEN); memcpy(NameStr(*result), s, len); PG_RETURN_NAME(result); -- cgit v1.2.3