]> granicus.if.org Git - postgresql/commitdiff
Fix string truncation to be multibyte-aware in text_name and bpchar_name.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 May 2012 21:34:51 +0000 (17:34 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 May 2012 21:34:51 +0000 (17:34 -0400)
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

src/backend/utils/adt/name.c
src/backend/utils/adt/varchar.c
src/backend/utils/adt/varlena.c

index 125b533963288e7e99eb4cd38481691f8cc46002..6d9342d7aaaba6ea7787bf8f5ee3291429df2b14 100644 (file)
@@ -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);
index 5774102bc02c9b4507e224ddd801f42de8852aad..199330cef295db88fe50131fd154b9623fe37260 100644 (file)
@@ -372,9 +372,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)
@@ -384,16 +384,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);
 }
 
index a5592d56e1104ea7491d1831d872ce1471146ad0..53989d1ecb36905485108a01c39caa69563240bd 100644 (file)
@@ -2256,18 +2256,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);
 }