diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2012-05-25 17:34:51 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2012-05-25 17:34:51 -0400 |
commit | d3b97d1488ba868f6bab12eeaf0b9ae75fad2566 (patch) | |
tree | c0baa7032cd63ff764b74b51524aac000b230d21 /src/backend/utils/adt/name.c | |
parent | 73cc7d3b240e1d46b1996382e5735a820f8bc3f7 (diff) | |
download | postgresql-d3b97d1488ba868f6bab12eeaf0b9ae75fad2566.tar.gz |
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/name.c')
-rw-r--r-- | src/backend/utils/adt/name.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c index 125b533963..6d9342d7aa 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); |