diff options
| author | Tom Lane <tgl@sss.pgh.pa.us> | 2012-05-25 17:35:05 -0400 |
|---|---|---|
| committer | Tom Lane <tgl@sss.pgh.pa.us> | 2012-05-25 17:35:05 -0400 |
| commit | d566ad3eb3e2ea532358123795f16fbeb5c0a46e (patch) | |
| tree | a7b827e8a22e2b6ae93f632fafaa169b559c157e /src/backend/utils/adt/varlena.c | |
| parent | 965d76f972bec5595e84a8e8567636cb3ba7a1f2 (diff) | |
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
Diffstat (limited to 'src/backend/utils/adt/varlena.c')
| -rw-r--r-- | src/backend/utils/adt/varlena.c | 12 |
1 files changed, 3 insertions, 9 deletions
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index ccef8ae1bed..7d97d61ff26 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -2145,18 +2145,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); } |
