Skip to content

Commit d2db166

Browse files
committed
Require superuser privilege to create a binary-compatible cast, per
discussion some weeks ago. Also, add a check that two types to be binary-equivalenced match as to typlen, typbyval, and typalign; if they don't then it's surely a mistake to equivalence them.
1 parent 04c57d6 commit d2db166

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

doc/src/sgml/ref/create_cast.sgml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_cast.sgml,v 1.5 2002/09/18 21:35:20 tgl Exp $ -->
1+
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_cast.sgml,v 1.6 2002/10/04 22:08:44 tgl Exp $ -->
22

33
<refentry id="SQL-CREATECAST">
44
<refmeta>
@@ -81,10 +81,9 @@ SELECT 'The time is ' || now();
8181
</programlisting>
8282
will be allowed only if the cast from type <type>timestamp</> to
8383
<type>text</type> is marked <literal>AS IMPLICIT</>. Otherwise it
84-
will be necessary to write one of
84+
will be necessary to write the cast explicitly, for example
8585
<programlisting>
8686
SELECT 'The time is ' || CAST(now() AS text);
87-
SELECT 'The time is ' || now()::text;
8887
</programlisting>
8988
(We generally use the term <firstterm>implicit
9089
cast</firstterm> to describe this kind of cast.)
@@ -107,7 +106,9 @@ SELECT 'The time is ' || now()::text;
107106

108107
<para>
109108
To be able to create a cast, you must own the source or the target
110-
data type.
109+
data type. To create a binary-compatible cast, you must be superuser
110+
(this restriction is made because an erroneous binary-compatible cast
111+
conversion can easily crash the server).
111112
</para>
112113

113114
<variablelist>

src/backend/commands/functioncmds.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/commands/functioncmds.c,v 1.22 2002/09/21 18:39:25 tgl Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/commands/functioncmds.c,v 1.23 2002/10/04 22:08:44 tgl Exp $
1313
*
1414
* DESCRIPTION
1515
* These routines take the parse tree and pick out the
@@ -756,8 +756,35 @@ CreateCast(CreateCastStmt *stmt)
756756
}
757757
else
758758
{
759+
int16 typ1len;
760+
int16 typ2len;
761+
bool typ1byval;
762+
bool typ2byval;
763+
char typ1align;
764+
char typ2align;
765+
759766
/* indicates binary coercibility */
760767
funcid = InvalidOid;
768+
769+
/*
770+
* Must be superuser to create binary-compatible casts, since
771+
* erroneous casts can easily crash the backend.
772+
*/
773+
if (!superuser())
774+
elog(ERROR, "Must be superuser to create a cast WITHOUT FUNCTION");
775+
776+
/*
777+
* Also, insist that the types match as to size, alignment, and
778+
* pass-by-value attributes; this provides at least a crude check
779+
* that they have similar representations. A pair of types that
780+
* fail this test should certainly not be equated.
781+
*/
782+
get_typlenbyvalalign(sourcetypeid, &typ1len, &typ1byval, &typ1align);
783+
get_typlenbyvalalign(targettypeid, &typ2len, &typ2byval, &typ2align);
784+
if (typ1len != typ2len ||
785+
typ1byval != typ2byval ||
786+
typ1align != typ2align)
787+
elog(ERROR, "source and target datatypes are not physically compatible");
761788
}
762789

763790
/* convert CoercionContext enum to char value for castcontext */

0 commit comments

Comments
 (0)