8000 Change CREATE TYPE to require datatype output and send functions to have · postgrespro/postgres_cluster@6c412f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c412f0

Browse files
committed
Change CREATE TYPE to require datatype output and send functions to have
only one argument. (Per recent discussion, the option to accept multiple arguments is pretty useless for user-defined types, and would be a likely source of security holes if it was used.) Simplify call sites of output/send functions to not bother passing more than one argument.
1 parent ae793ff commit 6c412f0

File tree

18 files changed

+102
-216
lines changed
  • nodes
  • tcop
  • utils
  • include/utils
  • pl
  • 18 files changed

    +102
    -216
    lines changed

    doc/src/sgml/ref/create_type.sgml

    Lines changed: 7 additions & 13 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1,5 +1,5 @@
    11
    <!--
    2-
    $PostgreSQL: pgsql/doc/src/sgml/ref/create_type.sgml,v 1.54 2005/01/04 00:39:53 tgl Exp $
    2+
    $PostgreSQL: pgsql/doc/src/sgml/ref/create_type.sgml,v 1.55 2005/05/01 18:56:17 tgl Exp $
    33
    PostgreSQL documentation
    44
    -->
    55

    @@ -107,13 +107,10 @@ CREATE TYPE <replaceable class="parameter">name</replaceable> (
    107107
    (or the type's own OID for a composite type),
    108108
    and the third is the <literal>typmod</> of the destination column, if known
    109109
    (-1 will be passed if not).
    110-
    The input function should return a value of the data type itself.
    111-
    The output function may be
    112-
    declared as taking one argument of the new data type, or as taking
    113-
    two arguments of which the second is type <type>oid</type>.
    114-
    The second argument is again the array element type OID for array types
    115-
    or the type OID for composite types.
    116-
    The output function should return type <type>cstring</type>.
    110+
    The input function must return a value of the data type itself.
    111+
    The output function must be
    112+
    declared as taking one argument of the new data type.
    113+
    The output function must return type <type>cstring</type>.
    117114
    </para>
    118115

    119116
    <para>
    @@ -137,11 +134,8 @@ CREATE TYPE <replaceable class="parameter">name</replaceable> (
    137134
    <replaceable class="parameter">send_function</replaceable> converts
    138135
    from the internal representation to the external binary representation.
    139136
    If this function is not supplied, the type cannot participate in binary
    140-
    output. The send function may be
    141-
    declared as taking one argument of the new data type, or as taking
    142-
    two arguments of which the second is type <type>oid</type>.
    143-
    The second argument is again the array element type OID for array types
    144-
    or the type OID for composite types.
    137+
    output. The send function must be
    138+
    declared as taking one argument of the new data type.
    145139
    The send function must return type <type>bytea</type>.
    146140
    </para>
    147141

    src/backend/access/common/printtup.c

    Lines changed: 13 additions & 25 deletions
    Original file line numberDiff line numberDiff line change
    @@ -9,7 +9,7 @@
    99
    * Portions Copyright (c) 1994, Regents of the University of California
    1010
    *
    1111
    * IDENTIFICATION
    12-
    * $PostgreSQL: pgsql/src/backend/access/common/printtup.c,v 1.89 2005/04/23 17:45:35 tgl Exp $
    12+
    * $PostgreSQL: pgsql/src/backend/access/common/printtup.c,v 1.90 2005/05/01 18:56:17 tgl Exp $
    1313
    *
    1414
    *-------------------------------------------------------------------------
    1515
    */
    @@ -48,7 +48,6 @@ typedef struct
    4848
    { /* Per-attribute information */
    4949
    Oid typoutput; /* Oid for the type's text output fn */
    5050
    Oid typsend; /* Oid for the type's binary output fn */
    51-
    Oid typioparam; /* param to pass to the output fn */
    5251
    bool typisvarlena; /* is it varlena (ie possibly toastable)? */
    5352
    int16 format; /* format code for this column */
    5453
    FmgrInfo finfo; /* Precomputed call info for output fn */
    @@ -263,15 +262,13 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
    263262
    {
    264263
    getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
    265264
    &thisState->typoutput,
    266-
    &thisState->typioparam,
    267265
    &thisState->typisvarlena);
    268266
    fmgr_info(thisState->typoutput, &thisState->finfo);
    269267
    }
    270268
    else if (format == 1)
    271269
    {
    272270
    getTypeBinaryOutputInfo(typeinfo->attrs[i]->atttypid,
    273271
    &thisState->typsend,
    274-
    &thisState->typioparam,
    275272
    &thisState->typisvarlena);
    276273
    fmgr_info(thisState->typsend, &thisState->finfo);
    277274
    }
    @@ -338,10 +335,8 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
    338335
    /* Text output */
    339336
    char *outputstr;
    340337

    341-
    outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
    342-
    attr,
    343-
    ObjectIdGetDatum(thisState->typioparam),
    344-
    Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
    338+
    outputstr = DatumGetCString(FunctionCall1(&thisState->finfo,
    339+
    attr));
    345340
    pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
    346341
    pfree(outputstr);
    347342
    }
    @@ -350,9 +345,8 @@ printtup(TupleTableSlot *slot, DestReceiver *self)
    350345
    /* Binary output */
    351346
    bytea *outputbytes;
    352347

    353-
    outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
    354-
    attr,
    355-
    ObjectIdGetDatum(thisState->typioparam)));
    348+
    outputbytes = DatumGetByteaP(FunctionCall1(&thisState->finfo,
    349+
    attr));
    356350
    /* We assume the result will not have been toasted */
    357351
    pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
    358352
    pq_sendbytes(&buf, VARDATA(outputbytes),
    @@ -439,10 +433,8 @@ printtup_20(TupleTableSlot *slot, DestReceiver *self)
    439433
    else
    440434
    attr = origattr;
    441435

    442-
    outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
    443-
    attr,
    444-
    ObjectIdGetDatum(thisState->typioparam),
    445-
    Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
    436+
    outputstr = DatumGetCString(FunctionCall1(&thisState->finfo,
    437+
    attr));
    446438
    pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
    447439
    pfree(outputstr);
    448440

    @@ -534,8 +526,7 @@ debugtup(TupleTableSlot *slot, DestReceiver *self)
    534526
    attr;
    535527
    char *value;
    536528
    bool isnull;
    537-
    Oid typoutput,
    538-
    typioparam;
    529+
    Oid typoutput;
    539530
    bool typisvarlena;
    540531

    541532
    for (i = 0; i < natts; ++i)
    @@ -544,7 +535,7 @@ debugtup(TupleTableSlot *slot, DestReceiver *self)
    544535
    if (isnull)
    545536
    continue;
    546537
    getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
    547-
    &typoutput, &typioparam, &typisvarlena);
    538+
    &typoutput, &typisvarlena);
    548539

    549540
    /*
    550541
    * If we have a toasted datum, forcibly detoast it here to avoid
    @@ -555,10 +546,8 @@ debugtup(TupleTableSlot *slot, DestReceiver *self)
    555546
    else
    556547
    attr = origattr;
    557548

    558-
    value = DatumGetCString(OidFunctionCall3(typoutput,
    559-
    attr,
    560-
    ObjectIdGetDatum(typioparam),
    561-
    Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
    549+
    value = DatumGetCString(OidFunctionCall1(typoutput,
    550+
    attr));
    562551

    563552
    printatt((unsigned) i + 1, typeinfo->attrs[i], value);
    564553

    @@ -647,9 +636,8 @@ printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
    647636
    else
    648637
    attr = origattr;
    649638

    650-
    outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
    651-
    attr,
    652-
    ObjectIdGetDatum(thisState->typioparam)));
    639+
    outputbytes = DatumGetByteaP(FunctionCall1(&thisState->finfo,
    640+
    attr));
    653641
    /* We assume the result will not have been toasted */
    654642
    pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
    655643
    pq_sendbytes(&buf, VARDATA(outputbytes),

    src/backend/bootstrap/bootstrap.c

    Lines changed: 3 additions & 5 deletions
    Original file line numberDiff line numberDiff line change
    @@ -8,7 +8,7 @@
    88
    * Portions Copyright (c) 1994, Regents of the University of California
    99
    *
    1010
    * IDENTIFICATION
    11-
    * $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.202 2005/04/14 20:03:23 tgl Exp $
    11+
    * $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.203 2005/05/01 18:56:17 tgl Exp $
    1212
    *
    1313
    *-------------------------------------------------------------------------
    1414
    */
    @@ -850,10 +850,8 @@ InsertOneValue(char *value, int i)
    850850
    CStringGetDatum(value),
    851851
    ObjectIdGetDatum(typioparam),
    852852
    Int32GetDatum(-1));
    853-
    prt = DatumGetCString(OidFunctionCall3(typoutput,
    854-
    values[i],
    855-
    ObjectIdGetDatum(typioparam),
    856-
    Int32GetDatum(-1)));
    853+
    prt = DatumGetCString(OidFunctionCall1(typoutput,
    854+
    values[i]));
    857855
    elog(DEBUG4, "inserted -> %s", prt);
    858856
    pfree(prt);
    859857
    }

    src/backend/commands/copy.c

    Lines changed: 10 additions & 18 deletions
    Original file line numberDiff line numberDiff line change
    @@ -8,7 +8,7 @@
    88
    *
    99
    *
    1010
    * IDENTIFICATION
    11-
    * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.240 2005/04/14 20:03:23 tgl Exp $
    11+
    * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.241 2005/05/01 18:56:18 tgl Exp $
    1212
    *
    1313
    *-------------------------------------------------------------------------
    1414
    */
    @@ -1178,8 +1178,6 @@ CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
    11781178
    Form_pg_attribute *attr;
    11791179
    FmgrInfo *out_functions;
    11801180
    bool *force_quote;
    1181-
    Oid *typioparams;
    1182-
    bool *isvarlena;
    11831181
    char *string;
    11841182
    ListCell *cur;
    11851183
    MemoryContext oldcontext;
    @@ -1194,22 +1192,21 @@ CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
    11941192
    * Get info about the columns we need to process.
    11951193
    */
    11961194
    out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
    1197-
    typioparams = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
    1198-
    isvarlena = (bool *) palloc(num_phys_attrs * sizeof(bool));
    11991195
    force_quote = (bool *) palloc(num_phys_attrs * sizeof(bool));
    12001196
    foreach(cur, attnumlist)
    12011197
    {
    12021198
    int attnum = lfirst_int(cur);
    12031199
    Oid out_func_oid;
    1200+
    bool isvarlena;
    12041201

    12051202
    if (binary)
    12061203
    getTypeBinaryOutputInfo(attr[attnum - 1]->atttypid,
    1207-
    &out_func_oid, &typioparams[attnum - 1],
    1208-
    &isvarlena[attnum - 1]);
    1204+
    &out_func_oid,
    1205+
    &isvarlena);
    12091206
    else
    12101207
    getTypeOutputInfo(attr[attnum - 1]->atttypid,
    1211-
    &out_func_oid, &typioparams[attnum - 1],
    1212-
    &isvarlena[attnum - 1]);
    1208+
    &out_func_oid,
    1209+
    &isvarlena);
    12131210
    fmgr_info(out_func_oid, &out_functions[attnum - 1]);
    12141211

    12151212
    if (list_member_int(force_quote_atts, attnum))
    @@ -1321,10 +1318,8 @@ CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
    13211318
    {
    13221319
    if (!binary)
    13231320
    {
    1324-
    string = DatumGetCString(FunctionCall3(&out_functions[attnum - 1],
    1325-
    value,
    1326-
    ObjectIdGetDatum(typioparams[attnum - 1]),
    1327-
    Int32GetDatum(attr[attnum - 1]->atttypmod)));
    1321+
    string = DatumGetCString(FunctionCall1(&out_functions[attnum - 1],
    1322+
    value));
    13281323
    if (csv_mode)
    13291324
    {
    13301325
    CopyAttributeOutCSV(string, delim, quote, escape,
    @@ -1339,9 +1334,8 @@ CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
    13391334
    {
    13401335
    bytea *outputbytes;
    13411336

    1342-
    outputbytes = DatumGetByteaP(FunctionCall2(&out_functions[attnum - 1],
    1343-
    value,
    1344-
    ObjectIdGetDatum(typioparams[attnum - 1])));
    1337+
    outputbytes = DatumGetByteaP(FunctionCall1(&out_functions[attnum - 1],
    1338+
    value));
    13451339
    /* We assume the result will not have been toasted */
    13461340
    CopySendInt32(VARSIZE(outputbytes) - VARHDRSZ);
    13471341
    CopySendData(VARDATA(outputbytes),
    @@ -1366,8 +1360,6 @@ CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
    13661360
    MemoryContextDelete(mycontext);
    13671361

    13681362
    pfree(out_functions);
    1369-
    pfree(typioparams);
    1370-
    pfree(isvarlena);
    13711363
    pfree(force_quote);
    13721364
    }
    13731365

    src/backend/commands/typecmds.c

    Lines changed: 5 additions & 26 deletions
    Original file line numberDiff line numberDiff line change
    @@ -8,7 +8,7 @@
    88
    *
    99
    *
    1010
    * IDENTIFICATION
    11-
    * $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.70 2005/04/14 20:03:24 tgl Exp $
    11+
    * $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.71 2005/05/01 18:56:18 tgl Exp $
    1212
    *
    1313
    * DESCRIPTION
    1414
    * The "DefineFoo" routines take the parse tree and pick out the
    @@ -920,12 +920,11 @@ findTypeInputFunction(List *procname, Oid typeOid)
    920920
    static Oid
    921921
    findTypeOutputFunction(List *procname, Oid typeOid)
    922922
    {
    923-
    Oid argList[2];
    923+
    Oid argList[1];
    924924
    Oid procOid;
    925925

    926926
    /*
    927-
    * Output functions can take a single argument of the type, or two
    928-
    * arguments (data value, element OID).
    927+
    * Output functions can take a single argument of the type.
    929928
    *
    930929
    * For backwards compatibility we allow OPAQUE in place of the actual
    931930
    * type name; if we see this, we issue a warning and fix up the
    @@ -937,24 +936,11 @@ findTypeOutputFunction(List *procname, Oid typeOid)
    937936
    if (OidIsValid(procOid))
    938937
    return procOid;
    939938

    940-
    argList[1] = OIDOID;
    941-
    942-
    procOid = LookupFuncName(procname, 2, argList, true);
    943-
    if (OidIsValid(procOid))
    944-
    return procOid;
    945-
    946939
    /* No luck, try it with OPAQUE */
    947940
    argList[0] = OPAQUEOID;
    948941

    949942
    procOid = LookupFuncName(procname, 1, argList, true);
    950943

    951-
    if (!OidIsValid(procOid))
    952-
    {
    953-
    argList[1] = OIDOID;
    954-
    955-
    procOid = LookupFuncName(procname, 2, argList, true);
    956-
    }
    957-
    958944
    if (OidIsValid(procOid))
    959945
    {
    960946
    /* Found, but must complain and fix the pg_proc entry */
    @@ -1016,25 +1002,18 @@ findTypeReceiveFunction(List *procname, Oid typeOid)
    10161002
    static Oid
    10171003
    findTypeSendFunction(List *procname, Oid typeOid)
    10181004
    {
    1019-
    Oid argList[2];
    1005+
    Oid argList[1];
    10201006
    Oid procOid;
    10211007

    10221008
    /*
    1023-
    * Send functions can take a single argument of the type, or two
    1024-
    * arguments (data value, element OID).
    1009+
    * Send functions can take a single argument of the type.
    10251010
    */
    10261011
    argList[0] = typeOid;
    10271012

    10281013
    procOid = LookupFuncName(procname, 1, argList, true);
    10291014
    if (OidIsValid(procOid))
    10301015
    return procOid;
    10311016

    1032-
    argList[1] = OIDOID;
    1033-
    1034-
    procOid = LookupFuncName(procname, 2, argList, true);
    1035-
    if (OidIsValid(procOid))
    1036-
    return procOid;
    1037-
    10381017
    ereport(ERROR,
    10391018
    (errcode(ERRCODE_UNDEFINED_FUNCTION),
    10401019
    errmsg("function %s does not exist",

    src/backend/executor/spi.c

    Lines changed: 5 additions & 15 deletions
    Original file line numberDiff line numberDiff line change
    @@ -8,7 +8,7 @@
    88
    *
    99
    *
    1010
    * IDENTIFICATION
    11-
    * $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.137 2005/03/29 02:53:53 neilc Exp $
    11+
    * $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.138 2005/05/01 18:56:18 tgl Exp $
    1212
    *
    1313
    *-------------------------------------------------------------------------
    1414
    */
    @@ -632,9 +632,7 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
    632632
    result;
    633633
    bool isnull;
    634634
    Oid typoid,
    635-
    foutoid,
    636-
    typioparam;
    637-
    int32 typmod;
    635+
    foutoid;
    638636
    bool typisvarlena;
    639637

    640638
    SPI_result = 0;
    @@ -651,17 +649,11 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
    651649
    return NULL;
    652650

    653651
    if (fnumber > 0)
    654-
    {
    655652
    typoid = tupdesc->attrs[fnumber - 1]->atttypid;
    656-
    typmod = tupdesc->attrs[fnumber - 1]->atttypmod;
    657-
    }
    658653
    else
    659-
    {
    660654
    typoid = (SystemAttributeDefinition(fnumber, true))->atttypid;
    661-
    typmod = -1;
    662-
    }
    663655

    664-
    getTypeOutputInfo(typoid, &foutoid, &typioparam, &typisvarlena);
    656+
    getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
    665657

    666658
    /*
    667659
    * If we have a toasted datum, forcibly detoast it here to avoid
    @@ -672,10 +664,8 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
    672664
    else
    673665
    val = origval;
    674666

    675-
    result = OidFunctionCall3(foutoid,
    676-
    val,
    677-
    ObjectIdGetDatum(typioparam),
    678-
    Int32GetDatum(typmod));
    667+
    result = OidFunctionCall1(foutoid,
    668+
    val);
    679669

    680670
    /* Clean up detoasted copy, if any */
    681671
    if (val != origval)

    0 commit comments

    Comments
     (0)
    0