Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit260faf0

Browse files
committed
Fix ALTER TABLE ADD COLUMN to disallow the same column types that are
disallowed by CREATE TABLE (eg, pseudo-types); also disallow these typesfrom being introduced by the range-function syntax. While at it, allowCREATE TABLE to create zero-column tables, per recent pghackers discussion.I am back-patching this into 7.3 since failure to disallow pseudo-typesis arguably a security hole.
1 parent88177f7 commit260faf0

File tree

5 files changed

+76
-45
lines changed

5 files changed

+76
-45
lines changed

‎src/backend/catalog/heap.c

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.237 2002/12/12 20:35:08 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.238 2002/12/16 18:39:22 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -307,8 +307,8 @@ heap_storage_create(Relation rel)
307307
*
308308
*this is done in 6 steps:
309309
*
310-
*1)CheckAttributeNames() is used to make certain the tuple
311-
* descriptor contains a valid set of attribute names
310+
*1)CheckAttributeNamesTypes() is used to make certain the tuple
311+
* descriptor contains a valid set of attribute names and types
312312
*
313313
*2) pg_class is opened and get_relname_relid()
314314
* performs a scan to ensure that no relation with the
@@ -334,20 +334,25 @@ heap_storage_create(Relation rel)
334334
*/
335335

336336
/* --------------------------------
337-
*CheckAttributeNames
337+
*CheckAttributeNamesTypes
338338
*
339339
*this is used to make certain the tuple descriptor contains a
340-
*valid set of attribute names. a problem simply generates
341-
*elog(ERROR) which aborts the current transaction.
340+
*valid set of attribute names and datatypes. a problem simply
341+
*generateselog(ERROR) which aborts the current transaction.
342342
* --------------------------------
343343
*/
344-
staticvoid
345-
CheckAttributeNames(TupleDesctupdesc,charrelkind)
344+
void
345+
CheckAttributeNamesTypes(TupleDesctupdesc,charrelkind)
346346
{
347347
inti;
348348
intj;
349349
intnatts=tupdesc->natts;
350350

351+
/* Sanity check on column count */
352+
if (natts<0||natts>MaxHeapAttributeNumber)
353+
elog(ERROR,"Number of columns is out of range (0 to %d)",
354+
MaxHeapAttributeNumber);
355+
351356
/*
352357
* first check for collision with system attribute names
353358
*
@@ -380,8 +385,29 @@ CheckAttributeNames(TupleDesc tupdesc, char relkind)
380385
}
381386

382387
/*
383-
* We also do some checking of the attribute types here.
384-
*
388+
* next check the attribute types
389+
*/
390+
for (i=0;i<natts;i++)
391+
{
392+
CheckAttributeType(NameStr(tupdesc->attrs[i]->attname),
393+
tupdesc->attrs[i]->atttypid);
394+
}
395+
}
396+
397+
/* --------------------------------
398+
*CheckAttributeType
399+
*
400+
*Verify that the proposed datatype of an attribute is legal.
401+
*This is needed because there are types (and pseudo-types)
402+
*in the catalogs that we do not support as elements of real tuples.
403+
* --------------------------------
404+
*/
405+
void
406+
CheckAttributeType(constchar*attname,Oidatttypid)
407+
{
408+
charatt_typtype=get_typtype(atttypid);
409+
410+
/*
385411
* Warn user, but don't fail, if column to be created has UNKNOWN type
386412
* (usually as a result of a 'retrieve into' - jolly)
387413
*
@@ -390,28 +416,20 @@ CheckAttributeNames(TupleDesc tupdesc, char relkind)
390416
* all references to complex types, but for now there's still some
391417
* Berkeley-derived code that thinks it can do this...)
392418
*/
393-
for (i=0;i<natts;i++)
419+
if (atttypid==UNKNOWNOID)
420+
elog(WARNING,"Attribute \"%s\" has an unknown type"
421+
"\n\tProceeding with relation creation anyway",
422+
attname);
423+
elseif (att_typtype=='p')
424+
elog(ERROR,"Attribute \"%s\" has pseudo-type %s",
425+
attname,format_type_be(atttypid));
426+
elseif (att_typtype=='c')
394427
{
395-
Oidatt_type=tupdesc->attrs[i]->atttypid;
396-
charatt_typtype=get_typtype(att_type);
397-
398-
if (att_type==UNKNOWNOID)
399-
elog(WARNING,"Attribute \"%s\" has an unknown type"
400-
"\n\tProceeding with relation creation anyway",
401-
NameStr(tupdesc->attrs[i]->attname));
402-
if (att_typtype=='p')
403-
elog(ERROR,"Attribute \"%s\" has pseudo-type %s",
404-
NameStr(tupdesc->attrs[i]->attname),
405-
format_type_be(att_type));
406-
if (att_typtype=='c')
407-
{
408-
Oidtyprelid=get_typ_typrelid(att_type);
428+
Oidtyprelid=get_typ_typrelid(atttypid);
409429

410-
if (get_rel_relkind(typrelid)==RELKIND_COMPOSITE_TYPE)
411-
elog(ERROR,"Attribute \"%s\" has composite type %s",
412-
NameStr(tupdesc->attrs[i]->attname),
413-
format_type_be(att_type));
414-
}
430+
if (get_rel_relkind(typrelid)==RELKIND_COMPOSITE_TYPE)
431+
elog(ERROR,"Attribute \"%s\" has composite type %s",
432+
attname,format_type_be(atttypid));
415433
}
416434
}
417435

