1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Check to ensure the number of primary key fields supplied does not

exceed the total number of non-dropped source table fields for
dblink_build_sql_*(). Addresses bug report from Rushabh Lathia.

Backpatch all the way to the 7.3 branch.
This commit is contained in:
Joe Conway
2010-02-03 23:01:47 +00:00
parent 46b6fbc9b4
commit ff6a12b3f3
3 changed files with 72 additions and 1 deletions

View File

@ -8,7 +8,7 @@
* Darko Prenosil <Darko.Prenosil@finteh.hr>
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
*
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.60.2.4 2009/01/03 19:58:10 joe Exp $
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.60.2.5 2010/02/03 23:01:47 joe Exp $
* Copyright (c) 2001-2006, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
@ -92,6 +92,7 @@ static Oid get_relid_from_relname(text *relname_text);
static char *generate_relation_name(Oid relid);
static char *connstr_strip_password(const char *connstr);
static void dblink_security_check(PGconn *conn, remoteConn *rconn, const char *connstr);
static int get_nondropped_natts(Oid relid);
/* Global */
static remoteConn *pconn = NULL;
@ -1388,6 +1389,7 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
int src_nitems;
int tgt_nitems;
char *sql;
int nondropped_natts;
/*
* Convert relname to rel OID.
@ -1415,6 +1417,15 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
errmsg("input for number of primary key " \
"attributes too large")));
/*
* ensure we don't ask for more pk attributes than we have
* non-dropped columns
*/
nondropped_natts = get_nondropped_natts(relid);
if (pknumatts > nondropped_natts)
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("number of primary key fields exceeds number of specified relation attributes")));
/*
* Source array is made up of key values that will be used to locate the
* tuple of interest from the local system.
@ -1480,6 +1491,7 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS)
int2vector *pkattnums = (int2vector *) PG_GETARG_POINTER(1);
int32 pknumatts_tmp = PG_GETARG_INT32(2);
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
int nondropped_natts;
Oid relid;
int16 pknumatts = 0;
char **tgt_pkattvals;
@ -1512,6 +1524,15 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS)
errmsg("input for number of primary key " \
"attributes too large")));
/*
* ensure we don't ask for more pk attributes than we have
* non-dropped columns
*/
nondropped_natts = get_nondropped_natts(relid);
if (pknumatts > nondropped_natts)
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("number of primary key fields exceeds number of specified relation attributes")));
/*
* Target array is made up of key values that will be used to build the
* SQL string for use on the remote system.
@ -1567,6 +1588,7 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
int32 pknumatts_tmp = PG_GETARG_INT32(2);
ArrayType *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
int nondropped_natts;
Oid relid;
int16 pknumatts = 0;
char **src_pkattvals;
@ -1601,6 +1623,15 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
errmsg("input for number of primary key " \
"attributes too large")));
/*
* ensure we don't ask for more pk attributes than we have
* non-dropped columns
*/
nondropped_natts = get_nondropped_natts(relid);
if (pknumatts > nondropped_natts)
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("number of primary key fields exceeds number of specified relation attributes")));
/*
* Source array is made up of key values that will be used to locate the
* tuple of interest from the local system.
@ -2441,3 +2472,28 @@ dblink_security_check(PGconn *conn, remoteConn *rconn, const char *connstr)
PQfinish(conn);
}
}
static int
get_nondropped_natts(Oid relid)
{
int nondropped_natts = 0;
TupleDesc tupdesc;
Relation rel;
int natts;
int i;
rel = relation_open(relid, AccessShareLock);
tupdesc = rel->rd_att;
natts = tupdesc->natts;
for (i = 0; i < natts; i++)
{
if (tupdesc->attrs[i]->attisdropped)
continue;
nondropped_natts++;
}
relation_close(rel, AccessShareLock);
return nondropped_natts;
}