aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-05-25 17:35:14 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-05-25 17:35:14 -0400
commit2a7c90c83bc5b668764faae643b9f3d8c652667a (patch)
tree82c160e2624d0a2f6da06dd71e32e36aa9a56fd6
parent5e0f3c793a3767f38498d9cd54a3bfeeccdce6bf (diff)
downloadpostgresql-2a7c90c83bc5b668764faae643b9f3d8c652667a.tar.gz
postgresql-2a7c90c83bc5b668764faae643b9f3d8c652667a.zip
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
-rw-r--r--src/backend/utils/adt/name.c10
-rw-r--r--src/backend/utils/adt/varchar.c14
-rw-r--r--src/backend/utils/adt/varlena.c12
3 files changed, 14 insertions, 22 deletions
diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c
index 23b3722375e..9729c0d3acc 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);
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
index c25385dee77..aac1039f551 100644
--- a/src/backend/utils/adt/varchar.c
+++ b/src/backend/utils/adt/varchar.c
@@ -371,9 +371,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)
@@ -383,16 +383,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);
}
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index b9b54e6db63..c8552a87c9f 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -1936,18 +1936,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);
}