@@ -689,11 +707,8 @@ heap_create_with_catalog(const char *relname,
689707
* sanity checks
690708
*/
691709
Assert(IsNormalProcessingMode()||IsBootstrapProcessingMode());
692-
if (tupdesc->natts <=0||tupdesc->natts>MaxHeapAttributeNumber)
693-
elog(ERROR,"Number of columns is out of range (1 to %d)",
694-
MaxHeapAttributeNumber);
695710

696-
CheckAttributeNames(tupdesc,relkind);
711+
CheckAttributeNamesTypes(tupdesc,relkind);
697712

698713
if (get_relname_relid(relname,relnamespace))
699714
elog(ERROR,"Relation '%s' already exists",relname);

‎src/backend/commands/tablecmds.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.61 2002/12/15 16:17:42 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.62 2002/12/16 18:39:22 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -125,7 +125,6 @@ DefineRelation(CreateStmt *stmt, char relkind)
125125
charrelname[NAMEDATALEN];
126126
OidnamespaceId;
127127
List*schema=stmt->tableElts;
128-
intnumberOfAttributes;
129128
OidrelationId;
130129
Relationrel;
131130
TupleDescdescriptor;
@@ -174,10 +173,6 @@ DefineRelation(CreateStmt *stmt, char relkind)
174173
stmt->relation->istemp,
175174
&inheritOids,&old_constraints,&parentHasOids);
176175

177-
numberOfAttributes=length(schema);
178-
if (numberOfAttributes <=0)
179-
elog(ERROR,"DefineRelation: please inherit from a relation or define an attribute");
180-
181176
/*
182177
* Create a relation descriptor from the relation schema and create
183178
* the relation. Note that in this stage only inherited (pre-cooked)
@@ -1800,6 +1795,9 @@ AlterTableAddColumn(Oid myrelid,
18001795
typeTuple=typenameType(colDef->typename);
18011796
tform= (Form_pg_type)GETSTRUCT(typeTuple);
18021797

1798+
/* make sure datatype is legal for a column */
1799+
CheckAttributeType(colDef->colname,HeapTupleGetOid(typeTuple));
1800+
18031801
attributeTuple=heap_addheader(Natts_pg_attribute,
18041802
false,
18051803
ATTRIBUTE_TUPLE_SIZE,

‎src/backend/parser/parse_clause.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.102 2002/12/12 20:35:13 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.103 2002/12/16 18:39:22 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
1515

1616
#include"postgres.h"
1717

1818
#include"access/heapam.h"
19+
#include"catalog/heap.h"
1920
#include"nodes/makefuncs.h"
2021
#include"optimizer/clauses.h"
2122
#include"optimizer/tlist.h"
@@ -502,6 +503,18 @@ transformRangeFunction(ParseState *pstate, RangeFunction *r)
502503
elog(ERROR,"cannot use aggregate function in FROM function expression");
503504
}
504505

506+
/*
507+
* If a coldeflist is supplied, ensure it defines a legal set of names
508+
* (no duplicates) and datatypes (no pseudo-types, for instance).
509+
*/
510+
if (r->coldeflist)
511+
{
512+
TupleDesctupdesc;
513+
514+
tupdesc=BuildDescForRelation(r->coldeflist);
515+
CheckAttributeNamesTypes(tupdesc,RELKIND_COMPOSITE_TYPE);
516+
}
517+
505518
/*
506519
* OK, build an RTE for the function.
507520
*/

‎src/backend/utils/cache/relcache.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.182 2002/12/15 21:01:34 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.183 2002/12/16 18:39:22 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -570,7 +570,8 @@ RelationBuildTupleDesc(RelationBuildDescInfo buildinfo,
570570
* cases for attnum=1 that used to exist in fastgetattr() and
571571
* index_getattr().
572572
*/
573-
relation->rd_att->attrs[0]->attcacheoff=0;
573+
if (relation->rd_rel->relnatts>0)
574+
relation->rd_att->attrs[0]->attcacheoff=0;
574575

575576
/*
576577
* Set up constraint/default info
@@ -2049,7 +2050,7 @@ RelationBuildLocalRelation(const char *relname,
20492050
inti;
20502051
boolhas_not_null;
20512052

2052-
AssertArg(natts>0);
2053+
AssertArg(natts >=0);
20532054

20542055
/*
20552056
* switch to the cache context to create the relcache entry.

‎src/include/catalog/heap.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: heap.h,v 1.59 2002/11/11 22:19:23 tgl Exp $
10+
* $Id: heap.h,v 1.60 2002/12/16 18:39:22 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -74,4 +74,8 @@ extern Form_pg_attribute SystemAttributeDefinition(AttrNumber attno,
7474
externForm_pg_attributeSystemAttributeByName(constchar*attname,
7575
boolrelhasoids);
7676

77+
externvoidCheckAttributeNamesTypes(TupleDesctupdesc,charrelkind);
78+
79+
externvoidCheckAttributeType(constchar*attname,Oidatttypid);
80+
7781
#endif/* HEAP_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp