diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2012-05-25 17:35:24 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2012-05-25 17:35:24 -0400 |
commit | 6f163609bded024a2b5c4506745740695ecfc04f (patch) | |
tree | 8ba85c57f8c97acc8f16916ccdbfc600b6f215bb /src/backend/utils/adt/varchar.c | |
parent | bd43c50a5bd560224f996d6bd14e1e3cb5a5b06b (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/varchar.c')
-rw-r--r-- | src/backend/utils/adt/varchar.c | 14 |
1 files changed, 4 insertions, 10 deletions
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index c954e2034ad..8fdbb2055bc 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -376,9 +376,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) @@ -388,16 +388,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); } |