mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
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
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user